如何提高Elasticsearch函数得分中的字段长度范数? [英] How can I boost the field length norm in elasticsearch function score?
问题描述
我知道,弹性搜索在计算查询检索的文档的分数时会考虑字段的长度。字段越短,权重越高(请参见字段长度范数)。
I know that elasticsearch takes in account the length of a field when computing the score of the documents retrieved by a query. The shorter the field, the higher the weight (see The field-length norm).
我喜欢这种行为:当我搜索 iphone
时,我对更加感兴趣iphone 6
比中的废话配件:iphone 5 iphone 5s iphone 6
。
I like this behaviour: when I search for iphone
I am much more interested in iphone 6
than in Crappy accessories for: iphone 5 iphone 5s iphone 6
.
现在,我想尝试增强这些功能,比方说,我想使其重要性加倍。
Now, I would like to try to boost this stuff, let's say that I want to double its importance.
I know that one can modify the score using the function score, and I guess that I can achieve what I want via script score.
我试图向得分添加另一个字段长度范数,如下所示:
I tried to add another field-length norm to the score like this:
{
"query": {
"function_score": {
"boost_mode": "replace",
"query": {...},
"script_score": {
"script": "_score + norm(doc)"
}
}
}
}
但是我失败很严重,得到了这个错误: [元素[function_score]没有解析器]
But I failed badly, getting this error: [No parser for element [function_score]]
编辑:
我的第一个错误是我没有包装函数分数一个问题。现在,我编辑了上面的代码。我的新错误是
My first error was that I hadn't wrapped the function score in a "query". Now I edited the code above. My new error says
GroovyScriptExecutionException[MissingMethodException
[No signature of method: Script5.norm() is applicable for argument types:
(org.elasticsearch.search.lookup.DocLookup) values:
[<org.elasticsearch.search.lookup.DocLookup@2c935f6f>]
Possible solutions: notify(), wait(), run(), run(), dump(), any()]]
编辑:我提供了第一个答案,但我希望有一个更好的答案
I provided a first answer, but I'm hoping for a better one
推荐答案
看来您可以使用类型 token_count
以及 field_value_factor
函数得分。
It looks like you could achieve that using a field of type token_count
together with a field_value_factor
function score.
因此,在字段映射中类似这样:
So, something like this in the field mapping:
"name": {
"type": "string",
"fields": {
"length": {
"type": "token_count",
"analyzer": "standard"
}
}
}
这将使用字段中的令牌数。如果要使用字符数,可以将分析器从 standard
更改为标记每个字符的自定义字符。
This will use the number of tokens in the field. If you want to use the number of characters, you can change the analyzer from standard
to a custom one that tokenizes each character.
然后在查询中:
"function_score": {
...,
"field_value_factor": {
"field": "name.length",
"modifier": "reciprocal"
}
}
这篇关于如何提高Elasticsearch函数得分中的字段长度范数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!