我有以下bool查询:
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
我希望将数据库的结果优先于弹性搜索的结果,以便获得以下结果:
1. intel
2. dell
3. netflix
4. google
5. microsoft
(请注意,亚马逊并没有出现,因为我们并未尝试凭空创建一个弹性搜索文档。)
答案 0 :(得分:2)
您可以使用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'字段进行排序,即使它是可能的。查看this link。
有关详细信息,请查看Sorting,Scripting和Painless。
希望它有所帮助!