spiralarchitect
spiralarchitect

Reputation: 910

Elasticsearch - get documents which have a field name

Lets say I am putting documents in elastic search as following:

{
"name": "xyz",
"address": {
    "pincode": "111111",
    "area": "downtown"
    }
}

I want to get all the documents which have pincode in them. Ofcourse, some documents entered in ES might not have pincode field.

I tried with something like this, but was unsuccessful:

XGET _search
{
    "query": {
        "terms": {
        "_field_names": [ "pincode" ] 
        }
    }
}

Upvotes: 0

Views: 840

Answers (2)

Val
Val

Reputation: 217254

You need to use the missing filter. The exists filter will not work if the field is missing altogether in your document.

{
  "query": {
    "filtered": {
      "filter": {
        "bool": {
          "must_not": [
            {
              "missing": {
                "field": "address.pincode"
              }
            }
          ]
        }
      }
    }
  }
}

Upvotes: 1

DrTyrsa
DrTyrsa

Reputation: 31951

You need exists query.

{
    "query" : {
        "filtered" : {
            "filter" : {
                "exists" : { "field" : "address.pincode" }
            }
        }
    }
}

Upvotes: 1

Related Questions