Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find empty strings in elasticsearch

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"
            }
        }
    }
}
like image 489
Evgeny Lazin Avatar asked Aug 09 '13 17:08

Evgeny Lazin


3 Answers

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 :)

like image 52
bensie Avatar answered Sep 21 '22 13:09

bensie


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": "*"
      }
     }
   ]
  }
 }
}
like image 42
Gabriel Wamunyu Avatar answered Sep 19 '22 13:09

Gabriel Wamunyu


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"
  }
}
like image 21
VirgileD Avatar answered Sep 18 '22 13:09

VirgileD