1、說明:html
1、Elasticsearch提供了兩個JAVA REST Client版本:java
一、java low level rest client:apache
低級別的rest客戶端,經過http與集羣交互,用戶需本身編組請求JSON串,及解析響應JSON串。兼容全部Elasticsearch版本。json
特色:maven引入api
使用介紹: https://www.elastic.co/guide/en/elasticsearch/client/java-rest/current/java-rest-low.html數組
二、java high rest client:app
高級別的REST客戶端,基於低級別的REST客戶端,增長了編組請求JSON串、解析響應JSON串等相關API,使用的版本須要保存和ES服務一致的版本,不然會有版本問題。負載均衡
從6.0.0開始加入的,目的是以java面向對象的方式進行請求、響應處理。異步
每一個API支持 同步、異步 兩種方式,同步方法之間返回一個結果對象。異步的方法以async爲後綴,經過listener參數來通知結果。高級java resy客戶端依賴Elasticsearch core pprojectasync
兼容性說明:
依賴jdk1.8和Elasticsearch core project
2、Java Low Level Rest Client的使用
一、JAVA High Level Rest Client maven集成
<dependency>
<groupId>org.elasticsearch.client</groupId>
<artifactId>elasticsearch-rest-high-level-client</artifactId>
<version>6.3.1</version>
</dependency>
版本:
Elasticsearch 6.3.1
pom文件:
<dependencies> <dependency> <groupId>org.elasticsearch.client</groupId> <artifactId>elasticsearch-rest-high-level-client</artifactId> <version>6.3.1</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-api</artifactId> <version>2.7</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.7</version> </dependency> <dependency> <groupId>net.sf.json-lib</groupId> <artifactId>json-lib</artifactId> <version>0.9</version> </dependency> </dependencies>
給定集羣的多個節點地址,將客戶端負載均衡地向這個節點地址集發請求:
import org.apache.http.HttpHost; import org.elasticsearch.client.RestClient; import org.elasticsearch.client.RestHighLevelClient; /** * @Author: xiaolaotou * @Date: 2019/4/28 */ public class ClientUtil { public static RestHighLevelClient getClient(){ RestHighLevelClient client = new RestHighLevelClient( RestClient.builder( new HttpHost("192.168.200.100",9200,"http"), new HttpHost("192.168.200.101",9200,"http"), new HttpHost("192.168.200.102",9200,"http")) ); return client; } }
二、建立索引
/** * 建立索引 * @throws Exception */ public static void createindex()throws Exception{ RestHighLevelClient client = ClientUtil.getClient(); //一、建立索引,設置索引名 CreateIndexRequest request = new CreateIndexRequest("mess"); //二、設置索引的settings request.settings(Settings.builder() .put("index.number_of_shards",3)//分片數 .put("index.number_of_replicas",1)//副本數 .put("analysis.analyzer.default.tokenizer","ik_smart"))//默認分詞器 ; //三、設置索引的mappings request.mapping("_doc", " {\n" + " \"_doc\": {\n" + " \"properties\": {\n" + " \"message\": {\n" + " \"type\": \"text\"\n" + " }\n" + " }\n" + " }\n" + " }", XContentType.JSON); //四、設置索引的別名 request.alias(new Alias("mmm")); //五、發送方式 // //五、1採用同步方式發送請求 CreateIndexResponse response = client.indices().create(request); // 處理響應 boolean acknowledged = response.isAcknowledged(); boolean shardsAcknowledged = response.isShardsAcknowledged(); System.out.println("acknowledged = " + acknowledged); System.out.println("shardsAcknowledged = " + shardsAcknowledged); client.close(); //五、2採用異步方法發送請求 // ActionListener<CreateIndexResponse> listener = new ActionListener<CreateIndexResponse>() { // @Override // public void onResponse(CreateIndexResponse createIndexResponse) { // //處理響應 // boolean acknowledged = createIndexResponse.isAcknowledged(); // boolean shardsAcknowledged = createIndexResponse.isShardsAcknowledged(); // System.out.println("shardsAcknowledged = " + shardsAcknowledged); // System.out.println("acknowledged = " + acknowledged); // } // @Override // public void onFailure(Exception e) { // System.out.println("建立索引異常 = " + e.getMessage()); // } // }; // client.indices().createAsync(request,listener); // client.close(); }
三、index document
public static void indexDocument() throws IOException { RestHighLevelClient client = ClientUtil.getClient(); //一、建立索引請求 IndexRequest request = new IndexRequest("mess", "_doc"); //二、準備文檔數據 //方式一:直接給JSON串 String jsonString = "{" + "\"user\":\"kimchy\"," + "\"postDate\":\"2013-01-31\"," + "\"message\":\"trying out Elasticsearch\"" + "}"; request.source(jsonString, XContentType.JSON); //方式二:以map對象來表示文檔 // HashMap<Object, Object> jsonMap = new HashMap<>(); // jsonMap.put("user", "kimchy"); // jsonMap.put("postDate", new Date()); // jsonMap.put("message", "trying out Elasticsearch"); // request.source(jsonMap); //方式三:用XContentBuilder來構建文檔 // XContentBuilder builder = XContentFactory.jsonBuilder(); // builder.startObject(); // { // builder.field("user", "kimchy"); // builder.field("postDate", new Date()); // builder.field("message", "trying out Elasticsearch"); // } // builder.endObject(); // request.source(builder); // //方式四:直接用key-value對給出 // request.source("user", "kimchy", // "postDate", new Date(), // "message", "trying out Elasticsearch"); //三、其餘的一些可選設置 /* request.routing("routing"); //設置routing值 request.timeout(TimeValue.timeValueSeconds(1)); //設置主分片等待時長 request.setRefreshPolicy("wait_for"); //設置重刷新策略 request.version(2); //設置版本號 request.opType(DocWriteRequest.OpType.CREATE); //操做類別 */ //四、發送請求-->同步方式 IndexResponse indexResponse = null; try { // 同步方式 indexResponse = client.index(request); } catch(ElasticsearchException e) { // 捕獲,並處理異常 //判斷是否版本衝突、create但文檔已存在衝突 if (e.status() == RestStatus.CONFLICT) { logger.error("衝突了,請在此寫衝突處理邏輯!\n" + e.getDetailedMessage()); } logger.error("索引異常", e); } //處理響應 if(indexResponse != null) { String index = indexResponse.getIndex(); String type = indexResponse.getType(); String id = indexResponse.getId(); long version = indexResponse.getVersion(); if (indexResponse.getResult() == DocWriteResponse.Result.CREATED) { System.out.println("新增文檔成功,處理邏輯代碼寫到這裏。"); } else if (indexResponse.getResult() == DocWriteResponse.Result.UPDATED) { System.out.println("修改文檔成功,處理邏輯代碼寫到這裏。"); } // 分片處理信息 ReplicationResponse.ShardInfo shardInfo = indexResponse.getShardInfo(); if (shardInfo.getTotal() != shardInfo.getSuccessful()) { } // 若是有分片副本失敗,能夠得到失敗緣由信息 if (shardInfo.getFailed() > 0) { for (ReplicationResponse.ShardInfo.Failure failure : shardInfo.getFailures()) { String reason = failure.reason(); System.out.println("副本失敗緣由:" + reason); } } } client.close(); //異步方式 // ActionListener<IndexResponse> listener = new ActionListener<IndexResponse>(){ // @Override // public void onResponse(IndexResponse indexResponse) { // //五、處理響應 // if (indexResponse != null) { // String index = indexResponse.getIndex(); // String type = indexResponse.getType(); // String id = indexResponse.getId(); // long version = indexResponse.getVersion(); // if (indexResponse.getResult() == DocWriteResponse.Result.CREATED) { // System.out.println("新增文檔成功,處理邏輯代碼寫到這裏。"); // } else if (indexResponse.getResult() == DocWriteResponse.Result.UPDATED) { // System.out.println("修改文檔成功,處理邏輯代碼寫到這裏。"); // } // } // } // @Override // public void onFailure(Exception e) { // System.out.println("建立索引異常:" + e.getMessage()); // } // }; // client.indexAsync(request,listener); // client.close(); }
四、get document(獲取文檔數據)
/** * 獲取文檔數據 * @throws IOException */ public static void getDocument() throws IOException { RestHighLevelClient client = ClientUtil.getClient(); //一、建立獲取文檔請求 GetRequest request = new GetRequest("mess","_doc","bcUli2oBM0IXAkB-Yvpr"); //二、可選的設置 // request.routing("routing"); // request.version(2); // request.fetchSourceContext(new FetchSourceContext(false));//是否獲取_source字段 //選擇返回的字段 String[] includes=new String[]{"message","postDate"}; String[] excludes = Strings.EMPTY_ARRAY; FetchSourceContext fetchSourceContext = new FetchSourceContext(true, includes, excludes); request.fetchSourceContext(fetchSourceContext); // 取stored字段 /*request.storedFields("message"); GetResponse getResponse = client.get(request); String message = getResponse.getField("message").getValue();*/ //發送請求 GetResponse getResponse = null; try { // 同步請求 getResponse = client.get(request); System.out.println("getResponse = " + getResponse); } catch (ElasticsearchException e) { if (e.status() == RestStatus.NOT_FOUND) { logger.error("沒有找到該id的文檔" ); } if (e.status() == RestStatus.CONFLICT) { logger.error("獲取時版本衝突了,請在此寫衝突處理邏輯!" ); } logger.error("獲取文檔異常", e); } catch (IOException e) { e.printStackTrace(); } //處理響應 if(getResponse != null) { String index = getResponse.getIndex(); String type = getResponse.getType(); String id = getResponse.getId(); if (getResponse.isExists()) { // 文檔存在 long version = getResponse.getVersion(); String sourceAsString = getResponse.getSourceAsString(); //結果取成 String Map<String, Object> sourceAsMap = getResponse.getSourceAsMap(); // 結果取成Map byte[] sourceAsBytes = getResponse.getSourceAsBytes(); //結果取成字節數組 logger.info("index:" + index + " type:" + type + " id:" + id); logger.info(sourceAsString); } else { logger.error("沒有找到該id的文檔" ); } } client.close();
五、bulk批量索引
import org.apache.log4j.LogManager; import org.apache.log4j.Logger; import org.elasticsearch.action.DocWriteRequest; import org.elasticsearch.action.DocWriteResponse; import org.elasticsearch.action.bulk.BulkItemResponse; import org.elasticsearch.action.bulk.BulkRequest; import org.elasticsearch.action.bulk.BulkResponse; import org.elasticsearch.action.delete.DeleteResponse; import org.elasticsearch.action.index.IndexRequest; import org.elasticsearch.action.index.IndexResponse; import org.elasticsearch.action.update.UpdateResponse; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.common.xcontent.XContentType; import java.io.IOException; /** * @Author: xiaolaotou * @Date: 2019/5/6 */ public class BulkIndex { public static void main(String[] args) throws IOException { RestHighLevelClient client = ClientUtil.getClient(); //一、建立批量操做請求 BulkRequest request = new BulkRequest(); request.add(new IndexRequest("mess","_doc","1") .source(XContentType.JSON,"field","foo")); request.add(new IndexRequest("mess","_doc","2") .source(XContentType.JSON,"field","bar")); request.add(new IndexRequest("mess","_doc","3") .source(XContentType.JSON,"field","baz")); // 二、可選的設置 /* request.timeout("2m"); request.setRefreshPolicy("wait_for"); request.waitForActiveShards(2); */ //發送請求--》同步方式 BulkResponse bulkResponse = client.bulk(request); //處理響應 if(bulkResponse != null) { for (BulkItemResponse bulkItemResponse : bulkResponse) { DocWriteResponse itemResponse = bulkItemResponse.getResponse(); if (bulkItemResponse.getOpType() == DocWriteRequest.OpType.INDEX || bulkItemResponse.getOpType() == DocWriteRequest.OpType.CREATE) { IndexResponse indexResponse = (IndexResponse) itemResponse; //TODO 新增成功的處理 } else if (bulkItemResponse.getOpType() == DocWriteRequest.OpType.UPDATE) { UpdateResponse updateResponse = (UpdateResponse) itemResponse; //TODO 修改爲功的處理 } else if (bulkItemResponse.getOpType() == DocWriteRequest.OpType.DELETE) { DeleteResponse deleteResponse = (DeleteResponse) itemResponse; //TODO 刪除成功的處理 } } } client.close(); } }
六、Search查詢數據
package com.study.es_hrset_client; import java.io.IOException; import java.util.List; import java.util.Map; import java.util.concurrent.TimeUnit; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.elasticsearch.action.search.SearchRequest; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.action.search.ShardSearchFailure; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.SearchHits; import org.elasticsearch.search.builder.SearchSourceBuilder; import org.elasticsearch.search.suggest.Suggest; import org.elasticsearch.search.suggest.term.TermSuggestion; /** * * @author xiaolaotou * @date 2019/5/6 * */ public class SearchDemo { private static Logger logger = LogManager.getRootLogger(); public static void main(String[] args) { try (RestHighLevelClient client = InitDemo.getClient();) { // 一、建立search請求 //SearchRequest searchRequest = new SearchRequest(); SearchRequest searchRequest = new SearchRequest("bank"); searchRequest.types("_doc"); // 二、用SearchSourceBuilder來構造查詢請求體 ,請仔細查看它的方法,構造各類查詢的方法都在這。 SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); //構造QueryBuilder /*QueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("user", "kimchy") .fuzziness(Fuzziness.AUTO) .prefixLength(3) .maxExpansions(10); sourceBuilder.query(matchQueryBuilder);*/ sourceBuilder.query(QueryBuilders.termQuery("age", 24)); sourceBuilder.from(0); sourceBuilder.size(10); sourceBuilder.timeout(new TimeValue(60, TimeUnit.SECONDS)); //是否返回_source字段 //sourceBuilder.fetchSource(false); //設置返回哪些字段 /*String[] includeFields = new String[] {"title", "user", "innerObject.*"}; String[] excludeFields = new String[] {"_type"}; sourceBuilder.fetchSource(includeFields, excludeFields);*/ //指定排序 //sourceBuilder.sort(new ScoreSortBuilder().order(SortOrder.DESC)); //sourceBuilder.sort(new FieldSortBuilder("_uid").order(SortOrder.ASC)); // 設置返回 profile //sourceBuilder.profile(true); //將請求體加入到請求中 searchRequest.source(sourceBuilder); // 可選的設置 //searchRequest.routing("routing"); // 高亮設置 /* HighlightBuilder highlightBuilder = new HighlightBuilder(); HighlightBuilder.Field highlightTitle = new HighlightBuilder.Field("title"); highlightTitle.highlighterType("unified"); highlightBuilder.field(highlightTitle); HighlightBuilder.Field highlightUser = new HighlightBuilder.Field("user"); highlightBuilder.field(highlightUser); sourceBuilder.highlighter(highlightBuilder);*/ //加入聚合 /*TermsAggregationBuilder aggregation = AggregationBuilders.terms("by_company") .field("company.keyword"); aggregation.subAggregation(AggregationBuilders.avg("average_age") .field("age")); sourceBuilder.aggregation(aggregation);*/ //作查詢建議 /*SuggestionBuilder termSuggestionBuilder = SuggestBuilders.termSuggestion("user").text("kmichy"); SuggestBuilder suggestBuilder = new SuggestBuilder(); suggestBuilder.addSuggestion("suggest_user", termSuggestionBuilder); sourceBuilder.suggest(suggestBuilder);*/ //三、發送請求 SearchResponse searchResponse = client.search(searchRequest); //四、處理響應 //搜索結果狀態信息 RestStatus status = searchResponse.status(); TimeValue took = searchResponse.getTook(); Boolean terminatedEarly = searchResponse.isTerminatedEarly(); boolean timedOut = searchResponse.isTimedOut(); //分片搜索狀況 int totalShards = searchResponse.getTotalShards(); int successfulShards = searchResponse.getSuccessfulShards(); int failedShards = searchResponse.getFailedShards(); for (ShardSearchFailure failure : searchResponse.getShardFailures()) { // failures should be handled here } //處理搜索命中文檔結果 SearchHits hits = searchResponse.getHits(); long totalHits = hits.getTotalHits(); float maxScore = hits.getMaxScore(); SearchHit[] searchHits = hits.getHits(); for (SearchHit hit : searchHits) { // do something with the SearchHit String index = hit.getIndex(); String type = hit.getType(); String id = hit.getId(); float score = hit.getScore(); //取_source字段值 String sourceAsString = hit.getSourceAsString(); //取成json串 Map<String, Object> sourceAsMap = hit.getSourceAsMap(); // 取成map對象 //從map中取字段值 /* String documentTitle = (String) sourceAsMap.get("title"); List<Object> users = (List<Object>) sourceAsMap.get("user"); Map<String, Object> innerObject = (Map<String, Object>) sourceAsMap.get("innerObject"); */ logger.info("index:" + index + " type:" + type + " id:" + id); logger.info(sourceAsString); //取高亮結果 /*Map<String, HighlightField> highlightFields = hit.getHighlightFields(); HighlightField highlight = highlightFields.get("title"); Text[] fragments = highlight.fragments(); String fragmentString = fragments[0].string();*/ } // 獲取聚合結果 /* Aggregations aggregations = searchResponse.getAggregations(); Terms byCompanyAggregation = aggregations.get("by_company"); Bucket elasticBucket = byCompanyAggregation.getBucketByKey("Elastic"); Avg averageAge = elasticBucket.getAggregations().get("average_age"); double avg = averageAge.getValue(); */ // 獲取建議結果 /*Suggest suggest = searchResponse.getSuggest(); TermSuggestion termSuggestion = suggest.getSuggestion("suggest_user"); for (TermSuggestion.Entry entry : termSuggestion.getEntries()) { for (TermSuggestion.Entry.Option option : entry) { String suggestText = option.getText().string(); } } */ //異步方式發送獲查詢請求 /* ActionListener<SearchResponse> listener = new ActionListener<SearchResponse>() { @Override public void onResponse(SearchResponse getResponse) { //結果獲取 } @Override public void onFailure(Exception e) { //失敗處理 } }; client.searchAsync(searchRequest, listener); */ } catch (IOException e) { logger.error(e); } } }
七、highlight 高亮
import org.apache.log4j.LogManager; import org.apache.log4j.Logger; import org.elasticsearch.action.search.SearchRequest; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.common.text.Text; import org.elasticsearch.index.query.MatchQueryBuilder; import org.elasticsearch.index.query.QueryBuilder; import org.elasticsearch.index.query.QueryBuilders; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.search.SearchHit; import org.elasticsearch.search.SearchHits; import org.elasticsearch.search.builder.SearchSourceBuilder; import org.elasticsearch.search.fetch.subphase.highlight.HighlightBuilder; import org.elasticsearch.search.fetch.subphase.highlight.HighlightField; import java.io.IOException; import java.util.Map; /** * @Author: xiaolaotou
* @Date: 2019/5/6 */ public class highlight { public static void main(String[] args) throws IOException { RestHighLevelClient client = ClientUtil.getClient(); //一、建立請求 SearchRequest searchRequest = new SearchRequest("mess"); //二、用SearchSourceBuilder來構造查詢請求體 SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); //構造QueryBuilder MatchQueryBuilder matchQueryBuilder = QueryBuilders.matchQuery("user", "kimchy"); sourceBuilder.query(matchQueryBuilder); //高亮設置 HighlightBuilder highlightBuilder = new HighlightBuilder(); //不一樣字段可有不一樣設置,如不一樣標籤 HighlightBuilder.Field highlightTitle = new HighlightBuilder.Field("user"); highlightTitle.preTags("<strong>").postTags("</strong>"); highlightBuilder.field(highlightTitle); sourceBuilder.highlighter(highlightBuilder); searchRequest.source(sourceBuilder); //三、發送請求 SearchResponse searchResponse = client.search(searchRequest); //四、處理響應 if (RestStatus.OK.equals(searchResponse.status())) { //處理搜索命中文檔結果 SearchHits hits = searchResponse.getHits(); SearchHit[] searchHits = hits.getHits(); for (SearchHit hit : searchHits) { //取_source字段值 Map<String, Object> sourceAsMap = hit.getSourceAsMap(); // 取成map對象 //從map中取字段值 /*String title = (String) sourceAsMap.get("title"); String content = (String) sourceAsMap.get("content"); */ System.out.println("sourceAsMap = " + sourceAsMap); //取高亮結果 Map<String, HighlightField> highlightFields = hit.getHighlightFields(); HighlightField highlight = highlightFields.get("user"); if (highlight != null) { Text[] fragments = highlight.fragments(); //多值的字段會有多個值 if (fragments != null) { String fragmentString = fragments[0].string(); System.out.println("user highlight = " + fragmentString); //可用高亮字符串替換上面sourceAsMap中的對應字段返回到上一級調用 //sourceAsMap.put("title", fragmentString); } } } } client.close(); } }
八、查詢建議
import org.elasticsearch.action.search.SearchRequest; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.search.builder.SearchSourceBuilder; import org.elasticsearch.search.suggest.Suggest; import org.elasticsearch.search.suggest.SuggestBuilder; import org.elasticsearch.search.suggest.SuggestBuilders; import org.elasticsearch.search.suggest.SuggestionBuilder; import org.elasticsearch.search.suggest.completion.CompletionSuggestion; import org.elasticsearch.search.suggest.term.TermSuggestion; import org.elasticsearch.search.suggest.term.TermSuggestionBuilder; import java.io.IOException; /** * @Author: xiaolaotou * @Date: 2019/5/6 */ public class SearchSuggestion { public static void termSuggest() throws IOException { RestHighLevelClient client = ClientUtil.getClient(); //一、建立search請求 SearchRequest searchRequest = new SearchRequest("mess"); // 二、用SearchSourceBuilder來構造查詢請求體 SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); //作查詢建議 //詞項建議 TermSuggestionBuilder termSuggestionBuilder = SuggestBuilders.termSuggestion("user").text("kimchy"); SuggestBuilder suggestBuilder = new SuggestBuilder(); suggestBuilder.addSuggestion("user",termSuggestionBuilder); sourceBuilder.suggest(suggestBuilder); //發送請求 SearchResponse searchResponse = client.search(searchRequest); //處理響應 //搜索結果狀態信息 if(RestStatus.OK.equals(searchResponse.status())) { // 獲取建議結果 Suggest suggest = searchResponse.getSuggest(); TermSuggestion termSuggestion = suggest.getSuggestion("suggest_user"); for (TermSuggestion.Entry entry : termSuggestion.getEntries()) { System.out.println("entry = " + entry.getText().string()); for (TermSuggestion.Entry.Option option : entry) { String suggestText = option.getText().string(); System.out.println("suggest option = " + suggestText); } } } } /** * 自動補全,根據用戶的輸入聯想到可能的詞或者短語 */ public static void completionSuggester() throws IOException { RestHighLevelClient client = ClientUtil.getClient(); //一、建立請求 SearchRequest searchRequest = new SearchRequest("mess"); //二、用SearchSourceBuilder來構造查詢請求體 SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); SuggestionBuilder termSuggestionBuilder = SuggestBuilders.completionSuggestion("suggest").prefix("lucene s") .skipDuplicates(true); SuggestBuilder suggestBuilder = new SuggestBuilder(); suggestBuilder.addSuggestion("song-suggest", termSuggestionBuilder); sourceBuilder.suggest(suggestBuilder); searchRequest.source(sourceBuilder); //三、發送請求 SearchResponse searchResponse = client.search(searchRequest); //四、處理響應 //搜索結果狀態信息 if(RestStatus.OK.equals(searchResponse.status())) { // 獲取建議結果 Suggest suggest = searchResponse.getSuggest(); CompletionSuggestion termSuggestion = suggest.getSuggestion("song-suggest"); for (CompletionSuggestion.Entry entry : termSuggestion.getEntries()) { System.out.println("text: " + entry.getText().string()); for (CompletionSuggestion.Entry.Option option : entry) { String suggestText = option.getText().string(); System.out.println("suggest option = " + suggestText); } } } } public static void main(String[] args) throws IOException { termSuggest(); // completionSuggester(); } }
九、aggregation 聚合分析
import java.io.IOException; import java.util.concurrent.ExecutionException; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; import org.elasticsearch.action.search.SearchRequest; import org.elasticsearch.action.search.SearchResponse; import org.elasticsearch.client.RestHighLevelClient; import org.elasticsearch.rest.RestStatus; import org.elasticsearch.search.aggregations.AggregationBuilders; import org.elasticsearch.search.aggregations.Aggregations; import org.elasticsearch.search.aggregations.BucketOrder; import org.elasticsearch.search.aggregations.bucket.terms.Terms; import org.elasticsearch.search.aggregations.bucket.terms.Terms.Bucket; import org.elasticsearch.search.aggregations.bucket.terms.TermsAggregationBuilder; import org.elasticsearch.search.aggregations.metrics.avg.Avg; import org.elasticsearch.search.builder.SearchSourceBuilder; public class AggregationDemo { private static Logger logger = LogManager.getRootLogger(); public static void main(String[] args) { try (RestHighLevelClient client = ClientUtil.getClient();) { // 一、建立search請求 //SearchRequest searchRequest = new SearchRequest(); SearchRequest searchRequest = new SearchRequest("bank"); // 二、用SearchSourceBuilder來構造查詢請求體 ,請仔細查看它的方法,構造各類查詢的方法都在這。 SearchSourceBuilder sourceBuilder = new SearchSourceBuilder(); sourceBuilder.size(0); //加入聚合 //字段值項分組聚合 TermsAggregationBuilder aggregation = AggregationBuilders.terms("by_age") .field("age").order(BucketOrder.aggregation("average_balance", true)); //計算每組的平均balance指標 aggregation.subAggregation(AggregationBuilders.avg("average_balance") .field("balance")); sourceBuilder.aggregation(aggregation); searchRequest.source(sourceBuilder); //三、發送請求 SearchResponse searchResponse = client.search(searchRequest); //四、處理響應 //搜索結果狀態信息 if(RestStatus.OK.equals(searchResponse.status())) { // 獲取聚合結果 Aggregations aggregations = searchResponse.getAggregations(); Terms byAgeAggregation = aggregations.get("by_age"); logger.info("aggregation by_age 結果"); logger.info("docCountError: " + byAgeAggregation.getDocCountError()); logger.info("sumOfOtherDocCounts: " + byAgeAggregation.getSumOfOtherDocCounts()); logger.info("------------------------------------"); for(Bucket buck : byAgeAggregation.getBuckets()) { logger.info("key: " + buck.getKeyAsNumber()); logger.info("docCount: " + buck.getDocCount()); //logger.info("docCountError: " + buck.getDocCountError()); //取子聚合 Avg averageBalance = buck.getAggregations().get("average_balance"); logger.info("average_balance: " + averageBalance.getValue()); logger.info("------------------------------------"); } //直接用key 來去分組 /*Bucket elasticBucket = byCompanyAggregation.getBucketByKey("24"); Avg averageAge = elasticBucket.getAggregations().get("average_age"); double avg = averageAge.getValue();*/ } } catch (Exception e) { logger.error(e); } } }