ElasticSearch 之 BucketScriptPipelineAggregationBuilder 代码实现

ElasticSearch 之 BucketScriptPipelineAggregationBuilder 代码实现,第1张

ElasticSearch 之 BucketScriptPipelineAggregationBuilder 代码实现

官网链接:

Bucket script Aggregation | Elasticsearch Guide [6.4] | Elastic

实现功能:

select sum(col01),sum(col02),sum(col03)。。。,sum(col01)/sum(col02) from log  group by col5

Java代码实现分组求比值,求和:

    public void searchAgg(String indexName,String typeName, String query, String groupByFieldName,String sumCols1,String sumCols2) {
        try {
            TermsAggregationBuilder aggBuilder = AggregationBuilders.terms(ELASTIC_SEARCH__AGGREGATION_NAME).field(groupByFieldName).size(10);

            SumAggregationBuilder subSumAgg1 = AggregationBuilders.sum(sumCols1 + ELASTIC_SEARCH__SUB_AGGREGATION_NAME__TAIL).field(sumCols1);
            aggBuilder.subAggregation(subSumAgg1);

            SumAggregationBuilder subSumAgg2 = AggregationBuilders.sum(sumCols2 + ELASTIC_SEARCH__SUB_AGGREGATION_NAME__TAIL).field(sumCols2);
            aggBuilder.subAggregation(subSumAgg2);

            HashMap bucketPathMaps = new HashMap<>();

            String bucketPath01=sumCols1+"_sum";
            String bucketPath02=sumCols2+"_sum";

            bucketPathMaps.put(bucketPath01,sumCols1 + ELASTIC_SEARCH__SUB_AGGREGATION_NAME__TAIL);
            bucketPathMaps.put(bucketPath02,sumCols2 + ELASTIC_SEARCH__SUB_AGGREGATION_NAME__TAIL);

            String script="params."+bucketPath01+"/params."+bucketPath02;
            script inline = new script(script);

            BucketscriptPipelineAggregationBuilder divisionAgg = new BucketscriptPipelineAggregationBuilder("division", bucketPathMaps, inline);
            aggBuilder.subAggregation(divisionAgg);
            SearchSourceBuilder sourceBuilder = new SearchSourceBuilder();
            sourceBuilder.query(QueryBuilders.queryStringQuery(query));
            sourceBuilder.aggregation(aggBuilder);

            //构建查询请求,封装请求体
            SearchRequest searchRequest = new SearchRequest(indexName.split(",")).types(typeName);
            searchRequest.source(sourceBuilder);
            //处理返回的请求
            SearchResponse searchResponse = mClient.search(searchRequest, RequestOptions.DEFAULT);
            Terms terms = searchResponse.getAggregations().get(ELASTIC_SEARCH__AGGREGATION_NAME);
            Iterator iterator = terms.getBuckets().iterator();

            while (iterator.hasNext()) {
                Terms.Bucket bucket = iterator.next();
                Map subAggMap = bucket.getAggregations().asMap();
                Set strings = subAggMap.keySet();
                for (String key:strings) {
                    Aggregation aggregation = subAggMap.get(key);
                    if(aggregation instanceof Sum){
                    double value = ((Sum) subAggMap.get(key)).getValue();
                        System.out.println("sum 分子和分母:"+value);
                    }else{
                        double value = ((ParsedSimplevalue) subAggMap.get(key)).value();
                        System.out.println("比值:"+value);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

上述代码对应的dsl实现:

curl -XGET http://localhost:9200/test/_search?pretty -H 'Content-Type:application/json' --data-binary '
{
    "query":{
        "query_string":{
            "query":"id:cafba57439eb40f1827670ac5dcf37ab"
        }
    },
    "aggregations":{
        "group_by_state":{
            "terms":{
                "field":"lastGroupId"
            },
            "aggregations":{
                "sessionDuration_Agg":{
                    "sum":{
                        "field":"duration"
                    }
                },
                "sessionHumanValid_Agg":{
                    "sum":{
                        "field":"validFlag"
                    }
                },
                "division":{
                    "bucket_script":{
                        "buckets_path":{
                            "sessionDuration_sum":"sessionDuration_Agg",
                            "sessionHumanValid_sum":"sessionHumanValid_Agg"
                        },
                        "script":{
                            "source":"params.sessionDuration_sum/params.sessionHumanValid_sum",
                            "lang":"painless"
                        },
                        "gap_policy":"skip"
                    }
                }
            }
        }
    }
}
'

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5715349.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存