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

前端 未结 5 2178
梦如初夏
梦如初夏 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"
                  }
                }
              ]
            }
          }
        }
      }
    }
    
    0 讨论(0)
  • 2020-12-14 15:41

    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

    0 讨论(0)
  • 2020-12-14 15:47
    GET /_search
    {
        "query": {
            "exists" : { "field" : "price" }
        }
    }
    

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

    0 讨论(0)
  • 2020-12-14 15:54

    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

    0 讨论(0)
  • 2020-12-14 15:54

    You can use exists filter:

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

    Regards, Alain

    0 讨论(0)
提交回复
热议问题