Count the number of buckets in elasticsearch brackets

I use elasticsearch to search a database with lots of duplicates. I use field collapse and it works, however it returns the number of hits (including duplicates), not the number of buckets.

"aggs": { "uniques": { "terms": { "field": "guid" }, "aggs": { "jobs": { "top_hits": { "_source": "title", "size": 1 }} } } } 

I can count the buckets by running another query using capacity (but it only returns the bill, not the documents):

 { "aggs" : { "uniques" : { "cardinality" : { "field" : "guid" } } } } 

Is there a way to return both queries (buckets + total number of buckets) in one search?

thanks

+7
elasticsearch
source share
1 answer

You can combine both of these aggregations into one query.

 { "aggs" : { "uniques" : { "cardinality" : { "field" : "guid" } }, "uniquesTerms": { "terms": { "field": "guid" }, "aggs": { "jobs": { "top_hits": { "_source": "title", "size": 1 }} } } } 
+11
source share

All Articles