gpt4 book ai didi

elasticsearch - 如何将简单的Groovy脚本转换为Lucene表达(或使用其他方法)?

转载 作者:行者123 更新时间:2023-12-02 23:37:35 25 4
gpt4 key购买 nike

我正在使用以下选项进行搜索:

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"
}
}
]
}

关于elasticsearch - 如何将简单的Groovy脚本转换为Lucene表达(或使用其他方法)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28635606/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com