elasticsearch笔记(4) java操作es的文档
------------恢复内容开始------------
一. 创建document, 分为自动生成id和手动生成id
1 # 创建文档 手动生成id 2 POST /books/noval/1011 3 { 4 "name": "红楼梦", 5 "author": "曹雪芹", 6 "count": 5932222, 7 "onsale": "1646", 8 "descr": "红楼梦是一本女生命运的小说" 9 } 10 11 # 创建文档 自动生成id 12 POST /books/noval 13 { 14 "name": "水浒传", 15 "author": "施耐庵", 16 "count": 59322, 17 "onsale": "1546", 18 "descr": "一本关于农民起义的小说" 19 }
1 // 准备json数据 2 Person person = new Person(); 3 person.setName("jim"); 4 person.setAge(10); 5 person.setId(1001); 6 7 ObjectMapper mapper = new ObjectMapper();//jackson-bind插件中的对象 8 String string = mapper.writeValueAsString(person); 9 10 11 // 准备IndexRequest对象(设置好index type 以及id,不加id会自动创建) POST /books/noval/1011 12 IndexRequest indexRequest = new IndexRequest(index,type, person.getId().toString()); 13 indexRequest.source(string, XContentType.JSON); 14 15 // 获取连接restHighLevelClient 16 HttpHost httpHost = new HttpHost(HOST,PORT); 17 RestClientBuilder restClientBuilder = RestClient.builder(httpHost); 18 RestHighLevelClient restHighLevelClient = new RestHighLevelClient(restClientBuilder); 19 // 通过client对象执行添加 20 IndexResponse indexResponse = restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT); 21 DocWriteResponse.Result result = indexResponse.getResult(); 22 System.out.println(result); 23 }
二. 修改document
1 // 1. 创建map 2 Map<String, Object> map = new HashMap(); 3 map.put("name","tom"); 4 map.put("age", 100); 5 // 2.建立updateRequest对象封装数据; 6 String index = "person"; 7 String type = "man"; 8 String id = "1001";//需要通过id操作 9 UpdateRequest updateRequest = new UpdateRequest(index, type, id); 10 updateRequest.doc(map); 11 // 3. clien操作 12 HttpHost httpHost = new HttpHost(HOST,PORT); 13 RestClientBuilder restClientBuilder = RestClient.builder(httpHost); 14 RestHighLevelClient restHighLevelClient = new RestHighLevelClient(restClientBuilder); 15 UpdateResponse update = restHighLevelClient.update(updateRequest, RequestOptions.DEFAULT); 16 System.out.println(update);
三, 删除document
1 //1封装request对象 2 String index = "person"; 3 String type = "man"; 4 String id = "1001";//需要通过id操作 5 DeleteRequest deleteRequest = new DeleteRequest(index, type, id); 6 // 2.用client删除 7 HttpHost httpHost = new HttpHost(HOST,PORT); 8 RestClientBuilder restClientBuilder = RestClient.builder(httpHost); 9 RestHighLevelClient restHighLevelClient = new RestHighLevelClient(restClientBuilder); 10 DeleteResponse delete = restHighLevelClient.delete(deleteRequest, RequestOptions.DEFAULT); 11 System.out.println(delete);
四. 批量操作(通过client.bulk(bulk)来操作)
4.1 批量添加
1 // 1. 准备多个person 2 Person person1 = new Person(1003,"jack1",10); 3 Person person2 = new Person(1004,"jack2",10); 4 Person person3 = new Person(1005,"jack3",10); 5 6 ObjectMapper mapper = new ObjectMapper(); 7 String p1 = mapper.writeValueAsString(person1); 8 String p2 = mapper.writeValueAsString(person2); 9 String p3 = mapper.writeValueAsString(person3); 10 11 // 2. 创建BulkRequest对象 12 BulkRequest bulkRequest = new BulkRequest(); 13 bulkRequest.add(new IndexRequest("person","man",person1.getId().toString()).source(p1, XContentType.JSON)) 14 .add(new IndexRequest("person","man",person2.getId().toString()).source(p2, XContentType.JSON)) 15 .add(new IndexRequest("person","man",person3.getId().toString()).source(p3, XContentType.JSON)); 16 17 // 3通过client操作 18 HttpHost httpHost = new HttpHost(HOST,PORT); 19 RestClientBuilder restClientBuilder = RestClient.builder(httpHost); 20 RestHighLevelClient restHighLevelClient = new RestHighLevelClient(restClientBuilder); 21 22 BulkResponse bulk = restHighLevelClient.bulk(bulkRequest,RequestOptions.DEFAULT); 23 System.out.println(bulk);
4.2 批量删除
1 // 1.准备deleteRequest 2 String index = "person"; 3 String type = "man"; 4 5 DeleteRequest deleteRequest1 = new DeleteRequest(index, type, "1003"); 6 DeleteRequest deleteRequest2 = new DeleteRequest(index, type, "1004"); 7 8 // 2.创建bulk 9 BulkRequest bulkRequest = new BulkRequest(); 10 bulkRequest.add(deleteRequest1).add(deleteRequest2); 11 12 // 3通过client操作 13 HttpHost httpHost = new HttpHost(HOST, PORT); 14 RestClientBuilder restClientBuilder = RestClient.builder(httpHost); 15 RestHighLevelClient restHighLevelClient = new RestHighLevelClient(restClientBuilder); 16 17 BulkResponse bulk = restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT); 18 System.out.println(bulk);