首页 > 解决方案 > Elasticsearch 对多个索引的结果进行排序,以便一个索引具有优先权

问题描述

我有 6 个网站,我们称它们为 A、B、C、D、E 和 M。M 是主网站,因为您可以从中搜索其他网站的内容,我很容易通过使用逗号分隔所有索引来完成在搜索查询中。

但是我现在有一个新的要求,从每个网站你可以搜索所有网站(很容易做到,将解决方案从 M 应用到所有),但优先考虑当前网站的结果。

所以如果我从 C 中搜索,第一个结果应该来自 C,然后是基于分数的其他结果。

现在,我如何给出一个索引优先于其他索引的结果?

标签: sortingelasticsearchindexing

解决方案


提升查询很好地服务于这个目的:

样本数据

POST /_bulk
{"index":{"_index":"a"}}
{"message":"First website"}
{"index":{"_index":"b"}}
{"message":"Second website"}
{"index":{"_index":"c"}}
{"message":"Third website"}
{"index":{"_index":"d"}}
{"message":"Something irrelevant"}

询问

POST /a,b,c,d/_search
{
  "query": {
    "boosting": {
      "positive": {
        "match": {
          "message": "website"
        }
      },
      "negative": {
        "terms": {
          "_index": ["b", "c", "d"]
        }
      }, 
      "negative_boost": 0.2
    }
  }
}

回复

{
  ...
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : 0.2876821,
    "hits" : [
      {
        "_index" : "a",
        "_type" : "_doc",
        "_id" : "sx-DkWsBHWmGEbsYwViS",
        "_score" : 0.2876821,
        "_source" : {
          "message" : "First website"
        }
      },
      {
        "_index" : "b",
        "_type" : "_doc",
        "_id" : "tB-DkWsBHWmGEbsYwViS",
        "_score" : 0.05753642,
        "_source" : {
          "message" : "Second website"
        }
      },
      {
        "_index" : "c",
        "_type" : "_doc",
        "_id" : "tR-DkWsBHWmGEbsYwViS",
        "_score" : 0.05753642,
        "_source" : {
          "message" : "Third website"
        }
      }
    ]
  }
}

笔记

  1. 越小negative_boost,“活跃指数”的结果就越有可能胜过其他指数
  2. 如果将 设置negative_boost0,您将保证“活动站点”结果首先排序,但您将丢弃所有其他站点的所有分数,因此剩余的排序将是任意的。

我认为类似negative_boost: 0.1,这是对相关性的数量级调整,应该可以为您提供所需的内容。


推荐阅读