Dynamic Sub Aggregations using Nest

I want to dynamically create sub aggregations on Nest search query from a List or Dictionary. The below query with pre-defined terms is working fine. But i want to form the query on run time from a list or dictionary.

client.Search(s => s
.Index("masterdata")
.Type("masterdatatype")
.Aggregations(agr => agr.Terms("Date", tr => tr.Field("MonthandYear").Size(int.MaxValue).Order(TermsOrder.TermAscending)
.Aggregations(a => a.Terms("DimensionValue", te => te.Field(dimensionName ).Size(int.MaxValue)
.Aggregations(ag => ag.Sum("SumofMeasure", t => t.Field(measureName)))))))
.Query(a => a.Terms(t => t.Field(filterName).Terms(filterValues)))
);

Please format your code using the </> button in the editor toolbar; it will make your question readable.

  • What does the List or Dictionary look like?
  • What kinds of sub-aggregations do you need to compose?
  • Are these sub-aggregations of an aggregation defined on the search request, or are they sibling aggregations?

If you can provide more detail, I may be able to help :smile:

Hi,
Below is the working code which was written on compile time itself. In the
below example the date would be first aggregated by field MonthandYear and
then followed by sub aggregations on two other fields dimensionname and
measurename.

Now consider i have to write this query on run time. I will get the three
(may be more or less) fields in a list or string[] or querystring and i
have to form the same aggregation query on run time.I used
termsaggregationcontainer object to build my aggregation but i am not able
to append aggregations to existing fields on a loop.

Please let me know if i am still unclear. I will try to write some sample
code that i tried earlier.

client.Search<object>(s => s
     .Index("masterdata")
     .Type("masterdatatype")
     .Aggregations(agr => agr
         .Terms("Date", tr => tr
             .Field("MonthandYear")
             .Size(int.MaxValue)
             .Order(TermsOrder.TermAscending)
             .Aggregations(a => a
                 .Terms("DimensionValue", te => te
                     .Field(dimensionName)
                     .Size(int.MaxValue)
                     .Aggregations(ag => ag
                         .Sum("SumofMeasure", t => t
                             .Field(measureName)
                         )
                     )
                 )
             )
         )
     )
     .Query(a => a
         .Terms(t => t
             .Field(filterName)
             .Terms(filterValues)
         )
     )
);
1 Like

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.