如何在弹性搜索中进行精确的短语匹配?

Gau*_*wla 5 elasticsearch match-phrase

我正在尝试在弹性搜索中实现精确匹配搜索.但我没有得到所需的结果.这是解释我面临的问题和我尝试过的事情的代码.

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)
Run Code Online (Sandbox Code Playgroud)

查询1

res = es.search(index="english",body={"from":0,"size":5,
                                  "query":
                                      {"match_phrase":
                                          {"sentence":{"query":"Today is a sunny day"}
                                          }},

                                          "explain":False})
Run Code Online (Sandbox Code Playgroud)

查询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}},

                                          }
                                            }
                                            })
Run Code Online (Sandbox Code Playgroud)

因此,当我运行查询1时,我将doc2作为最高结果,而我希望doc1成为最佳结果.

当我尝试使用相同的过滤器(将搜索的长度限制为查询的长度)时,如在查询2中,我没有得到任何结果.

如果我能得到任何解决方案的帮助,我将非常感激.我想要给定查询的完全匹配,而不是包含该查询的匹配.

谢谢

小智 0

该查询将起作用 -

{
    "query":{
        "match_phrase":{
            "sentence":{
                "query":"Today is a sunny day"
            }
        }
    },
    "size":5,
    "from":0,
    "explain":false
}
Run Code Online (Sandbox Code Playgroud)