前言
使用ES时,对于查询出的文档无疑会有文档相似度之别。而理想的排序是和查询条件相关性越高排序越靠前,而这个排序的依据就是_score。本文就是详解_score有关的信息,希望能对排序评分的理解有所帮助。
正文
一个文档对于搜索的评分一定是有据可依的,而接下来就要介绍根据哪些参数查找匹配的文档以及评分的标准。
词频/逆向文档频率(TF/IDF)
当匹配到一组文档后,需要根据相关度排序这些文档,不是所有的文档都包含所有词,有些词比其他的词更重要。一个文档的相关度评分部分取决于每个查询词在文档中的 权重 。词的权重由三个因素决定,在 什么是相关 中已经有所介绍,有兴趣可以了解下面的公式,但并不要求记住。
词频
词在文档中出现的频度是多少? 频度越高,权重 越高 。 5 次提到同一词的字段比只提到 1 次的更相关。词频的计算方式如下:
tf(t in d) = √frequency 词 t 在文档 d 的词频( tf )是该词在文档中出现次数的平方根。
如果不在意词在某个字段中出现的频次,而只在意是否出现过,则可以在字段映射中禁用词频统计:
1
2
3
4
5
6
7
8
9
10
11
12
13
PUT /my_index
{
"mappings": {
"doc": {
"properties": {
"text": {
"type": "string",
"index_options": "docs"
}
}
}
}
}
将参数 index_options 设置为 docs 可以禁用词频统计及词频位置,这个映射的字段不会计算词的出现次数,对于短语或近似查询也不可用。要求精确查询的 not_analyzed 字符串字段会默认使用该设置。
逆向文档频率
词在集合所有文档里出现的频率是多少?频次越高,权重 越低 。 常用词如 and 或 the 对相关度贡献很少,因为它们在多数文档中都会出现,一些不常见词如 elastic 或 hippopotamus 可以帮助我们快速缩小范围找到感兴趣的文档。逆向文档频率的计算公式如下:
idf(t) = 1 + log ( numDocs / (docFreq + 1))
词 t 的逆向文档频率( idf )是:索引中文档数量除以所有包含该词的文档数,然后求其对数。
文档长度归一值
字段的长度是多少? 字段越短,字段的权重 越高 。如果词出现在类似标题 title 这样的字段,要比它出现在内容 body 这样的字段中的相关度更高。字段长度的归一值公式如下:
norm(d) = 1 / √numTerms
字段长度归一值( norm )是字段中词数平方根的倒数。
字段长度的归一值对全文搜索非常重要, 许多其他字段不需要有归一值。无论文档是否包括这个字段,索引中每个文档的每个 string 字段都大约占用 1 个 byte 的空间。对于 not_analyzed 字符串字段的归一值默认是禁用的,而对于 analyzed 字段也可以通过修改字段映射禁用归一值:
1
2
3
4
5
6
7
8
9
10
11
12
PUT /my_index
{
"mappings": {
"doc": {
"properties": {
"text": {
"type": "string",
"norms": { "enabled": false }
}
}
}
}
对于有些应用场景如日志,归一值不是很有用,要关心的只是字段是否包含特殊的错误码或者特定的浏览器唯一标识符。字段的长度对结果没有影响,禁用归一值可以节省大量内存空间。
文档评分计算
评分计算公式
1
2
3
4
5
6
7
8
9
score(q,d) =
queryNorm(q) //归一化因子
· coord(q,d) //协调因子
· ∑ (
tf(t in d) //词频
· idf(t)² //逆向文档频率
· t.getBoost() //权重
· norm(t,d) //字段长度归一值
) (t in q)
下面简要介绍公式中新提及的三个参数,具体信息可以点击上方官方文档原文:
协调因子将评分与文档里匹配词的数量相乘,然后除以查询里所有词的数量,如果使用协调因子,评分会变成:
- 文档里有 fox → 评分: 1.5 * 1 / 3 = 0.5
- 文档里有 quick fox → 评分: 3.0 * 2 / 3 = 2.0
- 文档里有 quick brown fox → 评分: 4.5 * 3 / 3 = 4.5
协调因子能使包含所有三个词的文档比只包含两个词的文档评分要高出很多。
- Boost 权重:在查询中设置关键字的权重可以灵活的找到更匹配的文档。
实例测试
1
2
3
4
5
6
7
8
9
10
11
12
13
// 准备
/PUT {{host}}:{{port}}/demo
{
"mappings":{
"article":{
"properties":{
"content":{
"type":"text"
}
}
}
}
}
1
2
3
4
5
6
7
8
9
10
11
12
//导入数据
[
{
"content": "测试语句1"
},
{
"content": "测试语句2"
},
{
"content": "测试语句3,字段长度不同"
}
]
查询
1
2
3
4
5
6
7
8
/POST {{host}}:{{port}}/demo/article/_search
{
"query":{
"match":{
"content":"测"
}
}
}
测试结果:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
{
"took": 0,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": },
"hits": {
"total": 3,
"max_score": 0.2824934,
"hits": [
{
"_index": "demo",
"_type": "article",
"_id": "AWEIQ90700f4t28Wzjdj",
"_score": 0.2824934,
"_source": {
"content": "测试语句2"
}
},
{
"_index": "demo",
"_type": "article",
"_id": "AWEIQ71f00f4t28WzjZT",
"_score": 0.21247853,
"_source": {
"content": "测试语句1"
}
},
{
"_index": "demo",
"_type": "article",
"_id": "AWEIRAEw00f4t28Wzjkd",
"_score": 0.1293895,
"_source": {
"content": "测试语句3,字段长度不同"
}
}
]
}
}
奇怪的是,按照语句1和语句2的分数居然不同!因为他们两个文档的关键参数,词频,字段长度,逆向文档频率均相同,为什么算出来的分不同呢?
原因主要是因为 每个分片会根据 该分片内的所有文档计算一个本地 IDF 。而文档落在不同的分片就会导致逆向文档频率不同,算出来的分数也不同。
参见官网 被破坏的相关度
当文档数量比较大,分片分布均匀后,这个问题基本不会影响很大。那么在我们这个demo中使用添加 ?search_type=dfs_query_then_fetch来查询所有的idf。
1
2
3
4
5
6
7
8
/POST {{host}}:{{port}}/demo/article/_search?search_type=dfs_query_then_fetch
{
"query":{
"match":{
"content":"测"
}
}
}
测试结果:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": },
"hits": {
"total": 3,
"max_score": 0.14899126,
"hits": [
{
"_index": "demo",
"_type": "article",
"_id": "AWEIQ71f00f4t28WzjZT",
"_score": 0.14899126,
"_source": {
"content": "测试语句1"
}
},
{
"_index": "demo",
"_type": "article",
"_id": "AWEIQ90700f4t28Wzjdj",
"_score": 0.14899126,
"_source": {
"content": "测试语句2"
}
},
{
"_index": "demo",
"_type": "article",
"_id": "AWEIRAEw00f4t28Wzjkd",
"_score": 0.087505676,
"_source": {
"content": "测试语句3,字段长度不同"
}
}
]
}
}
可以看到,评分如我们所想得,文档1和2分数相同,而文档3因为长度更长,导致分数更低。
继续测试查询时权重的影响
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
/POST {{host}}:{{port}}/demo/article/_search?search_type=dfs_query_then_fetch
{
"query": {
"bool": {
"should": [
{
"match": {
"content": { "query": "1", "boost": 2 } }
},
{
"match": {
"content": "2" }
}
]
}
}
}
测试结果:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
{
"took": 2,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": },
"hits": {
"total": 2,
"max_score": 2.1887734,
"hits": [
{
"_index": "demo",
"_type": "article",
"_id": "AWEIQ71f00f4t28WzjZT",
"_score": 2.1887734,
"_source": {
"content": "测试语句1"
}
},
{
"_index": "demo",
"_type": "article",
"_id": "AWEIQ90700f4t28Wzjdj",
"_score": 1.0943867,
"_source": {
"content": "测试语句2"
}
}
]
}
}
可以看到,由于给予搜索关键字1更高的权重,因此文档1的分数比文档2分数要高,具体细节可以通过?explain查看。
其他更改评分的方法
由于其他几个方法官网介绍的比较详尽,所以这里就不多做介绍,直接贴上官网链接。而使用脚本评分,官网介绍有些细节不够完善,因此在此多加介绍:
按受欢迎度提升权重
过滤集提升权重
随机评分
越近越好
脚本评分
脚本评分主要应用在提供的评分满足不了需求,需要通过脚本自定义评分标准。比如虽然提供了前缀分词,但是前缀分词后,返回匹配的结果评分都是1,无法进一步区分。而我们可以通过脚本在使用tf/idf得出分数后,再加上前缀匹配后的额外分值,达到搜索和前缀匹配的目的。
那么在增加一组数据
1
2
3
{
"content":"语句测试4"
}
继续之前的查询条件:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
/POST {{host}}:{{port}}/demo/article/_search?search_type=dfs_query_then_fetch
{
"query":{
"match":{
"content":"测"
}
}
}
{
"took": 1,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": },
"hits": {
"total": 4,
"max_score": 0.11455677,
"hits": [
{
"_index": "demo",
"_type": "article",
"_id": "AWEIQ71f00f4t28WzjZT",
"_score": 0.11455677,
"_source": {
"content": "测试语句1"
}
},
{
"_index": "demo",
"_type": "article",
"_id": "AWEIQ90700f4t28Wzjdj",
"_score": 0.11455677,
"_source": {
"content": "测试语句2"
}
},
{
"_index": "demo",
"_type": "article",
"_id": "AWEIaVP000f4t28W0AmE",
"_score": 0.11455677,
"_source": {
"content": "语句测试4"
}
},
{
"_index": "demo",
"_type": "article",
"_id": "AWEIRAEw00f4t28Wzjkd",
"_score": 0.065936774,
"_source": {
"content": "测试语句3,字段长度不同"
}
}
]
}
}
从测试结果中看到,虽然语句4顺序不同,但是根据评分算法,依旧还是同分。
如果想突出前缀匹配的效果呢?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
/POST {{host}}:{{port}}/demo/article/_search?search_type=dfs_query_then_fetch
{
"query": {
"function_score": {
"query": {
"match": {
"content": "测" }
},
"script_score": {
"script": {
"lang": "painless", // "source": "if(doc['content'].value.startsWith(params.keyword))return 1; return 0;",
"params":{ // 2 "keyword":"测" } }
},
"boost_mode": "sum" / }
}
}
虽然和官网的实例代码有所不同,但是这个代码在我的ES 5.6.0上能正常工作。
- painless是一种新支持的脚本语言,语言格式和java十分类似。可以参考以下文档:
painless语言介绍
painless api
painless 实例
-
脚本参数
-
score_mode计算functions中的分数形式,加减乘除,boost_mode计算最外层的分数形式,加减乘除。所以最后总分是tf/idf分数加上脚本得分。
但是运行结果爆出异常:
“reason”: “Fielddata is disabled on text fields by default. Set fielddata=true on [content] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory. Alternatively use a keyword field instead.”
主要原因是如果一个可搜索的字段,默认是不能被脚本引用的。如果强行打开,对性能消耗很大,因此不建议这种做法。
参考官方文档 fielddata
1
2
3
4
5
6
7
8
9
PUT my_index/_mapping/my_type
{
"properties": {
"my_field": {
"type": "text",
"fielddata": true
}
}
}
所以建议重新定义索引映射
1
2
3
4
5
6
7
8
9
10
11
12
PUT my_index
{
"mappings": {
"my_type": {
"properties": {
"my_field": {
"type": "text",
"fields": { "keyword": { "type": "keyword" } } }
}
}
}
}
再重新输入搜索
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
/POST {{host}}:{{port}}/demo/article/_search?search_type=dfs_query_then_fetch
{
"query": {
"function_score": {
"query": {
"match": {
"content": "测" }
},
"script_score": {
"script": {
"lang": "painless",
"source": "if(doc['content.keyword'].value.startsWith(params.keyword))return 1; return 0;", //此处更改为content.keyword
"params":{ "keyword":"测" } }
},
"boost_mode": "sum"
}
}
}
查询结果:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
{
"took": 16,
"timed_out": false,
"_shards": {
"total": 5,
"successful": 5,
"skipped": 0,
"failed": },
"hits": {
"total": 4,
"max_score": 1.1145568,
"hits": [
{
"_index": "demo",
"_type": "article",
"_id": "AWEIiwUM00f4t28W0cS6",
"_score": 1.1145568,
"_source": {
"content": "测试语句1"
}
},
{
"_index": "demo",
"_type": "article",
"_id": "AWEIiy7900f4t28W0cc0",
"_score": 1.1145568,
"_source": {
"content": "测试语句2"
}
},
{
"_index": "demo",
"_type": "article",
"_id": "AWEIi1Bq00f4t28W0cjD",
"_score": 1.0659368,
"_source": {
"content": "测试语句3,字段长度不同"
}
},
{
"_index": "demo",
"_type": "article",
"_id": "AWEIi2uV00f4t28W0cpI",
"_score": 0.11455677,
"_source": {
"content": "语句测试4"
}
}
]
}
}
可以看到,给测开头的语句加了1分,脚本运行成功。
<h2><a id="_0"></a>前言</h2>
<p>使用ES时,对于查询出的文档无疑会有文档相似度之别。而理想的排序是和查询条件相关性越高排序越靠前,而这个排序的依据就是_score。本文就是详解_score有关的信息,希望能对排序评分的理解有所帮助。</p>
<h2><a id="_3"></a>正文</h2>
<p>一个文档对于搜索的评分一定是有据可依的,而接下来就要介绍根据哪些参数查找匹配的文档以及评分的标准。</p>
<h2><a id="TFIDF_6"></a>词频/逆向文档频率(TF/IDF)</h2>
<blockquote>
<p>当匹配到一组文档后,需要根据相关度排序这些文档,不是所有的文档都包含所有词,有些词比其他的词更重要。一个文档的相关度评分部分取决于每个查询词在文档中的 权重 。词的权重由三个因素决定,在 什么是相关 中已经有所介绍,有兴趣可以了解下面的公式,但并不要求记住。</p>
</blockquote>
<h3><a id="_10"></a>词频</h3>
<p>词在文档中出现的频度是多少? 频度越高,权重 越高 。 5 次提到同一词的字段比只提到 1 次的更相关。词频的计算方式如下:</p>
<blockquote>
<p>tf(t in d) = √frequency 词 t 在文档 d 的词频( tf )是该词在文档中出现次数的平方根。</p>
</blockquote>
<p>如果不在意词在某个字段中出现的频次,而只在意是否出现过,则可以在字段映射中禁用词频统计:</p>
<pre><div class="hljs"><code class="lang-json">PUT /my_index
{
<span class="hljs-attr">"mappings"</span>: {
<span class="hljs-attr">"doc"</span>: {
<span class="hljs-attr">"properties"</span>: {
<span class="hljs-attr">"text"</span>: {
<span class="hljs-attr">"type"</span>: <span class="hljs-string">"string"</span>,
<span class="hljs-attr">"index_options"</span>: <span class="hljs-string">"docs"</span>
}
}
}
}
}
</code></div></pre>
<p>将参数 index_options 设置为 docs 可以禁用词频统计及词频位置,这个映射的字段不会计算词的出现次数,对于短语或近似查询也不可用。要求精确查询的 not_analyzed 字符串字段会默认使用该设置。</p>
<h3><a id="_35"></a>逆向文档频率</h3>
<p>词在集合所有文档里出现的频率是多少?频次越高,权重 越低 。 常用词如 and 或 the 对相关度贡献很少,因为它们在多数文档中都会出现,一些不常见词如 elastic 或 hippopotamus 可以帮助我们快速缩小范围找到感兴趣的文档。逆向文档频率的计算公式如下:</p>
<blockquote>
<p>idf(t) = 1 + log ( numDocs / (docFreq + 1))</p>
</blockquote>
<p>词 t 的逆向文档频率( idf )是:索引中文档数量除以所有包含该词的文档数,然后求其对数。</p>
<h3><a id="_43"></a>文档长度归一值</h3>
<p>字段的长度是多少? 字段越短,字段的权重 越高 。如果词出现在类似标题 title 这样的字段,要比它出现在内容 body 这样的字段中的相关度更高。字段长度的归一值公式如下:</p>
<blockquote>
<p>norm(d) = 1 / √numTerms</p>
</blockquote>
<p>字段长度归一值( norm )是字段中词数平方根的倒数。</p>
<p>字段长度的归一值对全文搜索非常重要, 许多其他字段不需要有归一值。无论文档是否包括这个字段,索引中每个文档的每个 string 字段都大约占用 1 个 byte 的空间。对于 not_analyzed 字符串字段的归一值默认是禁用的,而对于 analyzed 字段也可以通过修改字段映射禁用归一值:</p>
<pre><div class="hljs"><code class="lang-json">PUT /my_index
{
<span class="hljs-attr">"mappings"</span>: {
<span class="hljs-attr">"doc"</span>: {
<span class="hljs-attr">"properties"</span>: {
<span class="hljs-attr">"text"</span>: {
<span class="hljs-attr">"type"</span>: <span class="hljs-string">"string"</span>,
<span class="hljs-attr">"norms"</span>: { <span class="hljs-attr">"enabled"</span>: <span class="hljs-literal">false</span> }
}
}
}
}
</code></div></pre>
<p>对于有些应用场景如日志,归一值不是很有用,要关心的只是字段是否包含特殊的错误码或者特定的浏览器唯一标识符。字段的长度对结果没有影响,禁用归一值可以节省大量内存空间。</p>
<h2><a id="_69"></a>文档评分计算</h2>
<h3><a id="_70"></a>评分计算公式</h3>
<pre><div class="hljs"><code class="lang-json">score(q,d) =
queryNorm(q) //归一化因子
· coord(q,d) //协调因子
· ∑ (
tf(t in d) //词频
· idf(t)² //逆向文档频率
· t.getBoost() //权重
· norm(t,d) //字段长度归一值
) (t in q)
</code></div></pre>
<p>下面简要介绍公式中新提及的三个参数,具体信息可以点击上方官方文档原文:</p>
<ul>
<li>
<p>queryNorm 查询归化因子:会被应用到每个文档,不能被更改,总而言之,可以被忽略。</p>
</li>
<li>
<p>coord 协调因子: 可以为那些查询词包含度高的文档提供奖励,文档里出现的查询词越多,它越有机会成为好的匹配结果。</p>
</li>
</ul>
<p>协调因子将评分与文档里匹配词的数量相乘,然后除以查询里所有词的数量,如果使用协调因子,评分会变成:</p>
<ol>
<li>文档里有 fox → 评分: 1.5 * 1 / 3 = 0.5</li>
<li>文档里有 quick fox → 评分: 3.0 * 2 / 3 = 2.0</li>
<li>文档里有 quick brown fox → 评分: 4.5 * 3 / 3 = 4.5</li>
</ol>
<p>协调因子能使包含所有三个词的文档比只包含两个词的文档评分要高出很多。</p>
<ul>
<li>Boost 权重:在查询中设置关键字的权重可以灵活的找到更匹配的文档。</li>
</ul>
<h3><a id="_101"></a>实例测试</h3>
<pre><div class="hljs"><code class="lang-json">// 准备
/PUT {{host}}:{{port}}/demo
{
<span class="hljs-attr">"mappings"</span>:{
<span class="hljs-attr">"article"</span>:{
<span class="hljs-attr">"properties"</span>:{
<span class="hljs-attr">"content"</span>:{
<span class="hljs-attr">"type"</span>:<span class="hljs-string">"text"</span>
}
}
}
}
}
</code></div></pre>
<pre><div class="hljs"><code class="lang-json">//导入数据
[
{
<span class="hljs-attr">"content"</span>: <span class="hljs-string">"测试语句1"</span>
},
{
<span class="hljs-attr">"content"</span>: <span class="hljs-string">"测试语句2"</span>
},
{
<span class="hljs-attr">"content"</span>: <span class="hljs-string">"测试语句3,字段长度不同"</span>
}
]
</code></div></pre>
<p>查询</p>
<pre><div class="hljs"><code class="lang-json">/POST {{host}}:{{port}}/demo/article/_search
{
<span class="hljs-attr">"query"</span>:{
<span class="hljs-attr">"match"</span>:{
<span class="hljs-attr">"content"</span>:<span class="hljs-string">"测"</span>
}
}
}
</code></div></pre>
<p>测试结果:</p>
<pre><div class="hljs"><code class="lang-json">{
<span class="hljs-attr">"took"</span>: <span class="hljs-number">0</span>,
<span class="hljs-attr">"timed_out"</span>: <span class="hljs-literal">false</span>,
<span class="hljs-attr">"_shards"</span>: {
<span class="hljs-attr">"total"</span>: <span class="hljs-number">5</span>,
<span class="hljs-attr">"successful"</span>: <span class="hljs-number">5</span>,
<span class="hljs-attr">"skipped"</span>: <span class="hljs-number">0</span>,
<span class="hljs-attr">"failed"</span>: },
<span class="hljs-attr">"hits"</span>: {
<span class="hljs-attr">"total"</span>: <span class="hljs-number">3</span>,
<span class="hljs-attr">"max_score"</span>: <span class="hljs-number">0.2824934</span>,
<span class="hljs-attr">"hits"</span>: [
{
<span class="hljs-attr">"_index"</span>: <span class="hljs-string">"demo"</span>,
<span class="hljs-attr">"_type"</span>: <span class="hljs-string">"article"</span>,
<span class="hljs-attr">"_id"</span>: <span class="hljs-string">"AWEIQ90700f4t28Wzjdj"</span>,
<span class="hljs-attr">"_score"</span>: <span class="hljs-number">0.2824934</span>,
<span class="hljs-attr">"_source"</span>: {
<span class="hljs-attr">"content"</span>: <span class="hljs-string">"测试语句2"</span>
}
},
{
<span class="hljs-attr">"_index"</span>: <span class="hljs-string">"demo"</span>,
<span class="hljs-attr">"_type"</span>: <span class="hljs-string">"article"</span>,
<span class="hljs-attr">"_id"</span>: <span class="hljs-string">"AWEIQ71f00f4t28WzjZT"</span>,
<span class="hljs-attr">"_score"</span>: <span class="hljs-number">0.21247853</span>,
<span class="hljs-attr">"_source"</span>: {
<span class="hljs-attr">"content"</span>: <span class="hljs-string">"测试语句1"</span>
}
},
{
<span class="hljs-attr">"_index"</span>: <span class="hljs-string">"demo"</span>,
<span class="hljs-attr">"_type"</span>: <span class="hljs-string">"article"</span>,
<span class="hljs-attr">"_id"</span>: <span class="hljs-string">"AWEIRAEw00f4t28Wzjkd"</span>,
<span class="hljs-attr">"_score"</span>: <span class="hljs-number">0.1293895</span>,
<span class="hljs-attr">"_source"</span>: {
<span class="hljs-attr">"content"</span>: <span class="hljs-string">"测试语句3,字段长度不同"</span>
}
}
]
}
}
</code></div></pre>
<p>奇怪的是,按照语句1和语句2的分数居然不同!因为他们两个文档的关键参数,词频,字段长度,逆向文档频率均相同,为什么算出来的分不同呢?</p>
<p>原因主要是因为 每个分片会根据 该分片内的所有文档计算一个本地 IDF 。而文档落在不同的分片就会导致逆向文档频率不同,算出来的分数也不同。</p>
<p>参见官网 被破坏的相关度</p>
<p>当文档数量比较大,分片分布均匀后,这个问题基本不会影响很大。那么在我们这个demo中使用添加 ?search_type=dfs_query_then_fetch来查询所有的idf。</p>
<pre><div class="hljs"><code class="lang-json">/POST {{host}}:{{port}}/demo/article/_search?search_type=dfs_query_then_fetch
{
<span class="hljs-attr">"query"</span>:{
<span class="hljs-attr">"match"</span>:{
<span class="hljs-attr">"content"</span>:<span class="hljs-string">"测"</span>
}
}
}
</code></div></pre>
<p>测试结果:</p>
<pre><div class="hljs"><code class="lang-json">{
<span class="hljs-attr">"took"</span>: <span class="hljs-number">1</span>,
<span class="hljs-attr">"timed_out"</span>: <span class="hljs-literal">false</span>,
<span class="hljs-attr">"_shards"</span>: {
<span class="hljs-attr">"total"</span>: <span class="hljs-number">5</span>,
<span class="hljs-attr">"successful"</span>: <span class="hljs-number">5</span>,
<span class="hljs-attr">"skipped"</span>: <span class="hljs-number">0</span>,
<span class="hljs-attr">"failed"</span>: },
<span class="hljs-attr">"hits"</span>: {
<span class="hljs-attr">"total"</span>: <span class="hljs-number">3</span>,
<span class="hljs-attr">"max_score"</span>: <span class="hljs-number">0.14899126</span>,
<span class="hljs-attr">"hits"</span>: [
{
<span class="hljs-attr">"_index"</span>: <span class="hljs-string">"demo"</span>,
<span class="hljs-attr">"_type"</span>: <span class="hljs-string">"article"</span>,
<span class="hljs-attr">"_id"</span>: <span class="hljs-string">"AWEIQ71f00f4t28WzjZT"</span>,
<span class="hljs-attr">"_score"</span>: <span class="hljs-number">0.14899126</span>,
<span class="hljs-attr">"_source"</span>: {
<span class="hljs-attr">"content"</span>: <span class="hljs-string">"测试语句1"</span>
}
},
{
<span class="hljs-attr">"_index"</span>: <span class="hljs-string">"demo"</span>,
<span class="hljs-attr">"_type"</span>: <span class="hljs-string">"article"</span>,
<span class="hljs-attr">"_id"</span>: <span class="hljs-string">"AWEIQ90700f4t28Wzjdj"</span>,
<span class="hljs-attr">"_score"</span>: <span class="hljs-number">0.14899126</span>,
<span class="hljs-attr">"_source"</span>: {
<span class="hljs-attr">"content"</span>: <span class="hljs-string">"测试语句2"</span>
}
},
{
<span class="hljs-attr">"_index"</span>: <span class="hljs-string">"demo"</span>,
<span class="hljs-attr">"_type"</span>: <span class="hljs-string">"article"</span>,
<span class="hljs-attr">"_id"</span>: <span class="hljs-string">"AWEIRAEw00f4t28Wzjkd"</span>,
<span class="hljs-attr">"_score"</span>: <span class="hljs-number">0.087505676</span>,
<span class="hljs-attr">"_source"</span>: {
<span class="hljs-attr">"content"</span>: <span class="hljs-string">"测试语句3,字段长度不同"</span>
}
}
]
}
}
</code></div></pre>
<p>可以看到,评分如我们所想得,文档1和2分数相同,而文档3因为长度更长,导致分数更低。</p>
<p>继续测试查询时权重的影响</p>
<pre><div class="hljs"><code class="lang-json">/POST {{host}}:{{port}}/demo/article/_search?search_type=dfs_query_then_fetch
{
<span class="hljs-attr">"query"</span>: {
<span class="hljs-attr">"bool"</span>: {
<span class="hljs-attr">"should"</span>: [
{
<span class="hljs-attr">"match"</span>: {
<span class="hljs-attr">"content"</span>: { <span class="hljs-attr">"query"</span>: <span class="hljs-string">"1"</span>, <span class="hljs-attr">"boost"</span>: <span class="hljs-number">2</span> } }
},
{
<span class="hljs-attr">"match"</span>: {
<span class="hljs-attr">"content"</span>: <span class="hljs-string">"2"</span> }
}
]
}
}
}
</code></div></pre>
<p>测试结果:</p>
<pre><div class="hljs"><code class="lang-json">{
<span class="hljs-attr">"took"</span>: <span class="hljs-number">2</span>,
<span class="hljs-attr">"timed_out"</span>: <span class="hljs-literal">false</span>,
<span class="hljs-attr">"_shards"</span>: {
<span class="hljs-attr">"total"</span>: <span class="hljs-number">5</span>,
<span class="hljs-attr">"successful"</span>: <span class="hljs-number">5</span>,
<span class="hljs-attr">"skipped"</span>: <span class="hljs-number">0</span>,
<span class="hljs-attr">"failed"</span>: },
<span class="hljs-attr">"hits"</span>: {
<span class="hljs-attr">"total"</span>: <span class="hljs-number">2</span>,
<span class="hljs-attr">"max_score"</span>: <span class="hljs-number">2.1887734</span>,
<span class="hljs-attr">"hits"</span>: [
{
<span class="hljs-attr">"_index"</span>: <span class="hljs-string">"demo"</span>,
<span class="hljs-attr">"_type"</span>: <span class="hljs-string">"article"</span>,
<span class="hljs-attr">"_id"</span>: <span class="hljs-string">"AWEIQ71f00f4t28WzjZT"</span>,
<span class="hljs-attr">"_score"</span>: <span class="hljs-number">2.1887734</span>,
<span class="hljs-attr">"_source"</span>: {
<span class="hljs-attr">"content"</span>: <span class="hljs-string">"测试语句1"</span>
}
},
{
<span class="hljs-attr">"_index"</span>: <span class="hljs-string">"demo"</span>,
<span class="hljs-attr">"_type"</span>: <span class="hljs-string">"article"</span>,
<span class="hljs-attr">"_id"</span>: <span class="hljs-string">"AWEIQ90700f4t28Wzjdj"</span>,
<span class="hljs-attr">"_score"</span>: <span class="hljs-number">1.0943867</span>,
<span class="hljs-attr">"_source"</span>: {
<span class="hljs-attr">"content"</span>: <span class="hljs-string">"测试语句2"</span>
}
}
]
}
}
</code></div></pre>
<p>可以看到,由于给予搜索关键字1更高的权重,因此文档1的分数比文档2分数要高,具体细节可以通过?explain查看。</p>
<p>其他更改评分的方法<br />
由于其他几个方法官网介绍的比较详尽,所以这里就不多做介绍,直接贴上官网链接。而使用脚本评分,官网介绍有些细节不够完善,因此在此多加介绍:</p>
<p><a href="https://www.elastic.co/guide/cn/elasticsearch/guide/cn/boosting-by-popularity.html" target="_blank">按受欢迎度提升权重</a></p>
<p><a href="https://www.elastic.co/guide/cn/elasticsearch/guide/cn/function-score-filters.html" target="_blank">过滤集提升权重</a></p>
<p><a href="https://www.elastic.co/guide/cn/elasticsearch/guide/cn/random-scoring.html" target="_blank">随机评分</a></p>
<p><a href="https://www.elastic.co/guide/cn/elasticsearch/guide/cn/decay-functions.html" target="_blank">越近越好</a></p>
<p><a href="https://www.elastic.co/guide/cn/elasticsearch/guide/cn/script-score.html" target="_blank">脚本评分</a></p>
<p>脚本评分主要应用在提供的评分满足不了需求,需要通过脚本自定义评分标准。比如虽然提供了前缀分词,但是前缀分词后,返回匹配的结果评分都是1,无法进一步区分。而我们可以通过脚本在使用tf/idf得出分数后,再加上前缀匹配后的额外分值,达到搜索和前缀匹配的目的。</p>
<p>那么在增加一组数据</p>
<pre><div class="hljs"><code class="lang-json"> {
<span class="hljs-attr">"content"</span>:<span class="hljs-string">"语句测试4"</span>
}
</code></div></pre>
<p>继续之前的查询条件:</p>
<pre><div class="hljs"><code class="lang-json"> /POST {{host}}:{{port}}/demo/article/_search?search_type=dfs_query_then_fetch
{
<span class="hljs-attr">"query"</span>:{
<span class="hljs-attr">"match"</span>:{
<span class="hljs-attr">"content"</span>:<span class="hljs-string">"测"</span>
}
}
}
{
<span class="hljs-attr">"took"</span>: <span class="hljs-number">1</span>,
<span class="hljs-attr">"timed_out"</span>: <span class="hljs-literal">false</span>,
<span class="hljs-attr">"_shards"</span>: {
<span class="hljs-attr">"total"</span>: <span class="hljs-number">5</span>,
<span class="hljs-attr">"successful"</span>: <span class="hljs-number">5</span>,
<span class="hljs-attr">"skipped"</span>: <span class="hljs-number">0</span>,
<span class="hljs-attr">"failed"</span>: },
<span class="hljs-attr">"hits"</span>: {
<span class="hljs-attr">"total"</span>: <span class="hljs-number">4</span>,
<span class="hljs-attr">"max_score"</span>: <span class="hljs-number">0.11455677</span>,
<span class="hljs-attr">"hits"</span>: [
{
<span class="hljs-attr">"_index"</span>: <span class="hljs-string">"demo"</span>,
<span class="hljs-attr">"_type"</span>: <span class="hljs-string">"article"</span>,
<span class="hljs-attr">"_id"</span>: <span class="hljs-string">"AWEIQ71f00f4t28WzjZT"</span>,
<span class="hljs-attr">"_score"</span>: <span class="hljs-number">0.11455677</span>,
<span class="hljs-attr">"_source"</span>: {
<span class="hljs-attr">"content"</span>: <span class="hljs-string">"测试语句1"</span>
}
},
{
<span class="hljs-attr">"_index"</span>: <span class="hljs-string">"demo"</span>,
<span class="hljs-attr">"_type"</span>: <span class="hljs-string">"article"</span>,
<span class="hljs-attr">"_id"</span>: <span class="hljs-string">"AWEIQ90700f4t28Wzjdj"</span>,
<span class="hljs-attr">"_score"</span>: <span class="hljs-number">0.11455677</span>,
<span class="hljs-attr">"_source"</span>: {
<span class="hljs-attr">"content"</span>: <span class="hljs-string">"测试语句2"</span>
}
},
{
<span class="hljs-attr">"_index"</span>: <span class="hljs-string">"demo"</span>,
<span class="hljs-attr">"_type"</span>: <span class="hljs-string">"article"</span>,
<span class="hljs-attr">"_id"</span>: <span class="hljs-string">"AWEIaVP000f4t28W0AmE"</span>,
<span class="hljs-attr">"_score"</span>: <span class="hljs-number">0.11455677</span>,
<span class="hljs-attr">"_source"</span>: {
<span class="hljs-attr">"content"</span>: <span class="hljs-string">"语句测试4"</span>
}
},
{
<span class="hljs-attr">"_index"</span>: <span class="hljs-string">"demo"</span>,
<span class="hljs-attr">"_type"</span>: <span class="hljs-string">"article"</span>,
<span class="hljs-attr">"_id"</span>: <span class="hljs-string">"AWEIRAEw00f4t28Wzjkd"</span>,
<span class="hljs-attr">"_score"</span>: <span class="hljs-number">0.065936774</span>,
<span class="hljs-attr">"_source"</span>: {
<span class="hljs-attr">"content"</span>: <span class="hljs-string">"测试语句3,字段长度不同"</span>
}
}
]
}
}
</code></div></pre>
<p>从测试结果中看到,虽然语句4顺序不同,但是根据评分算法,依旧还是同分。</p>
<p>如果想突出前缀匹配的效果呢?</p>
<pre><div class="hljs"><code class="lang-json"> /POST {{host}}:{{port}}/demo/article/_search?search_type=dfs_query_then_fetch
{
<span class="hljs-attr">"query"</span>: {
<span class="hljs-attr">"function_score"</span>: {
<span class="hljs-attr">"query"</span>: {
<span class="hljs-attr">"match"</span>: {
<span class="hljs-attr">"content"</span>: <span class="hljs-string">"测"</span> }
},
<span class="hljs-attr">"script_score"</span>: {
<span class="hljs-attr">"script"</span>: {
<span class="hljs-attr">"lang"</span>: <span class="hljs-string">"painless"</span>, // <span class="hljs-attr">"source"</span>: <span class="hljs-string">"if(doc['content'].value.startsWith(params.keyword))return 1; return 0;"</span>,
<span class="hljs-attr">"params"</span>:{ // 2 <span class="hljs-attr">"keyword"</span>:<span class="hljs-string">"测"</span> } }
},
<span class="hljs-attr">"boost_mode"</span>: <span class="hljs-string">"sum"</span> / }
}
}
</code></div></pre>
<p>虽然和官网的实例代码有所不同,但是这个代码在我的ES 5.6.0上能正常工作。</p>
<ol>
<li>painless是一种新支持的脚本语言,语言格式和java十分类似。可以参考以下文档:</li>
</ol>
<p><a href="https://www.elastic.co/guide/en/elasticsearch/reference/5.6/modules-scripting-painless.html" target="_blank">painless语言介绍</a></p>
<p><a href="https://www.elastic.co/guide/en/elasticsearch/painless/5.6/painless-api-reference.html" target="_blank">painless api</a></p>
<p><a href="https://www.elastic.co/guide/en/elasticsearch/painless/5.6/painless-examples.html" target="_blank">painless 实例</a></p>
<ol start="2">
<li>
<p>脚本参数</p>
</li>
<li>
<p>score_mode计算functions中的分数形式,加减乘除,boost_mode计算最外层的分数形式,加减乘除。所以最后总分是tf/idf分数加上脚本得分。</p>
</li>
</ol>
<p>但是运行结果爆出异常:</p>
<p>“reason”: “Fielddata is disabled on text fields by default. Set fielddata=true on [content] in order to load fielddata in memory by uninverting the inverted index. Note that this can however use significant memory. Alternatively use a keyword field instead.”<br />
主要原因是如果一个可搜索的字段,默认是不能被脚本引用的。如果强行打开,对性能消耗很大,因此不建议这种做法。</p>
<p>参考官方文档 fielddata</p>
<pre><div class="hljs"><code class="lang-json">PUT my_index/_mapping/my_type
{
<span class="hljs-attr">"properties"</span>: {
<span class="hljs-attr">"my_field"</span>: {
<span class="hljs-attr">"type"</span>: <span class="hljs-string">"text"</span>,
<span class="hljs-attr">"fielddata"</span>: <span class="hljs-literal">true</span>
}
}
}
</code></div></pre>
<p>所以建议重新定义索引映射</p>
<pre><div class="hljs"><code class="lang-json">PUT my_index
{
<span class="hljs-attr">"mappings"</span>: {
<span class="hljs-attr">"my_type"</span>: {
<span class="hljs-attr">"properties"</span>: {
<span class="hljs-attr">"my_field"</span>: {
<span class="hljs-attr">"type"</span>: <span class="hljs-string">"text"</span>,
<span class="hljs-attr">"fields"</span>: { <span class="hljs-attr">"keyword"</span>: { <span class="hljs-attr">"type"</span>: <span class="hljs-string">"keyword"</span> } } }
}
}
}
}
</code></div></pre>
<p>再重新输入搜索</p>
<pre><div class="hljs"><code class="lang-json">/POST {{host}}:{{port}}/demo/article/_search?search_type=dfs_query_then_fetch
{
<span class="hljs-attr">"query"</span>: {
<span class="hljs-attr">"function_score"</span>: {
<span class="hljs-attr">"query"</span>: {
<span class="hljs-attr">"match"</span>: {
<span class="hljs-attr">"content"</span>: <span class="hljs-string">"测"</span> }
},
<span class="hljs-attr">"script_score"</span>: {
<span class="hljs-attr">"script"</span>: {
<span class="hljs-attr">"lang"</span>: <span class="hljs-string">"painless"</span>,
<span class="hljs-attr">"source"</span>: <span class="hljs-string">"if(doc['content.keyword'].value.startsWith(params.keyword))return 1; return 0;"</span>, //此处更改为content.keyword
<span class="hljs-attr">"params"</span>:{ <span class="hljs-attr">"keyword"</span>:<span class="hljs-string">"测"</span> } }
},
<span class="hljs-attr">"boost_mode"</span>: <span class="hljs-string">"sum"</span>
}
}
}
</code></div></pre>
<p>查询结果:</p>
<pre><div class="hljs"><code class="lang-json">{
<span class="hljs-attr">"took"</span>: <span class="hljs-number">16</span>,
<span class="hljs-attr">"timed_out"</span>: <span class="hljs-literal">false</span>,
<span class="hljs-attr">"_shards"</span>: {
<span class="hljs-attr">"total"</span>: <span class="hljs-number">5</span>,
<span class="hljs-attr">"successful"</span>: <span class="hljs-number">5</span>,
<span class="hljs-attr">"skipped"</span>: <span class="hljs-number">0</span>,
<span class="hljs-attr">"failed"</span>: },
<span class="hljs-attr">"hits"</span>: {
<span class="hljs-attr">"total"</span>: <span class="hljs-number">4</span>,
<span class="hljs-attr">"max_score"</span>: <span class="hljs-number">1.1145568</span>,
<span class="hljs-attr">"hits"</span>: [
{
<span class="hljs-attr">"_index"</span>: <span class="hljs-string">"demo"</span>,
<span class="hljs-attr">"_type"</span>: <span class="hljs-string">"article"</span>,
<span class="hljs-attr">"_id"</span>: <span class="hljs-string">"AWEIiwUM00f4t28W0cS6"</span>,
<span class="hljs-attr">"_score"</span>: <span class="hljs-number">1.1145568</span>,
<span class="hljs-attr">"_source"</span>: {
<span class="hljs-attr">"content"</span>: <span class="hljs-string">"测试语句1"</span>
}
},
{
<span class="hljs-attr">"_index"</span>: <span class="hljs-string">"demo"</span>,
<span class="hljs-attr">"_type"</span>: <span class="hljs-string">"article"</span>,
<span class="hljs-attr">"_id"</span>: <span class="hljs-string">"AWEIiy7900f4t28W0cc0"</span>,
<span class="hljs-attr">"_score"</span>: <span class="hljs-number">1.1145568</span>,
<span class="hljs-attr">"_source"</span>: {
<span class="hljs-attr">"content"</span>: <span class="hljs-string">"测试语句2"</span>
}
},
{
<span class="hljs-attr">"_index"</span>: <span class="hljs-string">"demo"</span>,
<span class="hljs-attr">"_type"</span>: <span class="hljs-string">"article"</span>,
<span class="hljs-attr">"_id"</span>: <span class="hljs-string">"AWEIi1Bq00f4t28W0cjD"</span>,
<span class="hljs-attr">"_score"</span>: <span class="hljs-number">1.0659368</span>,
<span class="hljs-attr">"_source"</span>: {
<span class="hljs-attr">"content"</span>: <span class="hljs-string">"测试语句3,字段长度不同"</span>
}
},
{
<span class="hljs-attr">"_index"</span>: <span class="hljs-string">"demo"</span>,
<span class="hljs-attr">"_type"</span>: <span class="hljs-string">"article"</span>,
<span class="hljs-attr">"_id"</span>: <span class="hljs-string">"AWEIi2uV00f4t28W0cpI"</span>,
<span class="hljs-attr">"_score"</span>: <span class="hljs-number">0.11455677</span>,
<span class="hljs-attr">"_source"</span>: {
<span class="hljs-attr">"content"</span>: <span class="hljs-string">"语句测试4"</span>
}
}
]
}
}
</code></div></pre>
<p>可以看到,给测开头的语句加了1分,脚本运行成功。</p>