0

How to create dynamic nested aggregations query in Elastic search.net based on multiple fields.

These are my fields.

  1. keyword 1
  2. keyword 2
  3. 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.

2
  • So whats the actual problem? Commented Jan 31, 2019 at 4:52
  • @MichaelRandall, 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. Commented Jan 31, 2019 at 4:56

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.