pangpang
pangpang

Reputation: 8821

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?

Upvotes: 2

Views: 7660

Answers (2)

Sebastian
Sebastian

Reputation: 2550

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

Upvotes: 3

Val
Val

Reputation: 217274

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

Upvotes: 0

Related Questions