Home > Blockchain >  Search a Word with space in ES without modifying index mapping
Search a Word with space in ES without modifying index mapping

Time:03-09

I am using spring boot version 2.3.9 Release and ES is 7.6.How to search a Word with space in ES without modifying index mapping. For Example Name: John Jose I am trying with below code, but not working. I read some other post that we need to add 'Not analyser'. I no need to modify index mapping. is it possible to add this check in query builder itself.

QueryBuilder testQuery = QueryBuilders.matchQuery("cname","John Jose");

index mapping

"cname": {
"type": "text",
"fields": {
"keyword": {
"ignore_above": 256,
"type": "keyword"
}
}
}

CodePudding user response:

text field is analyzed and by default standard analyzer is used, Hence in your case, your John Jose will create tokens as john and jose (notice lowercase as well), and search time also for match query it will be tokenize using the same analyzer, So if your indexed documents have content like John Jose, it will match.. No need to do anything from your side.

Can you share your sample documents and explain what you mean that it's not working?

CodePudding user response:

It is not working because you are trying Match Query. By default match query set operator as OR.

So for your code, internally it will create query like John OR Jose and return result.

If you want both word present in name in any position then you can try to set default operator value AND as shown in below. which will create query like John AND Jose

QueryBuilder testQuery = QueryBuilders.matchQuery("cname","John Jose").operator(Operator.AND);

If you want to do phrase match (execat match) then you can try below query which will internally execute query like "John Jose" (as per my understanding you are looking for this query)

QueryBuilder testQuery = QueryBuilders.matchPhraseQuery("cname","John Jose");
  • Related