Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
224 views
in Technique[技术] by (71.8m points)

Best way to check if a field exist in an Elasticsearch document

May be is a very stupid question, 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?

Thanks

question from:https://stackoverflow.com/questions/32949321/best-way-to-check-if-a-field-exist-in-an-elasticsearch-document

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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"
              }
            }
          ]
        }
      }
    }
  }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...