我想根据用户输入动态构建聚合查询(可能是术语,嵌套,过滤器)。 输入是:
{
"search": [
{
"aggr": "terms",
"field": "team",
"filterFields": []
},
{
"aggr": "nested",
"field": "stage_test.sutname",
"filterFields": []
},
{
"aggr": "sum",
"field": "stage_test.stage_run_time"
}
]
}
我希望此列表中的每个对象按顺序
作为子聚合我正在解析此列表并在for循环中构建查询:
for (SearchData data : searchInput.getSearch()) {
System.out.println(data.getField());
System.out.println(data.getAggr());
if (termsBuilder != null) {
if(data.getAggr().equalsIgnoreCase("sum")){
termsBuilder = termsBuilder.subAggregation(AggregationBuilders.sum("sum").field(data.getField()));
}
else if(data.getAggr().equalsIgnoreCase("nested")){
termsBuilder = termsBuilder.subAggregation(AggregationBuilders.nested(data.getAggr()).path(data.getField())
.subAggregation(AggregationBuilders.terms(data.getField()).field(data.getField())));
System.out.println(XContentHelper.toString(termsBuilder));
}
}
else {
if(data.getAggr().equalsIgnoreCase("terms"))
{
if(data.getFilterFields().size() == 0){
termsBuilder = AggregationBuilders.terms(data.getField()).field(data.getField())
.size(0);
}
else{
String includeinput[] = new String[data.getFilterFields().size()];
includeinput = data.getFilterFields().toArray(includeinput);
termsBuilder = AggregationBuilders.terms(data.getField()).field(data.getField())
.include(includeinput).size(0);
}
}
}
}
我尝试打印已构建的查询:
{
"team" : {
"terms" : {
"field" : "team",
"size" : 0
},
"aggregations" : {
"nested" : {
"nested" : {
"path" : "stage_test.sutname"
},
"aggregations" : {
"stage_test.sutname" : {
"terms" : {
"field" : "stage_test.sutname"
}
}
}
},
"sum" : {
"sum" : {
"field" : "stage_test.stage_run_time"
}
}
}
}
}
在查询中最后一部分(即总和)我希望它作为子聚合但它来自聚合