elasticsearch中四种常见的相关度分数优化方法

elasticsearch中四种常见的相关度分数优化方法,第1张

elasticsearch中四种常见的相关度分数优化方法 对相关度评分进行调节和优化的常见的4种方法

1、query-time boost 查询的时候设置query的boost. 增加权重

2、重构查询结构.如should中嵌套bool。

3、negative boost 包含了negative term的doc,分数乘以negative boost,分数降低

4、constant_score 如果你压根儿不需要相关度评分,直接走constant_score加filter,所有的doc分数都是1,没有评分的概念了

1、query-time boost

GET /forum/article/_search
{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "title": {
              "query": "java spark",
              "boost": 2
            }
          }
        },
        {
          "match": {
            "content": "java spark"
          }
        }
      ]
    }
  }
}
2、重构查询结构

重构查询结果,在es新版本中,影响越来越小了。一般情况下,没什么必要的话,大家不用也行。

GET /forum/article/_search 
{
  "query": {
    "bool": {
      "should": [
        {
          "match": {
            "content": "java"  1/3
          }
        },
        {
          "match": {
            "content": "spark"  1/3
          }
        },
        {
          "bool": {
            "should": [
              {
                "match": {
                  "content": "solution"  1/6
                }
              },
              {
                "match": {
                  "content": "beginner"  1/6
                }
              }
            ]
          }
        }
      ]
    }
  }
}
3、negative boost

搜索包含java,不包含spark的doc,但是这样子很死板

搜索包含java,尽量不包含spark的doc,如果包含了spark,不会说排除掉这个doc,而是说将这个doc的分数降低

包含了negative term的doc,分数乘以negative boost,分数降低

GET /forum/article/_search 
{
  "query": {
    "bool": {
      "must": [
        {
          "match": {
            "content": "java"
          }
        }
      ],
      "must_not": [
        {
          "match": {
            "content": "spark"
          }
        }
      ]
    }
  }
}

GET /forum/article/_search 
{
  "query": {
    "boosting": {
      "positive": {
        "match": {
          "content": "java"
        }
      },
      "negative": {
        "match": {
          "content": "spark"
        }
      },
      "negative_boost": 0.2
    }
  }
}

negative的doc,会乘以negative_boost,降低分数

4、constant_score

如果你压根儿不需要相关度评分,直接走constant_score加filter,所有的doc分数都是1,也可以加上boost指定一个固定的分数,比如0,就是纯不计算积分,也就没有评分的概念了

GET /forum/article/_search 
{
  "query": {
    "bool": {
      "should": [
        {
          "constant_score": {
              "boost": 0,
            "filter": {
              "match": {
                "title": "java"
              }
            }
          }
        },
        {
          "constant_score": {
              "boost": 0,
            "filter": {
              "match": {
                "title": "spark"
              }
            }
          }
        }
      ]
    }
  }
}

实战案例

1. 在搜索中,我们有这样一种需求,期望搜索的结果中包含java 如果标题中包含hadoop或spark就优先搜索出来,同时呢,如果一个帖子包含java hadoop,一个帖子包含java spark,包含hadoop的帖子要比spark优先搜索出来

对于这样的需求,通俗来讲,就是需要通过增大某些搜索条件的权重,从而在搜索的结果中,更多符合和满足我们业务场景的数据靠前搜索出来,在es中可以通过boost关键词来增加搜索条件的权重,

GET /forum/article/_search
{
	"query": {
		"bool": {
			"must": [{
				"match": {
					"title": "java"
				}
			}],
			"should": [{
				"match": {
					"title": {
						"query": "hadoop"
					}
				}
			}, {
				"match": {
					"title": {
						"query": "spark",
						"boost": 2
					}
				}
			}, {
				"match": {
					"title": {
						"query": "php"
					}
				}
			}, {
				"match": {
					"title": {
						"query": "hadoop",
						"boost": 5
					}
				}
			}]
		}
	}
}

参考:elasticsearch中四种常见的相关度分数优化方法 - PHP面试网

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5683659.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存