1

I have this Elasticsearch NEST query:

var res = elastic.Search<SegmentRecord>(s => s.Index(esIndex).Aggregations(a => a.Terms("agg", x => x.Field(o => o.InstrumentName).Aggregations(a1 => a1.Terms("agg2", f => f.Field(y => y.GroupId))))));

how can I cycle through all the InstrumentName fields, and for each of those, cycle through all the GroupId fields?

2 Answers 2

5

On Nest 5.4.0

foreach (var bucket in res.Aggs.Terms("agg").Buckets)
         {
             foreach (var innerBucket in bucket.Terms("agg2").Buckets)
             {
                 System.Console.WriteLine($"agg:{bucket.Key}, agg2:{innerBucket.Key} - {innerBucket.DocCount}");
             }
         }  
Sign up to request clarification or add additional context in comments.

2 Comments

have hit this a while ago and found a great article on the matter
0

This is how I accessed my children buckets with nested aggregations.

var yourAgg = result.Aggregations.Terms("YourParentField");
foreach (var child in yourAgg .Buckets)
{
   var aggs = child.Terms("YourChildField").Buckets;
   foreach (var item in aggs)
   {
      perDealerAggItems.Add(new AggregateItem()
        {
              Count = item.DocCount ?? 0,
              Key = item.Key,
              ParentList = field
          });
     }
}

Comments

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.