I am trying to implement an exact match search in elastic search. But I am not getting the required results. Here is the code to explain the issue I am facing and things I tried.
doc1 = {"sentence": "Today is a sunny day."}
doc2 = {"sentence": " Today is a sunny day but tomorrow it might rain"}
doc3 = {"sentence": "I know I am awesome"}
doc4 = {"sentence": "The taste of your dish is awesome"}
doc5 = {"sentence": "The taste of banana shake is good"}
# Indexing the above docs
es.index(index="english",doc_type="sentences",id=1,body=doc1)
es.index(index="english",doc_type="sentences",id=2,body=doc2)
es.index(index="english",doc_type="sentences",id=3,body=doc3)
es.index(index="english",doc_type="sentences",id=4,body=doc4)
es.index(index="english",doc_type="sentences",id=5,body=doc5)
query 1
res = es.search(index="english",body={"from":0,"size":5,
"query":
{"match_phrase":
{"sentence":{"query":"Today is a sunny day"}
}},
"explain":False})
query 2
res = es.search(index="english",body={"from":0,"size":5,
"query":{
"bool":{
"must":{
"match_phrase":
{"sentence":{"query":"Today is a sunny day"}
}},
"filter":{
"term":{
"sentence.word_count": 5}},
}
}
})
So when I run query 1, I get doc2 as the top result, while I want doc1 to be the top result.
When I am trying to use filter for the same( to restrict the length of search to the length of query), as in query 2 , I am getting no result.
I will be really grateful if I can get any help on solving this. I want an exact match for the given query and not the match which contains that query.
Thanks