Elasticsearch Nest dynamic aggregation

Viewed 3160

Im trying to run aggregation query in c# (using nest 5) but I dont know how many aggregations I get as input and what are the aggregation type.

For example one query is: {"aggs":{"type_count":{"terms":{"field":"type"}}}}

And other query will be: {"aggs":{"type_count":{"terms":{"field":"type"}},"salary_count": {"field":"salary"}}}

And other query may not contain aggregation at all.

How can I write this code dynamic in c#?

This is what I tried (I have case for selected aggregation types. The problem is that this code support only one aggregation.

SearchDescriptor<object> SearchAgg = new SearchDescriptor<object>();
for (i=0;i < aggList.length;i++)
{
    SearchAgg.Aggregations(a => a.terms (aggList[i]), t=> t.Field(aggList[i]));
}

Edit:

I successed to add multiple aggregations using this code:

AggregationContainerDescriptor<SearchRequest> agg = new
AggregationContainerDescriptor<SearchRequest>();

agg.Terms("bucket", tm=> tm.Field("field"));
agg &= new AggregationContainerDescriptor<SearchRequest>().Terms("bucket2", tm=> tm.Field("field2"));

Thanks

1 Answers
Related