java *** 作es 创建索引和类型org.elasticsearch elasticsearch6.8.0 org.elasticsearch.client transport6.8.0 org.elasticsearch.plugin transport-netty4-client6.8.0
- 创建客户端 *** 作对象
//创建ES客户端 *** 作对象 @Test public void init() throws UnknownHostException { PreBuiltTransportClient preBuiltTransportClient = new PreBuiltTransportClient(Settings.EMPTY); preBuiltTransportClient.addTransportAddress(new TransportAddress( InetAddress.getByName("192.168.202.200"),9300)); }
- 创建索引
//创建索引 @Test public void createIndex() throws UnknownHostException, ExecutionException, InterruptedException { PreBuiltTransportClient preBuiltTransportClient = new PreBuiltTransportClient(Settings.EMPTY); preBuiltTransportClient.addTransportAddress(new TransportAddress( InetAddress.getByName("192.168.202.200"),9300)); //定义索引请求 CreateIndexRequest ems = new CreateIndexRequest("ems"); //执行索引创建 CreateIndexResponse createIndexResponse = preBuiltTransportClient.admin().indices().create(ems).get(); System.out.println(createIndexResponse.isAcknowledged()); }
- 删除索引
//删除索引 @Test public void deleteIndex() throws UnknownHostException, ExecutionException, InterruptedException { PreBuiltTransportClient preBuiltTransportClient = new PreBuiltTransportClient(Settings.EMPTY); preBuiltTransportClient.addTransportAddress(new TransportAddress( InetAddress.getByName("192.168.202.200"),9300)); //定义索引请求 DeleteIndexRequest ems = new DeleteIndexRequest("ems"); //执行索引删除 AcknowledgedResponse acknowledgedResponse = preBuiltTransportClient.admin().indices().delete(ems).get(); System.out.println(acknowledgedResponse.isAcknowledged()); }
- 创建索引和类型
//创建索引类型和映射 @Test public void init() throws UnknownHostException, ExecutionException, InterruptedException { PreBuiltTransportClient preBuiltTransportClient = new PreBuiltTransportClient(Settings.EMPTY); preBuiltTransportClient.addTransportAddress(new TransportAddress( InetAddress.getByName("192.168.202.200"),9300)); //创建索引 CreateIndexRequest ems = new CreateIndexRequest("ems"); //定义json格式映射 String json = "{"properties":{"name":{"type":"text","analyzer":"ik_max_word"},"age":{"type":"integer"},"sex":{"type":"keyword"},"content":{"type":"text","analyzer":"ik_max_word"}}}"; //设置类型和mapping ems.mapping("emp",json, XContentType.JSON); //执行创建 CreateIndexResponse createIndexResponse = preBuiltTransportClient.admin().indices().create(ems).get(); System.out.println(createIndexResponse.isAcknowledged()); }索引一条记录
- 指定id索引记录
//索引一条文档 指定id @Test public void createIndexOptionId() throws JsonProcessingException { Emp emp = new Emp("小陈", 23, "男", "这是一个单纯的少年,单纯的我!"); String s = JSONObject.toJSONString(emp); IndexResponse indexResponse = transportClient.prepareIndex("ems", "emp", "1").setSource(s, XContentType.JSON).get(); System.out.println(indexResponse.status()); }各种普通查询
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)