【发布时间】:2019-02-11 21:18:49
【问题描述】:
我正在尝试在弹性搜索中实现完全匹配搜索。但我没有得到所需的结果。 这是解释我面临的问题和我尝试过的事情的代码。
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)
查询 1
res = es.search(index="english",body={"from":0,"size":5,
"query":
{"match_phrase":
{"sentence":{"query":"Today is a sunny day"}
}},
"explain":False})
查询 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}},
}
}
})
因此,当我运行查询 1 时,我得到 doc2 作为最高结果,而我希望 doc1 成为最高结果。
当我尝试使用相同的过滤器(将搜索长度限制为查询长度)时,就像在查询 2 中一样,我没有得到任何结果。
如果我能得到任何帮助来解决这个问题,我将不胜感激。我想要给定查询的完全匹配,而不是包含该查询的匹配。
谢谢
【问题讨论】:
-
如果您想要精确的词组匹配,为什么在查询 1 中将 slop 设置为 3?
-
我的意思是,我希望匹配是用相同的词,顺序可以不同。
-
那么这不是“完全匹配”,您应该更新您的问题以使其清楚;-)
-
删除了 slop ;-)
-
我的直觉告诉我,您的索引有 5 个主分片,并且您没有足够的文档来使分数相关。如果您使用单个主分片创建索引,您的第一个查询将返回您期望的文档。承诺 ;-)
标签: elasticsearch match-phrase