0

I have a requirement to perform Aggregation (Count) of records using NEST wrapper but to fire the DSL query inside NEST.

Since I don't know how to construct it properly, I have done the same using LINQ approach.

ISearchResponse<AgencyDetailReportModel> searchResponse = ConnectionToESClient().Search<AgencyDetailReportModel>
                (s => s
                    .Index("accountsdata")


                    .From(0)
                    .Size(15000)
                    .Query(q =>
                            q.MatchAll()

                            )
                    );

var allocatedAgencies = agencySearchResponse.Documents.Where(w => !string.IsNullOrEmpty(w.agencyid)).Count(); 

var unAllocatedAgencies = agencySearchResponse.Documents.Where(w => string.IsNullOrEmpty(w.agencyid)).Count();

How can I construct the DSL query inside NEST?

halfer
  • 19,824
  • 17
  • 99
  • 186
priyanka.sarkar
  • 25,766
  • 43
  • 127
  • 173

1 Answers1

1

So for your question you need allocatedAgencies count and unAllocatedAgencies count right.We can achieve this by simple query rather than going for aggregations.

var searchResponse = await highLevelClient.CountAsync<accountsdata>(s => s
                                   .Index("accountsdata")
                                   .Query(q => q
                                      .ConstantScore(c => c
                                        .Filter(f => f
                                            .Bool(b => b
                                                .MustNot(m => m
                                                      .Exists(e => e.Field("agencyid"))))))));

This is for unAllocatedAgencies count and for allocatedAgencies below is the query.

var searchResponse = await highLevelClient.CountAsync<accountsdata>(s => s
                                   .Index("accountsdata")
                                   .Query(q => q
                                      .ConstantScore(c => c
                                        .Filter(f => f
                                            .Bool(b => b
                                                .Must(m => m
                                                      .Exists(e => e.Field("agencyid"))))))));

Let me know if you face any issues, max it will work for your above mentioned problem. Thanks

  • is it possible to perform the operation in a same query ? Because I have another scenario where I need to perform the count for 6 cases - so in that case do I have to write the query 6 times ? – priyanka.sarkar Apr 19 '20 at 17:46
  • Kartheek, as I said earlier, I have another requirement. Can u please take a look https://stackoverflow.com/questions/61344612/how-to-perform-sub-aggregation-using-nest – priyanka.sarkar Apr 21 '20 at 13:47
  • Kartheek, I have another requirement using filter aggregations (https://stackoverflow.com/questions/65034391/how-to-make-multiple-aggregations-and-put-filter-conditions-using-nest ). Can you please help me on that ? – priyanka.sarkar Nov 27 '20 at 09:20