How to create dynamic nested aggregations query in Elastic search.net based on multiple fields.
These are my fields.
- keyword 1
- keyword 2
- keyword 3
This is my static query.
ISearchResponse<T> response = client.Search<T>
(
s => s
.Index(indexName)
.Type(docType)
.Size(0)
.RequestConfiguration(r => r.DisableDirectStreaming())
.Aggregations
(
a => a.Terms("result_by_keyword_1", t => t.Field("keyword 1").Missing("N/A").Size(10000)
.Aggregations
(
ag => ag.Terms("result_by_keyword_2", tg => tg.Field("keyword 2").Missing("N/A").Size(10000)
.Aggregations
(
agf => agf.Terms("result_by_keyword_3", tfg => tfg.Field("keyword 3").Missing("N/A").Size(10000))
))
))
)
);
I need to access data like this:
foreach (var bucket in response.Aggs.Terms("result_by_keyword_1").Buckets)
{
foreach (var innerBucket in bucket.Terms("result_by_keyword_2").Buckets)
{
foreach (var innerBucket1 in innerBucket.Terms("result_by_keyword_3").Buckets)
{
AggregateData aggData = new AggregateData();
aggData.keyword1= bucket.Key;
aggData.keyword2= innerBucket.Key;
aggData.keyword3= innerBucket1.Key;
aggData.recordcount = innerBucket1.DocCount;
results.Add(aggData);
}
}
}
Problems are : Actually I want to create a dynamic query based on multiple fields. here I have 3 fields but it may more than 3 fields.so based on fields count I want to nested aggeration in elastic search in .net.
Thanks in advance.