priyanka.sarkar
priyanka.sarkar

Reputation: 26538

How to make aggregations on field basis in NEST and Elastic Search?

I would like to perform aggregation in Elastic Search using NEST which would mimic the below SQL query:

select region, bucket, count(dispositioncode)
from tbl
group by region, bucket

My attempt so far

var dispositionCodes = new TermsQuery
            {
                IsVerbatim = true,
                Field = "trailstatus",
                Terms = new string[] { "TR" }
            };

ISearchResponse<TrailReportModel> searchResponse =
               ConnectionToES.EsClient()
               .Search<TrailReportModel>
               (s => s
                   .Index(feedbackIndex)
                   .From(0)
                   .Size(RecordSize)
                     .Query(q => q.MatchAll()
                             
                                .Aggregations(fa => fa

                              .Filter("disp_aggs", f => f.Filter(fd => dispositionCodes))                              
                      )
                      
var result = ((Nest.SingleBucketAggregate)searchResponse.Aggregations["disp_aggs"]).DocCount;

This gives me the count for the entire records irrespective of Region and Bucket. But I am looking for the Count per Region and Bucket basis.

How can I achieve this?

Upvotes: 2

Views: 873

Answers (1)

nunohpinheiro
nunohpinheiro

Reputation: 2269

It is a bit difficult without a case to test, but you may want to use the Terms aggregation (in NEST).

With this, you could specify something like:

.Aggregations(a => a
     .Terms("group_by_region", t => t.Field("region"))
     .Terms("group_by_bucket", t => t.Field("bucket")))

This answer may also help, it is a similar problem :)

Upvotes: 4

Related Questions