Built-in functions under the function score query

The following are the built-in functions available to be used with the function_score query:

  • The weight function
  • The field_value_factor function
  • The script_score function
  • Decay functions - linear, exp, gauss

Let's look at them one by one.

The weight function

The weight function allows you to apply a simple boost to each document without the boost being normalized: a weight of 2 results in 2 * _score. For example:

curl -XGET "http://localhost:9200/library/_search" -d' 
{ 
  "query": { 
    "function_score": { 
      "query": { 
        "match": { 
          "tags": "novel" 
        } 
      }, 
      "functions": [ 
        { 
          "filter": { 
            "term": { 
              "tags": "classics" 
            } 
          }, 
          "weight": 2 
        } 
      ], 
      "boost_mode": "replace" 
    } 
  } 
}'

The preceding query will match all the books which ...

Get Mastering Elasticsearch 5.x - Third Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.