I want to build aggregation query dynamically based on the user input(may be terms, nested, filters).
input is:
{
"search": [{
"aggr": "terms",
"field": "team",
"filterFields": []
},
{
"aggr": "nested",
"field": "stage_test.sutname",
"filterFields": []
},
{
"aggr": "sum",
"field": "stage_test.stage_run_time"
}
]
}
I want each objects in this list as a subaggregation in order
I am Parsing this list and inside a for loop am building the query:
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);
}
}
}
}
I tried to print the query builded:
{
"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"
}
}
}
}
}
As in query the last part (i.e.. sum) I wanted that as subaggregation but it came out of aggregation
Any help would be appreciated
Thanks