我有兴趣在 Elasticsearch 2.1 中找到以下评分问题的最快解决方案
我的文档中有几个类似的字段,weight_1, ..., weight_N
并且decay_1, ..., decay_N
. 我也提前知道参数decay_1, ..., decay_N
和scale_1, ..., scale_N
. 我希望将以下内容计算为文档分数:
SUM(weight_i * 0.5**((decay_i - origin_i) / scale_i), i=1..N)
我会N
提前知道——所以我会确切地知道需要对多少字段求和,并且评分解决方案不需要处理动态数量的字段。
这很容易做到N = 1
:
{
"query": {
"function_score": {
"functions": [
{
"exp": {
"decay_1": {
"origin": "origin_1",
"scale": "scale_1",
"decay": "0.5"
}
}
},
{
"field_value_factor": {
"field": "weight_1",
"missing": 0
}
}
]
}
}
}
当然,我可以使用 Lucene 表达式(因为我N
提前知道)或使用本机 Java 脚本或 Groovy 脚本来完成此操作。但我对性能最高的解决方案感兴趣,这似乎通常意味着尽可能使用内置函数。