前言
elasticsearch
给我们提供了很强大的搜索功能,但是有时候仅仅只用相关度打分是不够的,所以elasticsearch
给我们提供了自定义打分函数function_score
,本文结合简单案例详解function_score
的使用方法,关于function-score-query
的文档最权威的莫过于官方文档:
function_score官方文档
基本数据准备
我们创建一张新闻表,包含如下字段:
字段 | 类型 | 说明 |
id | Long | 新闻ID |
title | string | 标题 |
tags | string | 标签 |
read_count | long | 阅读数 |
like_count | long | 点赞数 |
comment_count | long | 评论数 |
rank | double | 自定义权重 |
location | arrays | 文章发布经纬度 |
pub_time | date | 发布时间 |
创建elasticsearch
的 Mapping
:
准备测试数据:
id | title | tags | read_count | comment_count | like_count | rank | location | pub_time |
1 | 台风“杜苏芮”登陆福建晋江 多部门多地全力应对 | 台风;杜苏芮;福建 | 10000 | 2000 | 600 | 0 | 118.55199,24.78144 | 2023-07-29 09:47 |
2 | 受台风“杜苏芮”影响 北京7月29日至8月1日将有强降雨 | 台风;杜苏芮;北京 | 1000 | 200 | 60 | 0 | 116.23128,40.22077 | 2023-06-29 14:49:38 |
3 | 杭州解除台风蓝色预警信号 | 台风;杭州 | 10 | 2 | 6 | 0.9 | 120.21201,30.2084 | 2020-07-29 14:49:38 |
批量添加数据到elasticsearch
中:
random_score
的使用
我们通过random_score
理解一下weight
、score_mode
,boost_mode
的作用分别是什么,先直接看Demo
对应JAVA
查询代码:
查询结果:
这个查询使用的function_score
,query
中通过title
搜索“台风”,在functions
我们增加了两个打分,一个是random_score
,随机生成一个得分,得分的weight
权重是1,第二个是如果标题中有“杭州”,得分权重为42,
random_score
顾名思义就是生成一个(0,1)之间的随机得分,我能想到的一个应用场景是,有一天产品要求:每个人看到新闻都不一样,要做到“千人千面”,而且只给你一天的时间,这样我们就可以使用random_score
,每次拉取的数据都是随机的,每个人看到的新闻都是不一样的,这个随机查询比Mysql
实现简单多了,0成本实现了“千人千面”。weight
这个就是给生成的得分增加一个权重,在上面的Demo中,我们第一个weight=1
,第二个weight=42
,从搜索结果得分可以看出“杭州解除台风蓝色预警信号”这条得分是42.40192,而下面的只有0.8194501,因为增加了42倍的权重。score_mode
score_mode
的作用是对functions
中计算出来的多个得分做汇总计算,比如我用了是sum
,就是指将上面random_score
得到的打分和filter
中得到的42分相加,也就是说第一条42.40192得分是random_score
生成了0.40192再加上filter
中得到了42分。score_mode
默认是采用multiply
,总共有6种计算方式:
| 计算方式 |
| scores are multiplied (default) |
| scores are summed |
| scores are averaged |
| the first function that has a matching filter is applied |
| maximum score is used |
| minimum score is used |
boost_mode
boost_mode
作用是将functions
得到的总分数和我们query
查询的得到的分数做计算,比如我们使用的是replace
就是完全使用functions
中的得分替代query
中的得分,boost_mode
总共有6种计算方式:
| 得分计算方式 |
| query score and function score is multiplied (default) |
| only function score is used, the query score is ignored |
| query score and function score are added |
| average |
| max of query score and function score |
| min of query score and function score |
script_score
的使用
script_score
就是用记可以通过各种函数计算你文档中出现的字段,算出一个自己想要的得分,我们直接看Demo
每篇新闻有阅读数
、点赞数据
、评论数
,我们可以通过这三个指标算出一个分值来评价一篇文章的热度,然后将这个热度和query
中的得分相乘,这样热度很高的文章可以排到更前面。在这个Demo中我使用了一个简单的加权来计算文章热度,一般来说阅读数
是最大的,点赞数
次之,评论数
是最小的。
这里为了演示,简单算一下文章热度,真实的要比这个复杂的多,可能不同种类的文章重要性也是不一样的。
field_value_factor
的使用
field_value_factor
可以理解成elasticsearch
给你一些内置的script_score
,每次写script_score
必定不是太方便,如果有一些内置的函数,开箱即用就方便多了,我们直接看Demo
这里的field_value_factor
就对相当script_score
的 sqrt(10 * doc['rank'].value)
,这里的factor是乘以多少倍,默认是1倍,missing
是如果没有这个字段默认值为1,modifier
是计算函数,field
是要计算的字段。
modifier
计算函数有以下类型可以选择
| 得分计算方式 |
| Do not apply any multiplier to the field value |
| Take the common logarithm of the field value. Because this function will return a negative value and cause an error if used on values between 0 and 1, it is recommended to use |
| Add 1 to the field value and take the common logarithm |
| Add 2 to the field value and take the common logarithm |
| Take the natural logarithm of the field value. Because this function will return a negative value and cause an error if used on values between 0 and 1, it is recommended to use |
| Add 1 to the field value and take the natural logarithm |
| Add 2 to the field value and take the natural logarithm |
| Square the field value (multiply it by itself) |
| Take the square root of the field value |
| Reciprocate the field value, same as |
衰减函数Decay functions的使用
衰减函数可以理解成计算文档中某一个字段与给定值的距离,如果距离越近得分就越高,距离越远得分就越低,这个就比较适用于新闻发布时间的衰减了,越久前发布的新闻,得分应该越小,排序越往后。我们直接看Demo
搜索结果:
衰减函数有3种,分别为gauss
高斯函数、lin
线程函数、exp
对数函数,具体的计算公式可以参考官方文档,这里我们主要理解衰减函数的4个参数作用是什么。
origin
可以理解成计算距离的原点,比如上面计算新闻发布时间的原点是当前时间,计算经纬度的原点是用户搜索位置,比如我在杭州,那么origin
就是杭州的经纬度offset
这个偏移量可以理解成不需要衰减的距离,比如在上面的Demo中,距离pub_time
的offset
为7d,意思是说近7天内发布的新闻都不需要衰减,得分直接为1。计算经纬度中的offset
为50km意思是说距离用户50km里的新闻不需要衰减,50km内的基本都是杭州本地的新闻,就没必要衰减了。scale
和decay
这两个参数可以参考官方给的三种函数衰减图,scale
和decay
表示距离为scale
后得分衰减到原来的scale
倍。比如上面时间衰减offset=7d, scale=60d,decay= 0.9
加起来的意思就是7天内的新闻不衰减,67天(7d+60d)前的新闻得分为0.9,在经纬度衰减中offset=50km, scale=50km,decay= 0.1
的意思是50km内的距离不衰减,100km(50km+50km)外的数据得分为0.1。
总结
elasticsearch
的function_score
给我提供了好几种很灵活的自定义打分策略,在实际项目中需要根据自己的需求合理的组合这些打分策略并调整对应参数才能满足自己的搜索需求,本文主要介绍function_score
的使用,接下来我会根据一个实际的搜索应用介绍一下如何组合、设置这些函数以达到比较理解的搜索效果。