40810

Find empty strings in elasticsearch

Question:

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

Answer1:

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

Answer2:

Try using must_not with missing in your bool:

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

Answer3:

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

Answer4:

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

Answer5:

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

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

Answer6:

You can try this temporary solution which works but isn't optimal - <a href="https://github.com/elastic/elasticsearch/issues/7515" rel="nofollow">https://github.com/elastic/elasticsearch/issues/7515</a>

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": "*" } } ] } } }

Recommend

  • Search Facebook by first name with Koala
  • Get highest value from a file using mSL and mIRC
  • How to enforce project-wide unique ids/error codes for easily finding the origin of the error in sou
  • Finding max value in CUDA
  • Cassandra: What is a subcolumn
  • How can I count unique terms in a plaintext file case-insensitively?
  • How to make SASS put relative paths in its output
  • Classic ASP URL Rewriting
  • ASPNetCore MVC Routing Let Server Handle Specific Route
  • why calling cd shell command through system() or execvp() from a child process won't work?
  • several dataProvider per one Test in TestNG
  • extjs4 catching the scroll event on panel
  • using html data-attributes as css-variable (i.e. text-shadow)
  • there is no graph with tensorboard
  • Image map in Flex
  • How to specify input and output paths from cmd.exe for a PowerShell script?
  • npm 5.4.1 install/uninstall all failing
  • Scala multiline string placeholder
  • What does 'Language neutral' mean with regard to MAKELANGID?
  • Word Open XML Mail Merge
  • OpenGL 3.3 on Mac OSX El Capitan with LWJGL
  • Allowing both email and username for authentication
  • Get data from AJAX - How to
  • How to delay loading a property with linq to sql external mapping?
  • Adding a button at the bottom of a table view
  • How can I send an e-mail from a vbs script
  • Sails.js/waterline: Executing waterline queries in toJSON function of a model?
  • Jenkins: How To Build multiple projects from a TFS repository?
  • Is there a amazon webstore API for customers?
  • Can Jackson SerializationFeature be overridden per field or class?
  • Running a C# exe file
  • When should I choose bucket sort over other sorting algorithms?
  • How can I get HTML syntax highlighting in my editor for CakePHP?
  • Trying to get generic when generic is not available
  • how does django model after text[] in postgresql [duplicate]
  • How do I configure my settings file to work with unit tests?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Binding checkboxes to object values in AngularJs
  • Android Heatmap on canvas or ImageView
  • Conditional In-Line CSS for IE and Others?