1,hotkey 索引结构
{
"properties": {
"id": {
"type": "keyword"
},
"searchInput": {
"type": "keyword"
},
"createDate": {
"type": "keyword"
},
"searchType": {
"type": "keyword"
}
}
}
2 TopHitsAggregationBuilder
//设置要查询的索引
SearchRequest request = new SearchRequest().indices("hotkey");
//构建搜索
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
//添加搜索长度
sourceBuilder.size(0);
//添加搜索条件
sourceBuilder.query(new BoolQueryBuilder().must(QueryBuilders.termQuery("searchType", searchDto.getSourceType())));
//设置要聚合的字段以及条数
//设置该次聚合的名称 terms(args1)
// 需要返回字段的集合
String[] param = {"searchInput"};
// 对需要返回的数据包括哪些,不包括哪些,重复的只返回1条
TopHitsAggregationBuilder top1 = AggregationBuilders.topHits("top").fetchSource(param, Strings.EMPTY_ARRAY).size(1);
// 通过searchInput聚合并且聚合后返回10条数据,注意这里的size(这里代表聚合查询出多少条数据,注意这里的size要比最下面分页的size要大,因为是对聚合后的数据分页,如果不写的话默认是10)
TermsAggregationBuilder agg1 = AggregationBuilders.terms("searchInput").field("searchInput").subAggregation(top1).size(30);
//以及要聚合的字段field(args1 + ".keyword") 添加keyword是对字段进行不分词查询。
agg1.subAggregation(new BucketSortPipelineAggregationBuilder("bucket_field",null).
from((searchDto.getPageIndex() - 1) * searchDto.getPageSize()).size(searchDto.getPageSize()));
sourceBuilder.aggregation(agg1);
request.source(sourceBuilder);
SearchResponse response = null;
try {
response = elasticsearchTemplate.search(request);
} catch (Exception e) {
log.error("==========search hotkey error====", e);
}
Aggregations aggregations = response.getAggregations();
List<String> list = new ArrayList<>();
Terms terms1 = aggregations.get("searchInput");
3,kibana 查询文章来源:https://www.toymoban.com/news/detail-739120.html
{"size":0,"query":{"bool":{"must":[{"term":{"searchType":{"value":"app","boost":1.0}}}],"adjust_pure_negative":true,"boost":1.0}},"aggregations":{"searchInput":{"terms":{"field":"searchInput","size":30,"min_doc_count":1,"shard_min_doc_count":0,"show_term_doc_count_error":false,"order":[{"_count":"desc"},{"_key":"asc"}]},"aggregations":{"top":{"top_hits":{"from":0,"size":1,"version":false,"seq_no_primary_term":false,"explain":false,"_source":{"includes":["searchInput"],"excludes":[]}}},"bucket_field":{"bucket_sort":{"sort":[],"from":0,"size":10,"gap_policy":"SKIP"}}}}}}
4,返回数据文章来源地址https://www.toymoban.com/news/detail-739120.html
{"took":5,"timed_out":false,"_shards":{"total":3,"successful":3,"skipped":0,"failed":0},"hits":{"total":{"value":16,"relation":"eq"},"max_score":null,"hits":[]},"aggregations":{"searchInput":{"doc_count_error_upper_bound":0,"sum_other_doc_count":0,"buckets":[{"key":"ABCDE","doc_count":2,"top":{"hits":{"total":{"value":2,"relation":"eq"},"max_score":5.13318,"hits":[{"_index":"hotkey","_type":"_doc","_id":"d0b63727fc0743318534a4b190f68b53","_score":5.13318,"_source":{"searchInput":"ABCDE"}}]}}},{"key":"东临碣石","doc_count":1,"top":{"hits":{"total":{"value":1,"relation":"eq"},"max_score":4.5587773,"hits":[{"_index":"hotkey","_type":"_doc","_id":"2ad888de2d164a16b314b15904aaa6b7","_score":4.5587773,"_source":{"searchInput":"东临碣石"}}]}}},{"key":"以观沧海","doc_count":1,"top":{"hits":{"total":{"value":1,"relation":"eq"},"max_score":4.80253,"hits":[{"_index":"hotkey","_type":"_doc","_id":"74a8ed19c75c4cdc89cd6827a6504fda","_score":4.80253,"_source":{"searchInput":"以观沧海"}}]}}},{"key":"何时在人间","doc_count":1,"top":{"hits":{"total":{"value":1,"relation":"eq"},"max_score":4.5587773,"hits":[{"_index":"hotkey","_type":"_doc","_id":"438b4fdde48d40cda6e3563d9bcc62bd","_score":4.5587773,"_source":{"searchInput":"何时在人间"}}]}}},{"key":"创新医药实际","doc_count":1,"top":{"hits":{"total":{"value":1,"relation":"eq"},"max_score":5.13318,"hits":[{"_index":"hotkey","_type":"_doc","_id":"cb51799e599f46c4b2ea649fe6b11f20","_score":5.13318,"_source":{"searchInput":"创新医药实际"}}]}}},{"key":"天涯共此时","doc_count":1,"top":{"hits":{"total":{"value":1,"relation":"eq"},"max_score":4.5587773,"hits":[{"_index":"hotkey","_type":"_doc","_id":"facf5752fc284c0a9a58d157cc28c091","_score":4.5587773,"_source":{"searchInput":"天涯共此时"}}]}}},{"key":"山岛竦是","doc_count":1,"top":{"hits":{"total":{"value":1,"relation":"eq"},"max_score":5.13318,"hits":[{"_index":"hotkey","_type":"_doc","_id":"c14cc6f367934d43b9b0fcb7095b085c","_score":5.13318,"_source":{"searchInput":"山岛竦是"}}]}}},{"key":"我欲乘风归去","doc_count":1,"top":{"hits":{"total":{"value":1,"relation":"eq"},"max_score":4.80253,"hits":[{"_index":"hotkey","_type":"_doc","_id":"242358ccf1684f178d143e2cf64ad8f0","_score":4.80253,"_source":{"searchInput":"我欲乘风归去"}}]}}},{"key":"有空请楼宇与","doc_count":1,"top":{"hits":{"total":{"value":1,"relation":"eq"},"max_score":4.5587773,"hits":[{"_index":"hotkey","_type":"_doc","_id":"1f1ce4ce78814e5e99674ccfe214c87a","_score":4.5587773,"_source":{"searchInput":"有空请楼宇与"}}]}}},{"key":"水和丹丹","doc_count":1,"top":{"hits":{"total":{"value":1,"relation":"eq"},"max_score":4.5587773,"hits":[{"_index":"hotkey","_type":"_doc","_id":"5262a7dd87b14ec280fc684237dfdcad","_score":4.5587773,"_source":{"searchInput":"水和丹丹"}}]}}},{"key":"海上升明月","doc_count":1,"top":{"hits":{"total":{"value":1,"relation":"eq"},"max_score":4.80253,"hits":[{"_index":"hotkey","_type":"_doc","_id":"0b93f86d8223446db3f98db7c9a8efcb","_score":4.80253,"_source":{"searchInput":"海上升明月"}}]}}},{"key":"由于竟是","doc_count":1,"top":{"hits":{"total":{"value":1,"relation":"eq"},"max_score":5.13318,"hits":[{"_index":"hotkey","_type":"_doc","_id":"60bc393b0fae4a84b72ff311aab5e3dd","_score":5.13318,"_source":{"searchInput":"由于竟是"}}]}}},{"key":"神龟虽寿","doc_count":1,"top":{"hits":{"total":{"value":1,"relation":"eq"},"max_score":4.80253,"hits":[{"_index":"hotkey","_type":"_doc","_id":"4d389e8ec7da45c897bc961030990e66","_score":4.80253,"_source":{"searchInput":"神龟虽寿"}}]}}},{"key":"起舞弄清影","doc_count":1,"top":{"hits":{"total":{"value":1,"relation":"eq"},"max_score":4.80253,"hits":[{"_index":"hotkey","_type":"_doc","_id":"b7535c2102104a94a1e5ee457e955b95","_score":4.80253,"_source":{"searchInput":"起舞弄清影"}}]}}},{"key":"高处不胜寒","doc_count":1,"top":{"hits":{"total":{"value":1,"relation":"eq"},"max_score":4.5587773,"hits":[{"_index":"hotkey","_type":"_doc","_id":"a77bd175cfc64d1b8af7d6597c57530b","_score":4.5587773,"_source":{"searchInput":"高处不胜寒"}}]}}}]}}}
到了这里,关于es 聚合分页的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!