如何在弹性搜索函数得分中提高字段长度范数?

Mar*_*cco 10 elasticsearch boosting

我知道,弹性搜索在计算查询检索到的文档的分数时会考虑字段的长度.场越短,重量越大(参见场长标准).

我喜欢这样的行为:当我搜索iphone我更感兴趣的是iphone 6Crappy accessories for: iphone 5 iphone 5s iphone 6.

现在,我想尝试提升这些东西,让我们说我想要加倍它的重要性.

我知道可以使用功能分数修改分数,我想我可以通过脚本分数达到我想要的效果.

我试图在这个分数中添加另一个字段长度范数:

    {
     "query": {
       "function_score": {
         "boost_mode": "replace",
         "query": {...},
         "script_score": {
             "script": "_score + norm(doc)"
         }
       }
     }
   }
Run Code Online (Sandbox Code Playgroud)

但是我失败了,得到了这个错误: [No parser for element [function_score]]

编辑:

我的第一个错误是我没有将功能分数包装在"查询"中.现在我编辑了上面的代码.我的新错误说

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()]]
Run Code Online (Sandbox Code Playgroud)

编辑:我提供了第一个答案,但我希望有一个更好的答案

rob*_*nst 10

它看起来像你可以做到这一点使用的字段类型token_count有一个共同field_value_factor功能评分.

所以,在字段映射中这样的事情:

"name": { 
  "type": "string",
  "fields": {
    "length": { 
      "type":     "token_count",
      "analyzer": "standard"
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

这将使用该字段中的令牌数.如果要使用字符数,可以将分析器更改为standard用于标记每个字符的自定义分析器.

然后在查询中:

"function_score": {
  ...,
  "field_value_factor": {
    "field": "name.length",
    "modifier": "reciprocal"
  }
}
Run Code Online (Sandbox Code Playgroud)