-
Book Overview & Buying
-
Table Of Contents
Elasticsearch 8.x Cookbook - Fifth Edition
By :
It's common to want to score a document dynamically, depending on the context. For example, if you need to score more documents that are inside a category, the classic scenario is to boost (increase low-scored) documents that are based on a value, such as page rank, hits, or categories.
Elasticsearch provides two new ways to boost your scores based on values. One is the Rank Feature field, while the other is its extension, which is to use a vector of values.
You will need an up-and-running Elasticsearch installation, as we described in the Downloading and installing Elasticsearch recipe of Chapter 1, Getting Started.
To execute the commands in this recipe, you can use any HTTP client, such as curl (https://curl.haxx.se/), Postman (https://www.getpostman.com/), or similar. I suggest using the Kibana console, which provides code completion and better character escaping for Elasticsearch.
We want to use the rank_feature type to implement a common PageRank scenario where documents are scored based on the same characteristics. To achieve this, follow these steps:
pagerank value and an inverse url length, we can use the following mapping:PUT test-rank
{ "mappings": {
"properties": {
"pagerank": { "type": "rank_feature" },
"url_length": {
"type": "rank_feature",
"positive_score_impact": false
} } } }PUT test-rank/_doc/1
{ "pagerank": 5, "url_length": 20 }pagerank value to return our record with a similar query, like so:GET test-rank/_search
{ "query": { "rank_feature": { "field":"pagerank" }}} Important Note
To query the special rank/rank_features types, we need to use the special rank_feature query type, which is only used for this special case.
The evolution of the previous feature's functionality is to define a vector of values using the rank_features type; usually, it can be used to score by topics, categories, or similar discerning facets. We can implement this functionality by following these steps:
categories field:PUT test-ranks
{ "mappings": {
"properties": {
"categories": { "type": "rank_features" } } } }PUT test-ranks/_doc/1
{ "categories": { "sport": 14.2, "economic": 24.3 } }
PUT test-ranks/_doc/2
{ "categories": { "sport": 19.2, "economic": 23.1 } }GET test-ranks/_search
{ "query": { "feature": { "field": "categories.sport" } } }rank_feature and rank_features are special type fields that are used for storing values and are mainly used to score the results.
Important Note
The values that are stored in these fields can only be queried using the feature query. This cannot be used in standard queries and aggregations.
The value numbers in rank_feature and rank_features can only be single positive values (multi-values are not allowed).
In the case of rank_features, the values must be a hash, composed of a string and a positive numeric value.
There is a flag that changes the behavior of scoring – positive_score_impact. This value is true by default, but if you want the value of the feature to decrease the score, you can set it to false. In the pagerank example, the length of url reduces the score of the document because the longer url is, the less relevant it becomes.