读《深入理解Elasticsearch》点滴-查询二次评分
理解二次评分
二次评分是指重新计算查询返回文档中指定个数文档的得分,es会截取查询返回的前N个,并使用预定义的二次评分方法来重新计算他们的得分
小结
- 有时候,我们需要显示查询结果,并且使得页面上靠前文档的顺序能受到一些额外的规则控制,但遗憾的是,我们并不能通过二次评分来实现,也许有些读者会想到window-size参数,然而实际上这个参数与返回列表中靠前文档并无关系,他只是制定了每个分片应该返回的文档数,而且window_size不能小于页面大小
- 二次评分功能并不能与排序一起使用,这是因为排序发生在二次评分之前,所以排序没有考虑后续新计算出来的文档得分
- 如果search_type为scan或count,二次评分不会被执行
二次评分参数配置
在resource对象中,必须配置下面的参数:
- window_size 窗口大小,默认值是from和size参数值之和,它指定了每个分片上参与二次评分的文档个数
- query_weight 查询权重,默认值是1,原始查询得分与二次评分的得分相加之前将乘以改值
- rescore_query_weight 二次评分查询的权重值,默认值是1,二次评分查询得分在与原始查询得分相加之前,乘以该值
- rescore_mode 二次评分模式,默认为total,可用的选项有total、max、min、avg和mutiply
- total 得分是两种查询之he
- max 两种查询中的最大值
- min 两种查询中的最小值
- avg 两种查询的平均值
- multiply 两种查询的乘积
以下是官网手册(v5.1)
Rescoring can help to improve precision by reordering just the top (eg 100 - 500) documents returned by the query and post_filter phases, using a secondary (usually more costly) algorithm, instead of applying the costly algorithm to all documents in the index.
A rescore request is executed on each shard before it returns its results to be sorted by the node handling the overall search request.
Currently the rescore API has only one implementation: the query rescorer, which uses a query to tweak the scoring. In the future, alternative rescorers may be made available, for example, a pair-wise rescorer.
the rescore phase is not executed when sort is used.
when exposing pagination to your users, you should not change window_size as you step through each page (by passing different from values) since that can alter the top hits causing results to confusingly shift as the user steps through pages.
Query rescoreredit
The query rescorer executes a second query only on the Top-K results returned by the query and post_filter phases. The number of docs which will be examined on each shard can be controlled by the window_size parameter, which defaults to from and size.
By default the scores from the original query and the rescore query are combined linearly to produce the final _score for each document. The relative importance of the original query and of the rescore query can be controlled with the query_weight and rescore_query_weight respectively. Both default to 1.
For example:
curl -s -XPOST 'localhost:9200/_search' -d '{
"query" : {
"match" : {
"field1" : {
"operator" : "or",
"query" : "the quick brown",
"type" : "boolean"
}
}
},
"rescore" : {
"window_size" : 50,
"query" : {
"rescore_query" : {
"match" : {
"field1" : {
"query" : "the quick brown",
"type" : "phrase",
"slop" : 2
}
}
},
"query_weight" : 0.7,
"rescore_query_weight" : 1.2
}
}
}
'
The way the scores are combined can be controlled with the score_mode:
| Score Mode | Description |
|---|---|
|
|
Add the original score and the rescore query score. The default. |
|
|
Multiply the original score by the rescore query score. Useful for |
|
|
Average the original score and the rescore query score. |
|
|
Take the max of original score and the rescore query score. |
|
|
Take the min of the original score and the rescore query score. |
Multiple Rescoresedit
It is also possible to execute multiple rescores in sequence:
curl -s -XPOST 'localhost:9200/_search' -d '{
"query" : {
"match" : {
"field1" : {
"operator" : "or",
"query" : "the quick brown",
"type" : "boolean"
}
}
},
"rescore" : [ {
"window_size" : 100,
"query" : {
"rescore_query" : {
"match" : {
"field1" : {
"query" : "the quick brown",
"type" : "phrase",
"slop" : 2
}
}
},
"query_weight" : 0.7,
"rescore_query_weight" : 1.2
}
}, {
"window_size" : 10,
"query" : {
"score_mode": "multiply",
"rescore_query" : {
"function_score" : {
"script_score": {
"script": {
"lang": "painless",
"inline": "Math.log10(doc['numeric'].value + 2)"
}
}
}
}
}
} ]
}
'
The first one gets the results of the query then the second one gets the results of the first, etc. The second rescore will "see" the sorting done by the first rescore so it is possible to use a large window on the first rescore to pull documents into a smaller window for the second rescore.
读《深入理解Elasticsearch》点滴-查询二次评分的更多相关文章
- elasticsearch term 查询二:Range Query
Range Query 将文档与具有一定范围内字词的字段进行匹配. Lucene查询的类型取决于字段类型,对于字符串字段,TermRangeQuery,对于数字/日期字段,查询是NumericRang ...
- 深入理解ElasticSearch(PDF版 内含目录)
深入理解ElasticSearch 介绍: 本书涵盖了Elasticsearch的许多中高级功能,并介绍了缓存.ApacheLucene库以及监控等模块的内部运作机制.其中,还涉及一些实用案例,比如配 ...
- Elasticsearch入门教程(六):Elasticsearch查询(二)
原文:Elasticsearch入门教程(六):Elasticsearch查询(二) 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:h ...
- 从查询重写角度理解elasticsearch的高亮原理
一.高亮的一些问题 elasticsearch提供了三种高亮方式,前面我们已经简单的了解了elasticsearch的高亮原理; 高亮处理跟实际使用查询类型有十分紧密的关系,其中主要的一点就是muti ...
- Elasticsearch入门教程(二):Elasticsearch核心概念
原文:Elasticsearch入门教程(二):Elasticsearch核心概念 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:ht ...
- mysql系列:加深对脏读、脏写、可重复读、幻读的理解
关于相关术语的专业解释,请自行百度了解,本文皆本人自己结合参考书和自己的理解所做的阐述,如有不严谨之处,还请多多指教. 事务有四种基本特性,叫ACID,它们分别是: Atomicity-原子性,Con ...
- 《深入理解Elasticsearch》README
书目 <深入理解ElasticSearch>拉斐尔·酷奇,马雷克·罗戈任斯基[著]张世武,余洪森,商旦[译] 机械工业出版社,2016.1 本系列包括以下8篇笔记 第01章 Elastic ...
- elasticsearch 分页查询实现方案——Top K+归并排序
elasticsearch 分页查询实现方案 1. from+size 实现分页 from表示从第几行开始,size表示查询多少条文档.from默认为0,size默认为10,注意:size的大小不能超 ...
- elasticsearch 常见查询及聚合的JAVA API
ES 常见查询 (1)根据ID 进行单个查询 GetResponse response = client.prepareGet("accounts", "person&q ...
随机推荐
- 装饰器修复技术@wraps
@wrap修复技术 首先我先说一下wrap的效果 如果没使用@wraps,当A调用了装饰器B的话,即使A.name,返回的会是装饰器B的函数名称,而不是A的函数名称如果使用了@wraps,当A调用了装 ...
- 【原创】Linux Suspend流程分析
背景 Read the fucking source code! --By 鲁迅 A picture is worth a thousand words. --By 高尔基 说明: Kernel版本: ...
- 2016 ACM-ICPC 青岛站网络赛G题 题解
[参考博客][https://blog.csdn.net/Tawn0000/article/details/82255682] 题意: 将n个数按照每k个一组来合并,合并需要花费的cost是两个数的长 ...
- Jedis操作Redis--String类型
/** * String(字符串) * APPEND,BITCOUNT,BITOP,BITFIELD,DECR,DECRBY,GET,GETBIT,GETRANGE,GETSET,INCR,INCRB ...
- P2774 方格取数问题 网络最大流 割
P2774 方格取数问题:https://www.luogu.org/problemnew/show/P2774 题意: 给定一个矩阵,取出不相邻的数字,使得数字的和最大. 思路: 可以把方格分成两个 ...
- CodeForces 507E Breaking Good 2维权重dij
Breaking Good 题解: 2维权重dij, 先距离最短, 后改变最小. 在这个题中, 如果要改变最小, 则让更多的可用边放进来. 然后可以用pre存下关键边. 代码: ...
- poj2528 Mayor's posters(线段树区间修改+特殊离散化)
Description The citizens of Bytetown, AB, could not stand that the candidates in the mayoral electio ...
- poj 2115 求线性同余方程 C Looooops(好理解欧几里德扩展定理怎么应用)
C Looooops Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 29061 Accepted: 8360 Descr ...
- SpringBoot发送邮箱验证码
一.开启QQ邮箱服务 (1)登陆QQ找到设置,点击账户 (2)往下拉,开启POP3/SMTP服务和IMAP/SMTP服务 当开启IMAP/SMTP会有一串密文密码,保存起来后面要用到 二.spring ...
- Intel X86 32位CPU内存管理----《Linux内核源码情景分析》笔记(一)
Intel X86 32位CPU内存管理 在X86系列中,8086和8088是16为处理器,而从80386开始为32为处理器,80286则是该系列从8088到80386,也就是16位处理器到32位处理 ...