For Elasticsearch, the default is to use the _score field calculated by BM25 to sort in descending order. When we need to sort in descending or ascending order with other fields, we can use the sort field and pass in the desired sort field and method. Elasticsearch provides a DSL custom score for function_score, which allows you to sort by your own _score.

1. First example

Start by creating a new index test_ratings.

{
  "test_ratings_v1" : {
    "aliases" : {
      "test_ratings": {}},"mappings" : {
      "doc" : {
        "properties" : {
          "comment" : {
            "type" : "text"."analyzer" : "ik_smart"
          },
          "create" : {
            "type" : "date"
          },
          "id" : {
            "type" : "integer"
          },
          "productId" : {
            "type" : "integer"
          },
          "rating" : {
            "type" : "integer"
          },
          "test" : {
            "type" : "text"."fields" : {
              "keyword" : {
                "type" : "keyword"."ignore_above" : 256}}},"title" : {
            "type" : "text"."analyzer" : "ik_max_word"
          },
          "userId" : {
            "type" : "integer"}}}},"settings" : {
      "index" : {
        "refresh_interval" : "1s"."number_of_shards" : "3"."provided_name" : "test_ratings_v1"."max_result_window" : "10000000"."creation_date" : "1569227588095"."number_of_replicas" : "0"."uuid" : "cx7XVhITQnaBlqKtauZZow"."version" : {
          "created" : "6080199"
        }
      }
    }
  }
}
Copy the code

Then import some data. A basic request looks like this:

  • The DSLS inside the query wrapped in function_score are used just like the query conditions we normally use.
  • Field_value_factor specifies the _score calculated by the query query multiplied by the value of the rating field
  • Missing indicates that if the value of rating is true, use 1 as the default value

_score = _score * rating(default=1)

GET /test_ratings/_search {" query": {" function_score": {" query": {"match": {" title": "like"}}, "field_value_factor": { "field": "rating", "missing": 1 } }} }Copy the code

2.field_value_factor

Now that we’ve outlined the field_value_factor in the example above, let’s look at some of the parameters in field_value_factor

2.1 the field

The multiplied field, which must be of numeric type.

2.2 factor

The coefficient of multiplication, you can adjust the coefficient of multiplication

2.3 missing

Define field defaults

2.4 modifier

_score = _score = _score = _score = _score = _score

options meaning
none The default value
log log(x)
log1p log(1+x)
log2p log(2+x)
ln ln(x)
ln1p ln(1+x)
ln2p ln(2+x)
square x^x
sqrt The square root of x
reciprocal The reciprocal of the x
When both factor and modifier are used, factor takes precedence over modifier
In the case of log, the final score will be _score = _score * log(factor * x)
Log is used to correct _score when the difference between a score of 0 and 1 is greater than the difference between a score of 4 and a score of 5.
` ` `
GET /test_ratings/_search
{
“query”: {
"Function_score" : {" query ": {" match" : {" title ":" like "}}, "field_value_factor" : {" field ":" rating ", "missing" : 0, "modifier": "log1p" }Copy the code

}}}

When the default value is 0, using log generates an error, so this can be corrected using log1p. You can customize sorting functions using Painless scripts provided by Elasticsearch when simple weights in field_value_factor and functions are not enough. 1. Painless [https://www.elastic.co/guide/en/elasticsearch/painless/6.8/painless-examples.html] (HTTP: / / https://www.elastic.co/guide/en/elas Ticsearch/painless / 6.8 / painless - examples. HTML) a simple usage is as follows:Copy the code

GET/test_ratings / _search {” query “: {” function_score” : {” query “: {” match” : {” title “:” like “}}, “boost_mode” : “replace”, “script_score”: { “script”:{ “params”: { “a”:1, “b”:2 }, “source”: “_score + params.b * doc[‘rating’].value” } } }} }

Doc ['f'] = doc['f'] Using random_score allows different people to get different sorting results, and the same person to get the same sorting results, as follows:Copy the code

GET/test_ratings / _search {” query “: {” function_score” : {” query “: {” match” : {” title “:” like “}},

"random_score": {
    "seed": 1,
    "field": "userId"
},
"boost_mode": "replace"
Copy the code

}}}

## 4.1 seed Specifies a random seed. The same seed will return the same sort. Each seed will generate a random number between 0 and 1 for each document. For the same shard of the same field value, generate the same random number, so when using, try to select different values of the field. In the example above, each doc is multiplied by the same coefficient, sometimes we need to give different weights to different doc's. In this case, using functions is a good choice. The basic usage is as follows:Copy the code

GET /test_ratings/_search {“query”: {“function_score”: {“query”: {“match”: {“title”: “like”}}, “functions”: [{ “filter”: { “term”: { “userId”: 209983 } }, “weight”: 5 }, { “filter”: { “term”: { “rating”: 5 } }, “weight”: 2 } ], “boost”: 2, “score_mode”: “max”, “boost_mode”: “multiply”, “min_score”: 18, “max_boost”: 4 } } }

## 5.1 filter represents each filter with a different score. ## 5.2 weight indicates the corresponding weight. For search recall results, userID =209983 doc, weight = 5, * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * Score_mode = score_mode = score_mode = score_mode = score_mode = score_mode = score_mode = score_mode Meaning options | | | | : -- -- -- -- -- -- -- - | -- -- -- -- -- -- -- -- : | | multiply | functions provides within each weight multiplied | | sum | functions provides each weight in addition | | avg | functions provides the weight averaging | | first | functions provides the first weight values within the | | | Max functions provides the biggest weight | | min | functions provides the minimum min | # # 6.2 Max_boost Limit on score computed by functions, representing the maximum value returned by functions. Apply other scenarios such as field_value_factor # 6.3 Boost on functions to compute the results and then multiply the coefficients. Boost_mode Calculation between the product of boost and functions results and the original _score. In other scenarios, such as field_value_factor, the value can be: Meaning options | | | | : -- -- -- -- -- -- -- - | -- -- -- -- -- -- -- -- : | | multiply results and original _score multiplication | | | the replace original _score | are replaced with the calculation results | | sum results and original _score addition | | | avg | results and original _score averaging | | Max maximum results and original _score | o | | min results and original _score minimum | | o # # 6.5 min_score limit finally recalled the lowest score # # 6.6 weight You can also use weight externally, multiplying each document by a coefficient. Sometimes we need the highest score for a field equal to a certain value, and then decrease it either side or side, such as geographic location or price range. This is where you can use the decay function. The basic usage is as follows:Copy the code

{” query “: {” function_score” : {” query “: {” match” : {” title “:” like “}}, “gauss” : {” rating “: {” origin” : “2”, “offset” : “1”, “scale” : “0.5”, “decay” : “0.5”}}, “boost_mode” : “replace”}}}

The rating is the field name, gauss attenuation function name is # # 7.1 fiend internal parameters meaning options | | | | : -- -- -- -- -- -- -- - | -- -- -- -- -- -- -- -- : | | origin | best value position, The position has a score of 1 | | offset | the scope, on both sides of the optimal value in (origin - offset, origin + offset) within the scope of maximum score is: 1 | | scale | attenuation distance, scale from the offset and then out to the attenuation to the specified value of decay | | decay | specified attenuation values, arrived at the distance of the scal, attenuation to decay | # # 7.2 attenuation function Currently supports three attenuation function: Gauss, EXP, linear attenuation functions and parameters are shown in the figure below. Reference [https://www.elastic.co/guide/en/elasticsearch/reference/6.8/query-dsl-function-score-query.html#function-random] (HTTP S: / / www.elastic.co/guide/en/elasticsearch/reference/6.8/query-dsl-function-score-query.html#function-random)! Attenuation function diagram] [(https://p1-jj.byteimg.com/tos-cn-i-t2oaga2asx/gold-user-assets/2019/12/17/16f147c969366391~tplv-t2oaga2asx-ima Ge.image) ## 7.3 Multi-values fields (multi_value_mode) {ge.image) ## 7.3 Multi-values fields (multi_value_mode); Meaning options | | | | : -- -- -- -- -- -- -- - | -- -- -- -- -- -- -- -- : | | min | within the field to the origin of the minimum distance | | | Max within the field to a maximum distance of origin | | avg | | array in the field to the origin of the average distance | sum | | to the sum of the distance to the origin in the field array # 8. The function_score function of Elasticsearch allows you to sort search results flexibly. However, script_score should be used as little as possible, because script_score performance will be affected. More exciting content, please pay attention to the public number! [](https://p9-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/8eaba87dd484432299fa93a4d778f5eb~tplv-k3u1fbpfcp-zoom-1.image)Copy the code