Mapping the Rank Feature and Feature Vector fields
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.
Getting ready
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.
How to do it…
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:
- To be able to score based on a
pagerankvalue and an inverseurllength, we can use the following mapping:PUT test-rank { "mappings": { "properties": { "pagerank": { "type": "rank_feature" }, "url_length": { "type": "rank_feature", "positive_score_impact": false } } } } - Now, we can store a document, as shown here:
PUT test-rank/_doc/1 { "pagerank": 5, "url_length": 20 } - Now, we can execute a feature query on the
pagerankvalue 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_featurestypes, we need to use the specialrank_featurequery 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:
- First, we must define the mapping for the
categoriesfield:PUT test-ranks { "mappings": { "properties": { "categories": { "type": "rank_features" } } } } - Now, we can store some documents in the index by using the following commands:
PUT test-ranks/_doc/1 { "categories": { "sport": 14.2, "economic": 24.3 } } PUT test-ranks/_doc/2 { "categories": { "sport": 19.2, "economic": 23.1 } } - Now, we can search based on the saved feature values, as shown here:
GET test-ranks/_search { "query": { "feature": { "field": "categories.sport" } } }
How it works…
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.