Elasticsearch:得到精确匹配,然后模糊
Elasticsearch: get exact match, then fuzzy
我运行这个查询:
GET /thing/_search/?
{
"query": {
"multi_match" : {
"query": "castle pontivy",
"type": "most_fields",
"fields": [ "title", "loc" ]
}
}
}
有效,returns 来自 thing
,其中 title
和 loc
包含 castle
and/or pontivy
相关订单。不错
现在我想继续这样查询,但我也希望结果更喜欢 title
上的完全匹配。这意味着如果一个项目在其标题中完全匹配 castle pontivy
,则它必须作为第一个元素返回(然后其他结果将照常处理)。
有办法吗?
你可以做一个 phrase
匹配并给它一个 boost
5,所以无论默认分数是多少,它都会加上 +5。如果您想获得更多得分,请查看 function score query(我建议您这样做)
第二个 multi_match 将使用 most_fields
.
匹配其余文档
{
"query":{
"bool":{
"should":[
{
"multi_match":{
"query":"castle pontivy",
"type":"phrase",
"fields":[
"title",
"loc"
],
"boost":5
}
},
{
"multi_match":{
"query":"castle pontivy",
"type":"most_fields",
"fields":[
"title",
"loc"
]
}
}
]
}
}
}
我运行这个查询:
GET /thing/_search/?
{
"query": {
"multi_match" : {
"query": "castle pontivy",
"type": "most_fields",
"fields": [ "title", "loc" ]
}
}
}
有效,returns 来自 thing
,其中 title
和 loc
包含 castle
and/or pontivy
相关订单。不错
现在我想继续这样查询,但我也希望结果更喜欢 title
上的完全匹配。这意味着如果一个项目在其标题中完全匹配 castle pontivy
,则它必须作为第一个元素返回(然后其他结果将照常处理)。
有办法吗?
你可以做一个 phrase
匹配并给它一个 boost
5,所以无论默认分数是多少,它都会加上 +5。如果您想获得更多得分,请查看 function score query(我建议您这样做)
第二个 multi_match 将使用 most_fields
.
{
"query":{
"bool":{
"should":[
{
"multi_match":{
"query":"castle pontivy",
"type":"phrase",
"fields":[
"title",
"loc"
],
"boost":5
}
},
{
"multi_match":{
"query":"castle pontivy",
"type":"most_fields",
"fields":[
"title",
"loc"
]
}
}
]
}
}
}