53

What is the best way to check if a field of a document in elasticsearch exists? I can't find anything in the documentation.

For example if this document doesn't have the field/key "price" I don't want to return in the result.

{
    "updated": "2015/09/17 11:27:27",
     "name": "Eye Shadow",
     "format": "1.5 g / 0.05 oz",
}

What I can do?

Yu Hao
  • 119,891
  • 44
  • 235
  • 294
Ekaitz Hernandez Troyas
  • 1,147
  • 2
  • 11
  • 18

5 Answers5

74

You can use the exists filter combined with a bool/must filter like this:

{
  "query": {
    "filtered": {
      "filter": {
        "bool": {
          "must": [
            {
              "exists": {
                "field": "price"
              }
            },
            ...     <-- your other constraints, if any
          ]
        }
      }
    }
  }
}

DEPRECATED (since ES5) You can also use the missing filter combined with a bool/must_not filter:

{
  "query": {
    "filtered": {
      "filter": {
        "bool": {
          "must_not": [
            {
              "missing": {
                "field": "price"
              }
            }
          ]
        }
      }
    }
  }
}
Val
  • 207,596
  • 13
  • 358
  • 360
27

The exists filter has been replaced by exists query from ES 2.1, though the working of it is the same. Also, the missing filter is removed and missing query deprecated.

To get all docs which have a particular field,

"bool": {
    "must": {
        "exists": {
            "field": "my_field"
        }
    }
}

and to get all docs which does NOT have a particular field, use it with must_not like this

"bool": {
    "must_not": {
        "exists": {
            "field": "my_field"
        }
    }
}

Elastic docs: https://www.elastic.co/guide/en/elasticsearch/reference/2.3/query-dsl-missing-query.html

Dorjee Dhondup
  • 549
  • 6
  • 15
Devi
  • 5,023
  • 1
  • 34
  • 27
23

You can directly do

{
    "query": {
        "exists": {
            "field": "fieldName"
        }
    }
}

If you wanna add some match too, then you can go for

{
    "query": {
        "bool": {
            "must": [{
                "match": {
                    "fieldName": "value"
                }
            },
            {
                "exists": {
                    "field": "fieldName"
                }
            }]
        }
    }
}

https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-exists-query.html

Surya Purohit
  • 1,090
  • 1
  • 9
  • 29
9

You can use exists filter:

{
  "query": {
    "filtered": {
      "filter": {
        "exists": {
          "field": "status"
        }
      },
      "query": {
        "match_all": {}
      }
    }
  }
}

Regards, Alain

adouang
  • 374
  • 2
  • 8
8
GET /_search
{
    "query": {
        "exists" : { "field" : "price" }
    }
}

source: https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-exists-query.html

Thiago Falcao
  • 4,463
  • 39
  • 34