简体   繁体   中英

Elasticsearch exists filter how to make an empty string as null value?

I tried to index a name field to elasticsearch, the name maybe is a string or a empty string( "" ), I want to search all name which contain the empty value, so I use the exists filter, but for exists filter, the empty value is not a null value.

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

query dsl:

{ "filter" : {
                "exists" : { "field" : "name" }
            } 
}

How to make the empty string as a null value for elasticsearch exist filter? Anyone has good idea?

The term filter should do the job:

    {
      "term": {
        "name": ""
      }
    }

Just tried it with some of my data and got results, but might depend if your fields are "not-analyzed" (like mine) or not.

Update: Just found this similar question which has a much more detailed answer: Find documents with empty string value on elasticsearch

You can try searching by using a script filter instead in order to test the length of the string

{
  "query": {
    "script": {
      "script": "doc.name.value?.size() == 0"
    }
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM