ps0604
ps0604

Reputation: 1071

Less restrictive search doesn't return any hits in ElasticSearch

The query below returns hits, for example where name is "Balances by bank":

GET /_search

{ "query": {
    "multi_match": { "query": "Balances",
                     "fields": ["name","descrip","notes"] 
                   }
           }
 }

So why this doesn't return anything? Note that the query is less restrictive, the word is "Balance" and not "Balances" with an s.

GET /_search

{ "query": {
    "multi_match": { "query": "Balance",
                     "fields": ["name","descrip","notes"] 
                   }
           }
 }

What search would return both?

Upvotes: 0

Views: 21

Answers (1)

leandrojmp
leandrojmp

Reputation: 7463

You need to change your mapping to be able to do that.

If you didn't specified a mapping with specific analyzers when creating your index, elasticsearch will use the default mapping and analyzer.

The default mapping will map each text field as both text and keyword, so you will be able to performe full text search (match part of the string) and keyword search (match the whole string), but it will use the standard analyzer.

With the standard analyzer your example Balances by bank becomes the following list of tokens: [Balances, by, bank], those items are added to the inverted index and elasticsearch can find the documents when you search for any of them.

When you search for just Balance, this term does not exist in the inverted index and elasticsearch returns nothing.

To be able to return both Balance and Balances you need to change your mapping and use the analyzer for the english language, this analyzer will reduce your terms to their stem and match Balance, Balances as also Balancing, Balanced, Balancer etc.

Look at this part of the documentation to see how the analysis process work.

And of course, you can also search for Balance* and it will return both Balance and Balances, but it is a different query.

Upvotes: 1

Related Questions