我使用以下选项进行搜索:
scriptSort =
_script:
script: "if(doc['user.roles'].value=='contributor') return 1; else return 2;",
type: "number",
order: "asc"
options =
query: ...
size: ...
from: ...
aggs: ...
sort: [scriptSort]
如您所见,我正在使用_script
选项对结果进行排序。问题是,我使用的搜索服务放弃了对groovy
脚本语言的支持,我不得不以某种方式将这个脚本重写为Lucene expressions
。
这只是一种尝试,但应该是一种非常通用的方法。使用function_score
定义您自己的过滤器,根据user.roles
字段的值对其进行不同的评级。在我的示例中,我认为应该将"match_all": {}
替换为query
下的任何内容(这就是我询问完整查询的原因):
{
"query": {
"function_score": {
"query": {
"match_all": {}
},
"functions": [
{
"filter": {
"term": {
"user.roles": "contributor"
}
},
"weight": 1
},
{
"filter": {
"bool": {
"must_not": [
{
"term": {
"user.roles": "contributor"
}
}
]
}
},
"weight": 2
}
],
"boost_mode": "replace"
}
},
"sort": [
{
"_score": {
"order": "asc"
}
}
]
}