How to return only aggregation statistics in an ElasticSearch query?

Is it possible to exclude documents from the aggregation request? I just need to know "count" and "sum" and not need hits. I did it like this:

{ "query": { "match_all": { } }, "aggs": { "my_agg": { "stats": { "field": "country_id" } } } } 
+5
source share
2 answers

Add to your query ?search_type=count . For instance:

 GET /my_index/countries/_search?search_type=count { "query": { "match_all": { } }, "aggs": { "my_agg": { "stats": { "field": "country_id" } } } } 
+11
source

To focus only on aggregation with the match_all query, you can simply use "size": 0 (this means you don't want the query results) without a query:

 curl -XPOST "http://localhost:9200/indexname/doctype/_search" -d' { "size": 0, "aggs": { "my_agg": { "stats": { "field": "country_id" } } } }' 
+7
source

Source: https://habr.com/ru/post/1203683/


All Articles