How can I boost the field length norm in elasticsearch function score?

柔情痞子 提交于 2019-12-05 02:42:32

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

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.

Then in the query:

"function_score": {
  ...,
  "field_value_factor": {
    "field": "name.length",
    "modifier": "reciprocal"
  }
}

I have something that kind of works. With the following, I deduct the length of a field of my interest from the score.

{
 "query": {
   "function_score": {
     "boost_mode": "replace",
     "query": {...},
     "script_score": {
         "script": "_score  - doc['<field_name>'].value.length()"
     }
   }
 }
}

Hovever, I cannot control the relative weight of this number I am subtracting, compared to the old score. That's why I am not accepting my answer: I'll wait for better ones for a while. Ideally, I'd love to have a way to access the field length norm function within the script_score, or to get an equivalent result.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!