sorting - Elasticsearch 对多个索引的结果进行排序,以便一个索引具有优先权
问题描述
我有 6 个网站,我们称它们为 A、B、C、D、E 和 M。M 是主网站,因为您可以从中搜索其他网站的内容,我很容易通过使用逗号分隔所有索引来完成在搜索查询中。
但是我现在有一个新的要求,从每个网站你可以搜索所有网站(很容易做到,将解决方案从 M 应用到所有),但优先考虑当前网站的结果。
所以如果我从 C 中搜索,第一个结果应该来自 C,然后是基于分数的其他结果。
现在,我如何给出一个索引优先于其他索引的结果?
解决方案
提升查询很好地服务于这个目的:
样本数据
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"
}
}
]
}
}
笔记
- 越小
negative_boost
,“活跃指数”的结果就越有可能胜过其他指数 - 如果将 设置
negative_boost
为0
,您将保证“活动站点”结果首先排序,但您将丢弃所有其他站点的所有分数,因此剩余的排序将是任意的。
我认为类似negative_boost: 0.1
,这是对相关性的数量级调整,应该可以为您提供所需的内容。
推荐阅读
- google-docs-api - Google 趋势数据中使用的时区是什么?
- sqlite - 带有 React Native 错误的 SQLite 'table xxxx has no column yyyy'
- html - Vue输入下拉简单
- php - date_default_timezone_set() 损坏的时区
- css - 角度错误:PostCSS 收到未定义而不是 CSS 字符串
- r - 如何让 R 中的 table(·) 以因子顺序而不是字母顺序输出列名?
- bash - 最近更新到 windows 10,刚刚下载了最新版本的 git 但是 git-bash 不会在我的 windows 10 上运行
- node.js - Why does using res.writeHead in NextJS cause a permanent redirect?
- reactjs - 带有下一个 js 的 I18next 仅在家庭中无法用于生产
- html - onClick 总是返回 25