简体   繁体   中英

ElasticSearch - Sum of Types in Nest Query

I'm using Nest to do an "_all" query on an ElasticSearchIndex. I'm specifying three types:

                s.Types(typeof(TypeA), typeof(TypeB), typeof(TypeC));
                s.Query(q => q.QueryString(qs => qs
                   .DefaultField("_all")
                   .Query(criteria.SearchText)));

I'm trying to use Aggregations to get the totals found for each type. I've tried using the Nest code like this:

s.Aggregations(a => a.Sum("typeA", b => b.Field("Type")));

But it has not worked. Does anyone know how to accomplish this with Nest?

The sum aggregation is actually more for summing the values of field contents within your document. In your case since you just want a count of the documents, I would suggest you use a terms aggregation on the _type field. So use the following should get what you are looking for.

 s.Aggregations(a => a.Terms("types", b=>b.Field("_type")));

Hope this helps...

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