Elasticsearch : aggregation "existing" fields

litil picture litil · Oct 31, 2014 · Viewed 8.5k times · Source

I'm quite new to ElasticSearch aggregations. I want to be able to count how many documents are retrieved with a not null field.

Here's what I do to count how many documents don't have a name field.

{
  "size": 3,
  "query": {
    "query_string": {
      "query": "martin"
    }
  },
  "aggs": {
    "results_without_mb_id": {
      "missing": {
        "field": "name"
      }
    }
  }
}

It works but I want to do the exact opposite. Is there an existing aggregation?

Answer

emaxi picture emaxi · Oct 12, 2015

Like above, just replace 'missing' with 'exists', and also add 'filter' key, so:

{ "size": 3, 
  "query": {
    "query_string": {
      "query" : "martin"
    } 
  }, 
  "aggs": {
    "results_without_mb_id": { 
       "filter": { 
          "exists": { 
            "field": "name" 
          }  
       } 
    } 
}