根据上下文动态地对文档进行评分是很常见的。 例如,如果你需要对某个类别内的更多文档进行评分,经典方案是提升(给低分的文档提分)基于某个值的文档,例如页面排名、点击量或类别。Elasticsearch 提供了两种基于值提高分数的新方法。 一个是 rank feature 字段,另一个是它的扩展,即使用值向量。
根据 rank_feature 或 rank_features 字段的数值提高文档的相关性分数。
rank_feature 查询通常用在 bool 查询的 should 子句中,因此它的相关性分数被添加到 bool 查询的其他分数中。
将 rank_feature 或 rank_features 字段的 positive_score_impact 设置为 false,我们建议参与查询的每个文档都有该字段的值。 否则,如果在 should 子句中使用了 rank_feature 查询,它不会对具有缺失值的文档的分数添加任何内容(分数不变),但会为包含特征的文档添加一些提升(分数提高)。 这与我们想要的相反 — 因为我们认为这些特征是负面的,这是因为当 positive_score_impact 为 false 时,值越大则表示相关性越低;值越小,则表示相关性越高。我们希望将包含它们的文档排名低于缺少它们的文档。
与 function_score 查询或其他更改相关分数的方法不同,rank_feature 查询在 track_total_hits 参数不为真时有效地跳过非竞争性命中。 这可以显着提高查询速度。
Talk is cheap, give me the code!
要使用 rank_feature 查询,您的索引必须包含 rank_feature 或 rank_features 字段映射。 要了解如何为 rank_feature 查询设置索引,请尝试以下示例。
使用以下字段映射创建 test 索引:
- PUT /test
- {
- "mappings": {
- "properties": {
- "pagerank": {
- "type": "rank_feature"
- },
- "url_length": {
- "type": "rank_feature",
- "positive_score_impact": false
- },
- "topics": {
- "type": "rank_features"
- }
- }
- }
- }
将几个文档索引到 test 索引:
- PUT /test/_doc/1?refresh
- {
- "url": "https://en.wikipedia.org/wiki/2016_Summer_Olympics",
- "content": "Rio 2016",
- "pagerank": 50.3,
- "url_length": 42,
- "topics": {
- "sports": 50,
- "brazil": 30
- }
- }
- PUT /test/_doc/2?refresh
- {
- "url": "https://en.wikipedia.org/wiki/2016_Brazilian_Grand_Prix",
- "content": "Formula One motor race held on 13 November 2016",
- "pagerank": 50.3,
- "url_length": 47,
- "topics": {
- "sports": 35,
- "formula one": 65,
- "brazil": 20
- }
- }
- PUT /test/_doc/3?refresh
- {
- "url": "https://en.wikipedia.org/wiki/Deadpool_(film)",
- "content": "Deadpool is a 2016 American superhero film",
- "pagerank": 50.3,
- "url_length": 37,
- "topics": {
- "movies": 60,
- "super hero": 65
- }
- }
以下查询搜索 2016 年并根据 pagerank、url_length 和 sports 主题提高相关性分数。
- GET /test/_search?filter_path=**.hits
- {
- "query": {
- "bool": {
- "must": [
- {
- "match": {
- "content": "2016"
- }
- }
- ],
- "should": [
- {
- "rank_feature": {
- "field": "pagerank"
- }
- },
- {
- "rank_feature": {
- "field": "url_length",
- "boost": 0.1
- }
- },
- {
- "rank_feature": {
- "field": "topics.sports",
- "boost": 0.4
- }
- }
- ]
- }
- }
- }
上面的查询是这样的:
上面查询的结果为:
- {
- "hits" : {
- "hits" : [
- {
- "_index" : "test",
- "_id" : "1",
- "_score" : 0.9496303,
- "_source" : {
- "url" : "https://en.wikipedia.org/wiki/2016_Summer_Olympics",
- "content" : "Rio 2016",
- "pagerank" : 50.3,
- "url_length" : 42,
- "topics" : {
- "sports" : 50,
- "brazil" : 30
- }
- }
- },
- {
- "_index" : "test",
- "_id" : "2",
- "_score" : 0.838465,
- "_source" : {
- "url" : "https://en.wikipedia.org/wiki/2016_Brazilian_Grand_Prix",
- "content" : "Formula One motor race held on 13 November 2016",
- "pagerank" : 50.3,
- "url_length" : 47,
- "topics" : {
- "sports" : 35,
- "formula one" : 65,
- "brazil" : 20
- }
- }
- },
- {
- "_index" : "test",
- "_id" : "3",
- "_score" : 0.6779422,
- "_source" : {
- "url" : "https://en.wikipedia.org/wiki/Deadpool_(film)",
- "content" : "Deadpool is a 2016 American superhero film",
- "pagerank" : 50.3,
- "url_length" : 37,
- "topics" : {
- "movies" : 60,
- "super hero" : 65
- }
- }
- }
- ]
- }
- }
在有些场景里,比如抖音搜索里,就可以用到。如果一个用户的画像比较明确,比如该用户喜欢 sports 或 music,那么匹配那些含有这些标签的主播。
我们也可以做如下的查询:
- GET test/_search?filter_path=**.hits
- {
- "query": {
- "rank_feature": {
- "field": "pagerank"
- }
- }
- }
上面返回的结果为:
- {
- "hits" : {
- "hits" : [
- {
- "_index" : "test",
- "_id" : "1",
- "_score" : 0.5,
- "_source" : {
- "url" : "https://en.wikipedia.org/wiki/2016_Summer_Olympics",
- "content" : "Rio 2016",
- "pagerank" : 50.3,
- "url_length" : 42,
- "topics" : {
- "sports" : 50,
- "brazil" : 30
- }
- }
- },
- {
- "_index" : "test",
- "_id" : "2",
- "_score" : 0.5,
- "_source" : {
- "url" : "https://en.wikipedia.org/wiki/2016_Brazilian_Grand_Prix",
- "content" : "Formula One motor race held on 13 November 2016",
- "pagerank" : 50.3,
- "url_length" : 47,
- "topics" : {
- "sports" : 35,
- "formula one" : 65,
- "brazil" : 20
- }
- }
- },
- {
- "_index" : "test",
- "_id" : "3",
- "_score" : 0.5,
- "_source" : {
- "url" : "https://en.wikipedia.org/wiki/Deadpool_(film)",
- "content" : "Deadpool is a 2016 American superhero film",
- "pagerank" : 50.3,
- "url_length" : 37,
- "topics" : {
- "movies" : 60,
- "super hero" : 65
- }
- }
- }
- ]
- }
- }
我们还可以做如下的查询:
- GET test/_search?filter_path=**.hits
- {
- "query": {
- "rank_feature": {
- "field": "topics.sports"
- }
- }
- }
上面返回的结果为:
- {
- "hits" : {
- "hits" : [
- {
- "_index" : "test",
- "_id" : "1",
- "_score" : 0.5405406,
- "_source" : {
- "url" : "https://en.wikipedia.org/wiki/2016_Summer_Olympics",
- "content" : "Rio 2016",
- "pagerank" : 50.3,
- "url_length" : 42,
- "topics" : {
- "sports" : 50,
- "brazil" : 30
- }
- }
- },
- {
- "_index" : "test",
- "_id" : "2",
- "_score" : 0.4516129,
- "_source" : {
- "url" : "https://en.wikipedia.org/wiki/2016_Brazilian_Grand_Prix",
- "content" : "Formula One motor race held on 13 November 2016",
- "pagerank" : 50.3,
- "url_length" : 47,
- "topics" : {
- "sports" : 35,
- "formula one" : 65,
- "brazil" : 20
- }
- }
- }
- ]
- }
- }
rank_feature 和 rank_features 是用于存储值的特殊类型字段,主要用于对结果进行评分。rank_feature 和 rank_features 中的值只能是单个正值(不允许有多个值)。 在 rank_features 的情况下,值必须是一个 hash 值,由一个字符串和一个正数值组成。
有一个标志可以改变评分的行为 — positive_score_impact。 该值默认为 true,但如果你希望该特征的值降低分数,你可以将其设置为 false。 在 pagerank 示例中,url 的长度会降低文档的分数,因为 url 越长,它的相关性就越低。
参考:
【1】 Rank feature query | Elasticsearch Guide [8.2] | Elastic