Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elasticsearch - Find documents missing two fields

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"
         }
      }
   }
}
like image 670
alp123 Avatar asked Jul 13 '26 17:07

alp123


1 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"
        }
      }
    ]
  }
}
like image 145
Val Avatar answered Jul 17 '26 19:07

Val



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!