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

前端 未结 2 402
臣服心动
臣服心动 2021-01-11 15:13

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

2条回答
  •  说谎
    说谎 (楼主)
    2021-01-11 15:53

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

提交回复
热议问题