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

前端 未结 5 2203
梦如初夏
梦如初夏 2020-12-14 15:21

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 ex

5条回答
  •  鱼传尺愫
    2020-12-14 15:31

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

提交回复
热议问题