如同磁铁吸引四周的铁粉,热情也能吸引周围的人,改变周围的情况。

各位大神,有没有谁知道实现这种效果的思路:搜索bed关键词,最后结果显示bed在前面显示,dog bed放到后面,而不是dog bed 会放到前面来

Elasticsearch | 作者 dongxiao | 发布于2020年01月20日 | 阅读数:1200

我们领导想要提升网站搜索的相关性,想实现的效果:搜索bed关键词,最后结果显示bed在前面显示,dog bed放到后面,而不是dog bed 会放到前面来,主要目的是为了更好的把用户需要的产品展示给用户
已邀请:

laoyang360 - Elastic认证工程师 [死磕Elasitcsearch]知识星球地址:https://t.cn/RmwM3N9;微信公众号:铭毅天下; 博客:https://elastic.blog.csdn.net

赞同来自: dongxiao ppppenger

PUT test_index
{
"mappings": {
"properties": {
"title":{
"type":"text"
}
}
}
}

POST test_index/_bulk
{"index":{"_id":1}}
{"title":"bed"}
{"index":{"_id":2}}
{"title":"dog bed"}

POST test_index/_search
{
"query": {
"match_phrase": {
"title": "bed"
}
}
}

Ombres

赞同来自: dongxiao

是我理解错了吗?math_phrase貌似并不能解决这种问题吧,它解决的是召回问题,而题主说的是排序问题
 
通过增加部分结果的评分来修改排序,以下仅为示例
PUT test_index
{
"mappings": {
"properties": {
"title": {
"type": "text",
"fields": {
"keyword": {
"type": "keyword",
"ignore_above": 256
}
}
}
}
}
}


POST test_index/_bulk
{"index":{"_id":1}}
{"title":"bed"}
{"index":{"_id":2}}
{"title":"dog bed"}
{"index":{"_id":3}}
{"title":"bed dog"}

POST test_index/_search
{
"query": {
"bool": {
"must": [
{
"match": {
"title": "bed"
}
}
],
"should": [
{
"prefix": {
"title.keyword": {
"value": "bed"
}
}
}
]
}
}
}
最终结果如下
 
{
"took" : 84,
"timed_out" : false,
"_shards" : {
"total" : 1,
"successful" : 1,
"skipped" : 0,
"failed" : 0
},
"hits" : {
"total" : {
"value" : 3,
"relation" : "eq"
},
"max_score" : 1.1596571,
"hits" : [
{
"_index" : "test_index",
"_type" : "_doc",
"_id" : "1",
"_score" : 1.1596571,
"_source" : {
"title" : "bed"
}
},
{
"_index" : "test_index",
"_type" : "_doc",
"_id" : "3",
"_score" : 1.1234324,
"_source" : {
"title" : "bed dog"
}
},
{
"_index" : "test_index",
"_type" : "_doc",
"_id" : "2",
"_score" : 0.12343237,
"_source" : {
"title" : "dog bed"
}
}
]
}
}

dongxiao

赞同来自: Charles999

谢谢您的思路,但是我测试了下,实际上我们标题里面有很多文字的,比如 title="this is pepole bed" 实际上,希望这个标题排在前面,使用prefix用should语句匹配,好像没办法达到这个效果

dongxiao

赞同来自:

谢谢解答

five

赞同来自:

按理说bed应该排第一的,参考这个https://www.ksh17j.com/question/2275

要回复问题请先登录注册