8

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

{
  "query": {
    "match_all": {

    }
  },
  "aggs": {
    "my_agg": {
      "stats": {
        "field": "country_id"
      }
    }
  }
}
1

2 Answers 2

14

To focus only on aggregation with a match_all query, you could simply use "size":0 (this specifies you want no query results) with no query:

curl -XPOST "http://localhost:9200/indexname/doctype/_search" -d'
{
    "size": 0, 
    "aggs": {
        "my_agg": {
            "stats": {
                "field": "country_id"
            }
        }
    }
}'
Sign up to request clarification or add additional context in comments.

1 Comment

Actually, Andrei Stefan's answer is better (source : elastic.co/guide/en/elasticsearch/reference/1.x/…). However, no need to add the match_all query.
11

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

GET /my_index/countries/_search?search_type=count
{
"query": {
    "match_all": {

    }
  },
  "aggs": {
    "my_agg": {
      "stats": {
        "field": "country_id"
      }
    }
  }
}

1 Comment

"Deprecated in 2.0.0-beta1: count does not provide any benefits over query_then_fetch with a size of 0." link

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.