我如何确定传递的结果的优先级?
How do I prioritize results that I pass in?
我有以下布尔查询:
POST _search
{
"query": {
"bool" : {
"must" : {
"term" : { "title" : "technology" }
},
"should" : [
{ "term" : { "text" : "companies that produce hardware" } }
]
}
}
}
假设此 returns 以下文档 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
(注意 amazon 没有出现,因为我们不是想凭空创建一个 elasticsearch 文档)。
您可以使用 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 and Painless。
希望对您有所帮助!
我有以下布尔查询:
POST _search
{
"query": {
"bool" : {
"must" : {
"term" : { "title" : "technology" }
},
"should" : [
{ "term" : { "text" : "companies that produce hardware" } }
]
}
}
}
假设此 returns 以下文档 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
(注意 amazon 没有出现,因为我们不是想凭空创建一个 elasticsearch 文档)。
您可以使用 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 and Painless。
希望对您有所帮助!