ElasticSearch6.6.0强大的JAVA|ElasticSearch6.6.0强大的JAVA API详解

从maven导入

org.elasticsearch.client elasticsearch-rest-high-level-client 6.6.0

参见文档地址
【ElasticSearch6.6.0强大的JAVA|ElasticSearch6.6.0强大的JAVA API详解】https://www.elastic.co/guide/en/elasticsearch/client/java-rest/6.6/java-rest-high.html
DEMO
package com.hengyi; import org.apache.http.HttpHost; import org.elasticsearch.action.ActionListener; import org.elasticsearch.action.admin.indices.create.CreateIndexRequest; import org.elasticsearch.action.admin.indices.create.CreateIndexResponse; import org.elasticsearch.action.admin.indices.delete.DeleteIndexRequest; import org.elasticsearch.action.admin.indices.get.GetIndexRequest; import org.elasticsearch.action.admin.indices.get.GetIndexResponse; 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.support.master.AcknowledgedResponse; import org.elasticsearch.action.update.UpdateRequest; import org.elasticsearch.action.update.UpdateResponse; import org.elasticsearch.client.RequestOptions; import org.elasticsearch.client.RestClient; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.SearchHits; import org.elasticsearch.search.builder.SearchSourceBuilder; import org.junit.jupiter.api.Test; import java.util.Arrays; import java.util.HashMap; import java.util.Map; import java.util.concurrent.TimeUnit; public class EsTest {/** * 创建Index */ @Test public void createIndex() throws Exception{ RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost("192.168.199.213", 9200, "http"))); //初始化 CreateIndexRequest request = new CreateIndexRequest("employee"); //创建索引 //创建的每个索引都可以有与之关联的特定设置。 request.settings(Settings.builder() .put("index.number_of_shards", 3) .put("index.number_of_replicas", 2) ); //可选参数 //request.timeout(TimeValue.timeValueMinutes(2)); //超时,等待所有节点被确认(使用TimeValue方式) //request.timeout("2m"); //超时,等待所有节点被确认(使用字符串方式) request.masterNodeTimeout(TimeValue.timeValueMinutes(1)); //连接master节点的超时时间(使用TimeValue方式) //request.masterNodeTimeout("1m"); //连接master节点的超时时间(使用字符串方式) //request.waitForActiveShards(2); //在创建索引API返回响应之前等待的活动分片副本的数量,以int形式表示。CreateIndexResponse createIndexResponse = client.indices().create(request, RequestOptions.DEFAULT); client.close(); System.out.println("isAcknowledged:" + createIndexResponse.isAcknowledged()); System.out.println("isShardsAcknowledged:" + createIndexResponse.isShardsAcknowledged()); }/** * 删除索引 * @throws Exception */ @Test public void deleteIndex() throws Exception{ RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost("192.168.199.213", 9200, "http"))); //初始化 DeleteIndexRequest request = new DeleteIndexRequest("goods"); request.timeout(TimeValue.timeValueMinutes(2)); //request.timeout("2m"); request.masterNodeTimeout(TimeValue.timeValueMinutes(1)); //request.masterNodeTimeout("1m"); AcknowledgedResponse deleteIndexResponse = client.indices().delete(request, RequestOptions.DEFAULT); System.out.println("isAcknowledged:" + deleteIndexResponse.isAcknowledged()); client.close(); }/** * 检查索引是否存在 * @throws Exception */ @Test public void existIndex()throws Exception{ RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost("192.168.199.213", 9200, "http"))); //初始化 GetIndexRequest request = new GetIndexRequest(); request.indices("goods"); boolean exists = client.indices().exists(request, RequestOptions.DEFAULT); System.out.println("索引goods:"+exists); client.close(); }/** * 获取索引 * @throws Exception */ @Test public void getIndex() throws Exception{ RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost("192.168.199.213", 9200, "http"))); //初始化 GetIndexRequest request = new GetIndexRequest().indices("shop"); GetIndexResponse getIndexResponse = client.indices().get(request, RequestOptions.DEFAULT); System.out.println(Arrays.toString(getIndexResponse.getIndices())); client.close(); }/** * 创建或者更新文档 * @throws Exception */ @Test public void insertDocument()throws Exception{ RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost("192.168.199.213", 9200, "http"))); //初始化Map jsonMap = new HashMap<>(); jsonMap.put("title", "完美的衣服"); jsonMap.put("create_date","2019-02-17"); IndexRequest request = new IndexRequest("shop", "goods", "2").source(jsonMap); IndexResponse indexResponse = client.index(request, RequestOptions.DEFAULT); System.out.println("id:"+indexResponse.getId()); client.close(); }/** * 获取文档 * @throws Exception */ @Test public void getDocument()throws Exception{ RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost("192.168.199.213", 9200, "http"))); //初始化 GetRequest request = new GetRequest( "shop","goods","1"); GetResponse getResponse = client.get(request, RequestOptions.DEFAULT); System.out.println("数据如下:"+getResponse.getSource().toString()); client.close(); }/** * 判断某一个文档是否存在 * @throws Exception */ @Test public void existDocument() throws Exception{ RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost("192.168.199.213", 9200, "http"))); //初始化 GetRequest request = new GetRequest( "shop","goods","1"); boolean exists = client.exists(request, RequestOptions.DEFAULT); System.out.println("是否存在:"+exists); client.close(); }/** * 删除文档 * @throws Exception */ @Test public void deleteDocuemnt()throws Exception{ RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost("192.168.199.213", 9200, "http"))); //初始化 DeleteRequest request = new DeleteRequest("shop", "goods", "2"); request.timeout(TimeValue.timeValueMinutes(2)); // request.timeout("2m"); DeleteResponse deleteResponse = client.delete(request, RequestOptions.DEFAULT); System.out.println(deleteResponse.getSeqNo()); client.close(); }/** * 更新文档 * @throws Exception */ @Test public void updateDocument()throws Exception{ RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost("192.168.199.213", 9200, "http"))); //初始化 Map jsonMap = new HashMap<>(); jsonMap.put("title", "完美的衣服222"); jsonMap.put("create_date","2019-02-17"); UpdateRequest request = new UpdateRequest("shop", "goods", "1").doc(jsonMap); UpdateResponse updateResponse = client.update(request, RequestOptions.DEFAULT); System.out.println(updateResponse.getId()); client.close(); }/** * 条件搜索 * @throws Exception */ @Test public void searchDocument()throws Exception{ RestHighLevelClient client = new RestHighLevelClient(RestClient.builder(new HttpHost("192.168.199.213", 9200, "http"))); //初始化 SearchRequest request = new SearchRequest(); request.indices("shop"); SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); sourceBuilder.query(QueryBuilders.matchQuery("title", "鞋子")); ## 排序start sourceBuilder.sort(new ScoreSortBuilder().order(SortOrder.DESC)); sourceBuilder.sort(new FieldSortBuilder("_uid").order(SortOrder.ASC)); ## 排序end sourceBuilder.from(0); sourceBuilder.size(5); sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS)); request.source(sourceBuilder); SearchResponse searchResponse = client.search(request, RequestOptions.DEFAULT); SearchHits hits = searchResponse.getHits(); SearchHit[] searchHits = hits.getHits(); System.out.println("SearchHit:" + searchHits.length); for(SearchHit hit : searchHits){ Map datas = hit.getSourceAsMap(); System.out.println(datas.toString()); }client.close(); } }

    推荐阅读