ES 关于文档的API操作
添加FastJSON依赖
<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.60</version> </dependency>
关于文档的操作
package com.dance.danceesapi.test; import com.alibaba.fastjson.JSON; import com.dance.danceesapi.pojo.User; import org.elasticsearch.action.bulk.BulkRequest; import org.elasticsearch.action.bulk.BulkResponse; import org.elasticsearch.action.delete.DeleteRequest; import org.elasticsearch.action.delete.DeleteResponse; import org.elasticsearch.action.get.GetRequest; import org.elasticsearch.action.get.GetResponse; import org.elasticsearch.action.index.IndexRequest; import org.elasticsearch.action.index.IndexResponse; import org.elasticsearch.action.search.SearchRequest; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.action.update.UpdateRequest; import org.elasticsearch.action.update.UpdateResponse; import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.common.xcontent.XContentType; import org.elasticsearch.index.query.MatchQueryBuilder; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.builder.SearchSourceBuilder; import org.elasticsearch.search.fetch.subphase.FetchSourceContext; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.boot.test.context.SpringBootTest; import java.io.IOException; /** * 关于文档的API的操作 */ @SpringBootTest public class TestDocument { @Autowired @Qualifier("restHighLevelClient") RestHighLevelClient restHighLevelClient; /** * 测试添加文档 * @throws IOException */ @Test void addDocument() throws IOException { // 创建文档对象 User user = new User("彼岸舞111", 18); // 指定索引库 IndexRequest flower = new IndexRequest("flower"); // 设置参数 id 超时时间 和数据源 flower.id("4").timeout(TimeValue.timeValueSeconds(5)).source(JSON.toJSONString(user), XContentType.JSON); // 执行请求 IndexResponse index = restHighLevelClient.index(flower, RequestOptions.DEFAULT); System.out.println(index.toString()); System.out.println(index.status()); } /** * 测试文档是否存在 * @throws IOException */ @Test void existDocument() throws IOException { GetRequest flower = new GetRequest("flower", "1"); boolean exists = restHighLevelClient.exists(flower, RequestOptions.DEFAULT); System.out.println(exists); } /** * 获取文档信息 * @throws IOException */ @Test void getDocument() throws IOException { GetRequest flower = new GetRequest("flower", "1"); GetResponse documentFields = restHighLevelClient.get(flower, RequestOptions.DEFAULT); System.out.println(documentFields.getSourceAsString()); } /** * 测试文档的更新 * @throws IOException */ @Test void updateDocument() throws IOException { User user = new User("彼岸草小姐姐",19); UpdateRequest flower = new UpdateRequest("flower","1"); flower.timeout(TimeValue.timeValueSeconds(5)); flower.doc(JSON.toJSONString(user),XContentType.JSON); UpdateResponse update = restHighLevelClient.update(flower, RequestOptions.DEFAULT); System.out.println(update); System.out.println(update.status()); } /** * 测试文档的删除 * @throws IOException */ @Test void deleteDocument() throws IOException { DeleteRequest flower = new DeleteRequest("flower", "4"); DeleteResponse delete = restHighLevelClient.delete(flower, RequestOptions.DEFAULT); System.out.println(delete); System.out.println(delete.status()); } /** * 测试批量 增删改查都可以 只需要更换不同的Request就可以了 * @throws IOException */ @Test void batchDocument() throws IOException { BulkRequest flower = new BulkRequest(); for (int i = 0; i < 100; i++) { int y = i + 4; User user = new User("测试" + y, y); flower.add( new IndexRequest("flower") .id(y+"") .source(JSON.toJSONString(user),XContentType.JSON) ); } BulkResponse bulk = restHighLevelClient.bulk(flower, RequestOptions.DEFAULT); System.out.println(bulk); System.out.println(bulk.status()); // 返回false 代表没有失败 System.out.println(bulk.hasFailures()); } @Test void query() throws IOException { SearchRequest flower = new SearchRequest("flower"); // 包含的字段 String[] includes = new String[]{"name"}; // 排除的字段 String[] excludes = new String[]{"age"}; // 构造搜索条件 SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); // match匹配 字段 MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("name", "彼岸"); // 加入条件 sourceBuilder.query(matchQueryBuilder); sourceBuilder.fetchSource(new FetchSourceContext(true,includes,excludes)); // 分页 sourceBuilder.from(0); sourceBuilder.size(2); flower.source(sourceBuilder); SearchResponse search = restHighLevelClient.search(flower, RequestOptions.DEFAULT); System.out.println(search); System.out.println(search.getHits()); for (SearchHit hit : search.getHits().getHits()) { System.out.println(hit.getSourceAsString()); } } }
作者:彼岸舞
时间:2020\09\11
内容关于:ElasticSearch
本文来源于网络,只做技术分享,一概不负任何责任