6

I'm trying to _search documents that has some specific value in the field.

{
  "query": {
      "bool": {
        "must": [
         {"field": {"advs.status": "warn"}}
       ]
      }
  }
}

That works find. But when I'm trying to find documents that has empty string in that field, I get this error:

ParseException[Cannot parse '' ...

and then - long list of what was expected instead of empty string.

I try this query:

{
  "query": { 
    "bool": {
        "must": [
            {"term": {"advs.status": ""}}
         ]
        }
  }
}

It doesn't fails but finds nothing. It works for non empty strings instead. How am I supposed to do this?

My mapping for this type looks exactly like this:

{
    "reports": {
        "dynamic": "false",
        "_ttl": {
            "enabled": true,
            "default": 7776000000
        },
        "properties": {
            "@fields": {
                "dynamic": "true",
                "properties": {
                    "upstream_status": {
                        "type": "string"
                    }
                }
            },
            "advs": {
                "properties": {
                    "status": {
                        "type": "string",
                        "store": "yes"
                    }
                }
            },
            "advs.status": {
                "type": "string",
                "store": "yes"
            }
        }
    }
}
1
  • How can I enable syntax highlighting for json? Commented Aug 9, 2013 at 17:12

6 Answers 6

6

Or another way to do the same thing more efficiently is to use the exists filter:

"exists" : {
  "field" : "advs.status"
}

Both are valid, but this one is better :)

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

Comments

2

You can try this temporary solution which works but isn't optimal - https://github.com/elastic/elasticsearch/issues/7515

PUT t/t/1
{
"textContent": ""
}

PUT t/t/2
{
 "textContent": "foo"
}

GET t/t/_search
{
 "query": {
  "bool": {
   "must": [
    {
      "exists": {
        "field": "textContent"
      }
    }
  ],
  "must_not": [
    {
      "wildcard": {
        "textContent": "*"
      }
     }
   ]
  }
 }
}

Comments

1

Try using must_not with missing in your bool:

"must_not":{
  "missing":{
    "field":"advs.status",
    "existence":true,
    "null_value":true
  }
}

1 Comment

Could you combine the two answers into one?
1

If tou want to search for fields containing an empty string, either you change your mapping to set not_analyzed to this particular field or you can use a script filter:

"filter": {
  "script": {
    "script": "_source.advs.status.length() == 0"
  }
}

Comments

1

I generally use a filter if the field is not analyzed. Here is snippet:

{
  "filtered": {
    "filter": {
      "term": {
        "field": ""
      }
    }
  }
},

Comments

0

the "missing" does work only for null values or not being there at all. Matching empty string was already answered here: https://stackoverflow.com/a/25562877/155708

Comments

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.