当前位置:网站首页>A general es aggregation query method
A general es aggregation query method
2022-06-10 03:21:00 【Yan children's shoes】
/**
* A generic es Aggregate query method
* @param restHighLevelClient
* @param start
* @param end
* @param index
* @param aggregationName
* @param painlessScript
* @param existsQueryParams
* @param notExistsQueryParams
* @return
* @throws IOException
*/
private List<String> queryFromEs(RestHighLevelClient restHighLevelClient, String start, String end, String[] index, String aggregationName, String painlessScript, List<String> existsQueryParams, List<String> notExistsQueryParams) throws IOException {
List<String> resultList = new ArrayList<>();
// Create a search source
SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
//SearchRequest Query by one or more indexes , Need one SearchSourceBuilder, Search sources provide search options
SearchRequest searchRequest = new SearchRequest();
// Conditions for aggregation
TermsAggregationBuilder aggregation = AggregationBuilders.terms(aggregationName).script(new Script(painlessScript)).size(Integer.MAX_VALUE).minDocCount(1).shardMinDocCount(0).showTermDocCountError(false);
// add to bool filter , Query conditions
BoolQueryBuilder boolQueryBuilder = QueryBuilders.boolQuery();
//must -- Time must meet
boolQueryBuilder.must(QueryBuilders.rangeQuery("TIME").gte(start).lte(end));
// The conditions of existence
for (String existsQueryParam : existsQueryParams) {
boolQueryBuilder.must(QueryBuilders.existsQuery(existsQueryParam));
}
// Nonexistent condition
for (String notExistsQueryParam : notExistsQueryParams) {
boolQueryBuilder.mustNot(QueryBuilders.existsQuery(notExistsQueryParam));
}
// Definition sourceBuilder Sort by time , positive sequence , Then pass in the previous query criteria ,from 0 size 0 Do not check the original data
sourceBuilder.sort("TIME", SortOrder.ASC).from(0).size(0).query(boolQueryBuilder).aggregation(aggregation);
// Define the index of the query , Define search sources , namely sourceBuilder object
searchRequest.indices(index);
searchRequest.source(sourceBuilder);
log.info("-query from es index:{}, sql:{}-->", index, sourceBuilder);
// Begin your search , Get the results
SearchResponse response = null;
response = restHighLevelClient.search(searchRequest, RequestOptions.DEFAULT);
// Get the aggregated data
Aggregations aggregations = response.getAggregations();
if (aggregations != null) {
ParsedStringTerms empAccountAggregationsTerms = aggregations.get(aggregationName);
List<? extends Terms.Bucket> buckets = empAccountAggregationsTerms.getBuckets();
for (Terms.Bucket bucket : buckets) {
log.info("--query from es index results:-->" + bucket.getKey().toString());
resultList.add(bucket.getKey().toString());
}
}
return resultList;
}边栏推荐
猜你喜欢

Idea start multiple services with the same set of code

17 orthogonal matrix and gram Schmidt orthogonalization

The playback capacity of a single video is more than 8000w, so hard core cooking is so superior

Wang Xing, Zhang Yong, Xu Lei, who can win the local e-commerce?

Will free price increases force young people back?

IDE problem (I) wechat developer tool cannot be opened

protobuf 基本介绍安装和使用

Shift+ right mouse button No: Open command window here

Refactoring --inline

Vscade C language code ctrl+ left key cannot jump to definition
随机推荐
The new account started to attract nearly 2million fans, and the "old age" account can also become a dark horse for promotion
When the most successful and worst CEO dies, Sony still follows his old path
Tensor programming
重构--Rename
小功能实现(三)字符串分割中括号中的内容
Implementation of small functions (III) string segmentation contents in brackets
[mui+flask+mongodb+hbuilderx] detailed explanation of the answer integration logic of app development
剑指 Offer 24. 反转链表
Tidb experience sharing 01
清晨开播近100万人观看,快手知识类主播化身“妇女之友”?
leetcode 19. 删除链表的倒数第 N 个结点
IDE problem (I) wechat developer tool cannot be opened
Wang Xing, Zhang Yong, Xu Lei, who can win the local e-commerce?
Esp32 intrinsic function / variable cannot jump to definition
P1082 [NOIP2012 提高组] 同余方程
Ptrtostructure error prompt: this structure must not be a value class. Solution
剑指 Offer 09. 用两个栈实现队列
Determinant and its properties
Anaconda modify file save path
PtrToStructure 错误提示:此结构不得为值类,解决办法