Elasticsearch - Find documents missing two fields

Viewed 1315

I'm trying to create a query that returns information about how many documents that don't have data for two fields (date.new and date.old). I have tried the query below, but it works as OR-logic, where all documents missing either date.new or date.old are returned. Does anyone know how I can make this only return documents missing both fields?

{
   "aggs":{
      "Missing_field_count1":{
         "missing":{
            "field":"date.new"
         }
      },
      "Missing_field_count2":{
         "missing":{
            "field":"date.old"
         }
      }
   }
}
2 Answers

Aggregations is not the feature to use for this. You need to use the exists query wrapped within a bool/must_not query, like this:

GET index/_count
{
  "size": 0,
  "bool": {
    "must_not": [
      {
        "exists": {
          "field": "date.new"
        }
      },
      {
        "exists": {
          "field": "date.old"
        }
      }
    ]
  }
}

hits.total.value indicates the count of the documents that match the search request. The value indicates the number of hits that match and relation indicates whether the value is accurate (eq) or a lower bound (gte)

Index Data:

{
  "data": {
    "new": 1501,
    "old": 10
  }
}

{
  "title": "elasticsearch"
}

{
  "title": "elasticsearch-query"
}
{
  "date": {
    "new": 1400
  }
}

The search query given by @Val answers on how to achieve your use case.

Search Result:

"hits": {
    "total": {
      "value": 2,                <-- note this
      "relation": "eq"
    },
    "max_score": 0.0,
    "hits": [
      {
        "_index": "65112793",
        "_type": "_doc",
        "_id": "2",
        "_score": 0.0,
        "_source": {
          "title": "elasticsearch"
        }
      },
      {
        "_index": "65112793",
        "_type": "_doc",
        "_id": "5",
        "_score": 0.0,
        "_source": {
          "title": "elasticsearch-query"
        }
      }
    ]
  }
Related