简体   繁体   中英

Get Elasticsearch result from a NEST C# nested aggregation

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?

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}");
             }
         }  

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
          });
     }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM