I am using a matchQuery to query Elasticsearch in Java. Below is my query:
sourceBuilder.query(QueryBuilders.matchQuery("TransactionId_s","BulkRunTest.20Nov20201446.00"));
The field TransactionId_s is not a keyword. And I am expecting the matchQuery to match the exact string I have given and return the results. There should be no documents in Elasticsearch with TransactionId_s as BulkRunTest.20Nov20201446.00. But I am getting some results and they have the TransactionId_s like below:
"TransactionId_s" : "BulkRunTest.17Sep20201222.00"
"TransactionId_s" : "BulkRunTest.22Sep20201450.00"
"TransactionId_s" : "BulkRunTest.20Sep20201250.00"
When I tried using a termQuery instead of matchQuery, I am getting 0 results, which is the expected result. I thought matchQuery would allow me to query any field for the given value without me having to worry about tokenization. Am i wrong? And how do I resolve the issue I am seeing?
Any help would be much appreciated. Thank you.
Matchqueries are analyzed ie it applied the same analyzer which is used on the field at index time, you can analyzer API and see the tokens for indexed and search term.Considering you have a
textfield with default analyzer(Standard) it will generate the below token for search termBulkRunTest.20Nov20201446.00And generated tokens
Now lets see the tokens for one of the matches doc
BulkRunTest.17Sep20201222.00And generated tokens
As you can see
bulkruntestis the same token in both indexed and search term, hence the match query returned the search result and same is with another indexed doc.If you used the default auto-generated mapping and have
.keywordsubfield then you can use the.keywordfield for the exact search.Working example
And search result