elasticsearch(17) 几种优化查询分数得方法

xiaoxiao2025-10-15  5

1.加boost权重

2.重构查询语句

3.nagtive boost 负相关分数

4.constant_score 固定分数

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

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"           }         },         {           "match": {             "content": "spark"           }         },         {           "bool": {             "should": [               {                 "match": {                   "content": "solution"                 }               },               {                 "match": {                   "content": "beginner"                 }               }             ]           }         }       ]     }   } }

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,没有评分的概念了

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

5.自己写函数

给所有的帖子数据增加follower数量

POST /forum/article/_bulk { "update": { "_id": "1"} } { "doc" : {"follower_num" : 5} } { "update": { "_id": "2"} } { "doc" : {"follower_num" : 10} } { "update": { "_id": "3"} } { "doc" : {"follower_num" : 25} } { "update": { "_id": "4"} } { "doc" : {"follower_num" : 3} } { "update": { "_id": "5"} } { "doc" : {"follower_num" : 60} }

将对帖子搜索得到的分数,跟follower_num进行运算,由follower_num在一定程度上增强帖子的分数 看帖子的人越多,那么帖子的分数就越高

GET /forum/article/_search {   "query": {     "function_score": {       "query": {         "multi_match": {           "query": "java spark",           "fields": ["tile", "content"]         }       },       "field_value_factor": {         "field": "follower_num",         "modifier": "log1p",         "factor": 0.5       },       "boost_mode": "sum",       "max_boost": 2     }   } }  

转载请注明原文地址: https://www.6miu.com/read-5037933.html

最新回复(0)