2

I have many documents with uID:. To query those documents, I am querying as below -

'query': {
           'term':  {
               'uID': ''
             }
    }

But it is fetching no documents. Why it is not throwing any document? Is there any other way to do this?

3
  • You have to make sure that ,your uID is indexed as keyword ,otherwise elastic will do a full text search Commented Nov 1, 2019 at 11:38
  • @Shubh Thanks, is there other way to query if uID isn't indexed as keyword? Commented Nov 1, 2019 at 11:43
  • 1
    See this -discuss.elastic.co/t/query-for-an-empty-string/83070 Commented Nov 1, 2019 at 11:44

1 Answer 1

1

Even with the default analyzer you can do this kind of search: use a script filter, which is slower but can handle the empty string:

curl -XPOST 'http://localhost:3000/_search' -d 
{
 "query": {
   "filtered": {
     "filter": {
       "script": {
         "script": "term. uID.length() == 0"
       }
     }
   }
 }
}

It will return the document with empty string as _content without a special mapping

As pointed by @js_gandalf, this is deprecated for ES>5.0. Instead you should use: query->bool->filter->script as in https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-bool-query.html

q=!(yourfield.keyword:"")

Please refer below link for more information. https://www.elastic.co/guide/en/elasticsearch/reference/6.5/query-dsl-query-string-query.html#query-string-syntax

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

2 Comments

Thanks. And I checked with query->...->script but no avail. BTW, where did @js_gandalf point this?
elastic.co/guide/en/elasticsearch/reference/6.5/… Please refer this link for more information. I've updated my answer, please refer again.

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.