Tom Chiverton
Tom Chiverton

Reputation: 677

bool query with filter does not return any documents

The simple query

"query": {
    "simple_query_string": { "query":"great guide" } 
},

returns my document as expected, containing

"groups": [
        "Local Business"
],

But if I use a filter, it returns no documents:

"query": {
        "bool":{
            "must":[
                 {"simple_query_string": { "query":"great guide" }} 
            ],
            "filter":{
              "terms":{
                "groups":["Local Business"]
              }
            }
        }
    },

If I remove the "filter" key and values, then the document is retrieved.

Why isn't the filter matching the document ?

Upvotes: 0

Views: 148

Answers (1)

Kamal Kunjapur
Kamal Kunjapur

Reputation: 8840

If the groups field is of type keyword, then the query you've mentioned works as expected.

However it wouldn't work if the field groups if of type text. In that case the below query would actually fit what you are looking for.

Query for group - Type text

POST <your_index_name>/_search
{  
   "query":{  
      "bool":{  
         "must":[  
            {  
               "simple_query_string":{  
                  "query":"great guide"
               }
            }
         ],
         "filter":{  
            "match":{  
               "groups":"Local Business"
            }
         }
      }
   }
}

The reason the query you've mentioned doesn't work for the field of type text is because this field goes through Analysis phase making use of Standard Analyzer by default where it would first convert Local Business into small cases and then saves local and business as two individual words in the inverted index.

Elasticsearch would only give you results if the words you query match what's available in the index.

And what keyword does is, it saves Local Business as is in inverted index.

Note: You can try the query you have by replacing groups with groups.keyword if mapping hasn't been defined and is created dynamically.

Hope this helps!

Upvotes: 1

Related Questions