In my elastic search I have documents which contains a "fieldname" with values "abc" and "abc-def". When I am using match_phrase query for searching documents with fieldname "abc" it is returning me documents with values "abc-def" as well. However when I am querying for "abc-def" it is working fine. My query is as follows:
Get my_index/_search
{
"query" : {
"match_phrase" : {"fieldname" : "abc"}
}
}
Can someone please help me in understanding the issue?
Match phrase query searches for those documents that have all the terms present in the field (from the search term), and the terms must be present in the correct order.
In your case, "abc-def"
gets tokenized to "abc"
and "def"
(because of standard analyzer). Now when you are using match phrase query for "abc-def"
, this searches for all documents that have both abc
and def
in the same order. (therefore you are getting only 1 doc in the result)
When searching for "abc"
, this will search for those documents that have abc
in the fieldname
field (since both the document contain abc
, so both are returned in the result)
If you want to return only exact matching documents in the result, then you need to change the way the terms are analyzed.
.keyword
to the fieldname
field. This uses the keyword analyzer instead of the standard analyzer (notice the ".keyword" after the fieldname field).Adding a working example with index data, mapping, search query and search result
Index data:
{
"name":"abc-def"
}
{
"name":"abc"
}
Search Query:
{
"query": {
"match_phrase": {
"name.keyword": "abc"
}
}
}
Search Result:
"hits": [
{
"_index": "67394740",
"_type": "_doc",
"_id": "1",
"_score": 0.6931471,
"_source": {
"name": "abc"
}
}
]