我有以下 bool(boolean) 查询:
POST _search
{
"query": {
"bool" : {
"must" : {
"term" : { "title" : "technology" }
},
"should" : [
{ "term" : { "text" : "companies that produce hardware" } }
]
}
}
}
假设这将返回以下文档ID(按顺序):
1. netflix
2. google
3. microsoft
4. dell
5. intel
在此查询发生之前,我查询一个单独的数据库并获得以下结果(按顺序):
1. intel
2. amazon
3. dell
我想将数据库的结果优先于elasticsearch的结果,以便获得以下结果:
1. intel
2. dell
3. netflix
4. google
5. microsoft
(请注意,由于我们不是在凭空创建 flex 搜索文档,因此不会显示亚马逊)。
最佳答案
您可以使用script based sorting,它允许您基于自定义脚本对数据进行排序。
使用为每个结果分配优先级值的脚本。
可能是这样的:
{
"query": {
"bool" : {
"must" : {
"term" : { "title" : "technology" }
},
"should" : [
{ "term" : { "text" : "companies that produce hardware" } }
]
}
},
"sort": {
"_script": {
"type": "number",
"order": "asc",
"script": {
"lang": "painless",
"source": "if (doc['company'].value.equals('intel')) return 0;
else if (doc['company'].value.equals('amazon')) return 1;
else if (doc['company'].value.equals('dell')) return 2;
else return 3;
}
}
}
}
我使用了company
字段,因为不鼓励对'_id'字段进行排序,即使有可能。 checkout this link。有关更多信息,请检查Sorting,Scripting和Painless。
希望能帮助到你!
关于elasticsearch - 如何确定我传递的结果的优先级?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48014828/