依赖
<dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.62</version>
</dependency>
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-data-elasticsearch</artifactId><version>7.6.2</version>
</dependency>
配置类
@Configuration
public class ElasticsearchClientConfig {@Beanpublic RestHighLevelClient restHighLevelClient(){return new RestHighLevelClient(RestClient.builder(// 是集群可以配置多个new HttpHost("127.0.0.1",9200,"http")));}
}
api 使用
@SpringBootTest
class ApplicationTests {@Autowired// 在容器中的bean的id@Qualifier("restHighLevelClient")private RestHighLevelClient restHighLevelClient;// 创建索引@Testvoid createIndex() throws IOException {// 创建索引请求CreateIndexRequest createIndexRequest = new CreateIndexRequest("springboot_01");// 客户端执行请求,返回响应CreateIndexResponse createIndexResponse = restHighLevelClient.indices().create(createIndexRequest, RequestOptions.DEFAULT);System.out.println(createIndexResponse);}// 获取索引@Testvoid getIndex() throws IOException {GetIndexRequest getIndexRequest = new GetIndexRequest("springboot_01");// 是否存在boolean exists = restHighLevelClient.indices().exists(getIndexRequest, RequestOptions.DEFAULT);System.out.println(exists);GetIndexResponse getIndexResponse = restHighLevelClient.indices().get(getIndexRequest, RequestOptions.DEFAULT);System.out.println(getIndexResponse);}// 删除索引@Testvoid deleteIndex() throws IOException {DeleteIndexRequest deleteIndexRequest = new DeleteIndexRequest("springboot_01");AcknowledgedResponse acknowledgedResponse = restHighLevelClient.indices().delete(deleteIndexRequest, RequestOptions.DEFAULT);// 是否成功删除System.out.println(acknowledgedResponse.isAcknowledged());}@Testvoid testDocument() throws IOException {// 添加文档IndexRequest indexRequest = new IndexRequest("springboot_01");// 文档idindexRequest.id("1");indexRequest.timeout("1s");// JSON格式数据indexRequest.source(JSON.toJSONString(new User("zhangsan",1)), XContentType.JSON);// 发送请求IndexResponse indexResponse = restHighLevelClient.index(indexRequest, RequestOptions.DEFAULT);System.out.println(indexResponse.toString());// 更新文档UpdateRequest updateRequest = new UpdateRequest("springboot_01","1");updateRequest.doc(JSON.toJSONString(new User("zhangsan",3)), XContentType.JSON);UpdateResponse updateResponse = restHighLevelClient.update(updateRequest, RequestOptions.DEFAULT);System.out.println(updateResponse);// 获取文档信息GetRequest getRequest = new GetRequest("springboot_01","1");// 判断文档是否存在boolean exists = restHighLevelClient.exists(getRequest, RequestOptions.DEFAULT);System.out.println(exists);GetResponse getResponse = restHighLevelClient.get(getRequest, RequestOptions.DEFAULT);System.out.println(getResponse);// 删除文档记录DeleteRequest deleteRequest = new DeleteRequest("springboot_01","1");DeleteResponse deleteResponse = restHighLevelClient.delete(deleteRequest, RequestOptions.DEFAULT);System.out.println(deleteResponse);}// 文档批量插入@Testvoid addDocumentBatch() throws IOException {BulkRequest bulkRequest = new BulkRequest();bulkRequest.timeout("10s");for (int i = 0; i < 5; i++) {bulkRequest.add(new IndexRequest("springboot_01")// 不设置id默认是随机id.id(""+(i+1)).source(JSON.toJSONString(new User("zhangsan",i+1)), XContentType.JSON));}BulkResponse bulkResponse = restHighLevelClient.bulk(bulkRequest, RequestOptions.DEFAULT);// 是否失败System.out.println(bulkResponse.hasFailures());}// 文档查询@Testvoid searchDocument() throws IOException {SearchRequest searchRequest = new SearchRequest("springboot_01");// 构建搜索条件SearchSourceBuilder searchSourceBuilder = new SearchSourceBuilder();// 匹配所有//MatchAllQueryBuilder matchAllQueryBuilder = QueryBuilders.matchAllQuery();// 精确查询TermQueryBuilder termQueryBuilder = QueryBuilders.termQuery("name", "zhangsan");searchSourceBuilder.query(termQueryBuilder);searchSourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS));// 搜索条件放入请求searchRequest.source(searchSourceBuilder);SearchResponse searchResponse = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);System.out.println(JSON.toJSONString(searchResponse.getHits()));for (SearchHit searchHit : searchResponse.getHits().getHits()) {System.out.println(searchHit.getSourceAsMap());}}
}