理解二次评分

二次评分是指重新计算查询返回文档中指定个数文档的得分,es会截取查询返回的前N个,并使用预定义的二次评分方法来重新计算他们的得分

小结

  1. 有时候,我们需要显示查询结果,并且使得页面上靠前文档的顺序能受到一些额外的规则控制,但遗憾的是,我们并不能通过二次评分来实现,也许有些读者会想到window-size参数,然而实际上这个参数与返回列表中靠前文档并无关系,他只是制定了每个分片应该返回的文档数,而且window_size不能小于页面大小
  2. 二次评分功能并不能与排序一起使用,这是因为排序发生在二次评分之前,所以排序没有考虑后续新计算出来的文档得分
  3. 如果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

total

Add the original score and the rescore query score.  The default.

multiply

Multiply the original score by the rescore query score.  Useful for function query rescores.

avg

Average the original score and the rescore query score.

max

Take the max of original score and the rescore query score.

min

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》点滴-查询二次评分的更多相关文章

  1. elasticsearch term 查询二:Range Query

    Range Query 将文档与具有一定范围内字词的字段进行匹配. Lucene查询的类型取决于字段类型,对于字符串字段,TermRangeQuery,对于数字/日期字段,查询是NumericRang ...

  2. 深入理解ElasticSearch(PDF版 内含目录)

    深入理解ElasticSearch 介绍: 本书涵盖了Elasticsearch的许多中高级功能,并介绍了缓存.ApacheLucene库以及监控等模块的内部运作机制.其中,还涉及一些实用案例,比如配 ...

  3. Elasticsearch入门教程(六):Elasticsearch查询(二)

    原文:Elasticsearch入门教程(六):Elasticsearch查询(二) 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:h ...

  4. 从查询重写角度理解elasticsearch的高亮原理

    一.高亮的一些问题 elasticsearch提供了三种高亮方式,前面我们已经简单的了解了elasticsearch的高亮原理; 高亮处理跟实际使用查询类型有十分紧密的关系,其中主要的一点就是muti ...

  5. Elasticsearch入门教程(二):Elasticsearch核心概念

    原文:Elasticsearch入门教程(二):Elasticsearch核心概念 版权声明:本文为博主原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明. 本文链接:ht ...

  6. mysql系列:加深对脏读、脏写、可重复读、幻读的理解

    关于相关术语的专业解释,请自行百度了解,本文皆本人自己结合参考书和自己的理解所做的阐述,如有不严谨之处,还请多多指教. 事务有四种基本特性,叫ACID,它们分别是: Atomicity-原子性,Con ...

  7. 《深入理解Elasticsearch》README

    书目 <深入理解ElasticSearch>拉斐尔·酷奇,马雷克·罗戈任斯基[著]张世武,余洪森,商旦[译] 机械工业出版社,2016.1 本系列包括以下8篇笔记 第01章 Elastic ...

  8. elasticsearch 分页查询实现方案——Top K+归并排序

    elasticsearch 分页查询实现方案 1. from+size 实现分页 from表示从第几行开始,size表示查询多少条文档.from默认为0,size默认为10,注意:size的大小不能超 ...

  9. elasticsearch 常见查询及聚合的JAVA API

    ES 常见查询 (1)根据ID 进行单个查询 GetResponse response = client.prepareGet("accounts", "person&q ...

随机推荐

  1. unity之截屏功能

    1.全屏截图 方法一:在unity的API中,unity给我们提供了一个现成的API  :  Application.CaptureScreenshot(imagename). 但是这个API虽然简单 ...

  2. JDBC工具类连接数据库,模仿登录

    ## 使用JDBC工具类的原因在使用JDBC连接数据库的时候,加载驱动.获取连接.释放资源等代码是重复的,所有为了提高代码的复用性,我们可以写一个工具类,将数据库驱动加载.获取连接.资源释放的代码封装 ...

  3. Python 命令行之旅:使用 argparse 实现 git 命令

    作者:HelloGitHub-Prodesire HelloGitHub 的<讲解开源项目>系列,项目地址:https://github.com/HelloGitHub-Team/Arti ...

  4. .net core api服务端跨域配置

    第1步:添加包引用(.net core 2.2 已自带此包,可跳过此步骤) Install-Package Microsoft.AspNetCore.Cors 第2步:在Startup.cs文件的Co ...

  5. springboot-vue前后端分离session过期重新登录

    springboot-vue前后端分离session过期重新登录 简单回顾cookie和session cookie和session都是回话管理的方式 Cookie cookie是浏览器端存储信息的一 ...

  6. HDU-6437 Videos

    HDU-6437 题意:一天有n个小时,现在有m场电影,每场电影有一个愉悦值,有k个人,电影分2种类型A, B, 并且每一场电影只能一个人看, 一个人可以看无数次电影, 只要时间足够, 但是连续看同一 ...

  7. 天梯杯 L2-010. 排座位

    L2-010. 排座位 时间限制 150 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 陈越 布置宴席最微妙的事情,就是给前来参宴的各位宾客安排座位. ...

  8. codeforces 764 C. Timofey and a tree(dfs+思维)

    题目链接:http://codeforces.com/contest/764/problem/C 题意:给出一个树,然后各个节点有对应的颜色,问是否存在以一个点为根节点子树的颜色都一样. 这里的子树颜 ...

  9. 一篇文章看懂JS执行上下文

     壹 ❀ 引 我们都知道,JS代码的执行顺序总是与代码先后顺序有所差异,当先抛开异步问题你会发现就算是同步代码,它的执行也与你的预期不一致,比如: function f1() { console.lo ...

  10. 多级树的深度遍历与广度遍历(Java实现)

    目录 多级树的深度遍历与广度遍历 节点模型 深度优先遍历 广度优先遍历 多级树的深度遍历与广度遍历 深度优先遍历与广度优先遍历其实是属于图算法的一种,多级树可以看做是一种特殊的图,所以多级数的深/广遍 ...