# Building dynamic Aggregation query based on user input

**URL:** <https://discuss.elastic.co/t/building-dynamic-aggregation-query-based-on-user-input/60595>\
**Category:** Elasticsearch\
**Created:** [September 15, 2016, 10:27am UTC](https://discuss.elastic.co/t/building-dynamic-aggregation-query-based-on-user-input/60595 "2016-09-15T10:27:06Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bhat](https://avatars.discourse-cdn.com/v4/letter/b/9dc877/32.png) [@Bhat](https://discuss.elastic.co/u/Bhat)\
**Post date:** [September 15, 2016, 10:27am UTC](https://discuss.elastic.co/t/building-dynamic-aggregation-query-based-on-user-input/60595/1 "2016-09-15T10:27:06Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![Bhat](https://avatars.discourse-cdn.com/v4/letter/b/9dc877/32.png) [@Bhat](https://discuss.elastic.co/u/Bhat)\
**Post date:** [September 16, 2016, 9:52am UTC](https://discuss.elastic.co/t/building-dynamic-aggregation-query-based-on-user-input/60595/2 "2016-09-16T09:52:58Z")

</div>

Could anyone help me out with this

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [July 5, 2017, 10:19pm UTC](https://discuss.elastic.co/t/building-dynamic-aggregation-query-based-on-user-input/60595/3 "2017-07-05T22:19:45Z")

</div>


