2

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?

1
  • You can combine missing filter. "filter": {"missing" : { "field" : "name" }} Commented Apr 22, 2016 at 8:54

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

1 Comment

I hope to use exists filter.
0

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

2 Comments

Scripts are slow and should be avoided whenever possible.
Sebastian is right, his solution works best... only if you have not_analyzed field, otherwise it won't work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.