问题描述
我想在一个字段中搜索 vision项,但是通过使用DSL中的match / match_phrace / term,我只得到了 vision A, vision B, xx版本, vision等结果。
I want to search item "vision" for one field but I just got results like "vision A", "vision B", "xx version","vision" etc by using match/match_phrace/term in the DSL.
我想要的是完全匹配的视觉应该获得最高分,而包含视觉的项目应该排在完全匹配之后。排名应为:
What I want is the exact match "vision" should have the highest score and the items which contain "vision" should rank behind the exact match. The ranking should be:
vision > vision A > vision B > xx version
我检查了,其中将 index更改为 not_analyzed被识别为实现精确匹配。但是在我的情况下,不仅需要完全匹配,还需要包含匹配项。
I checked Elasticsearch match exact term in which change the "index" to be "not_analyzed" is recognized to realize exact match. But in my case not only exact match but also the containing match is needed.
在这种情况下我该怎么办?谢谢!
What can I do for this case? Thanks!
推荐答案
您可以做的是在q bool /中加入一些约束。应该
查询以便控制排名。
What you can do to achieve this is to include a few constraints in q bool/should
query in order to control the ranking.
{
"query": {
"bool": {
"must": {
"match": {
"name": "vision" <--- match on vision
}
},
"should": [
{
"term": {
"name.keyword": { <--- boost exact matches on keyword field (i.e. "vision")
"value": "vision",
"boost": 3
}
}
},
{
"prefix": {
"name.keyword": { <--- boost prefix matches on keyword field (i.e. "vision A" and "vision B"
"value": "vision",
"boost": 2
}
}
}
]
}
}
}
- 第一个子句将匹配其
名称
字段中包含vision
的所有文档。 - 第二个子句将大大提高其
name.keyword
字段完全包含vision
的文档。name.keyword
通常是一个keyword
字段(以前是not_analyzed
string
字段。) - 第三个子句将对名称为
的文档提供更高的提升。
字段以vision
开头。 - The first clause will match all documents which contain
vision
in theirname
field. - The second clause will give a higher boost to documents whose
name.keyword
field contains exactlyvision
.name.keyword
is usually akeyword
field (formerly anot_analyzed
string
field). - The third clause will give a slightly higher boost to documents whose
name.keyword
field starts withvision
.
这篇关于Elasticsearch:精确匹配的排名如何?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!