- Elasticsearch 7.0 Cookbook(Fourth Edition)
- Alberto Paro
- 253字
- 2021-06-24 14:52:12
How to do it...
We want use the feature type to implement a common PageRank scenario where documents are scored base of same characteristics. To achieve this will execute the following steps:
- To be able to score base on a pagerank value and an inverse url length, we will use a similar mapping:
PUT test-feature
{
"mappings": {
"properties": {
"pagerank": {
"type": "feature"
},
"url_length": {
"type": "feature",
"positive_score_impact": false
}
}
}
}
- Now, we can store a document as shown here:
PUT test-feature/_doc/1
{
"pagerank": 5,
"url_length": 20
}
- Now, we can execute a feature query on the pagerank value to return our record with a similar query, like so:
GET test-feature/_search
{
"query": {
"feature": {
"field": "pagerank"
}
}
}
- The evolution of the previous feature functionality is to define a vector of values using the feature_vector type; usually it cab be use to score by topics, categories, or similar discerning facets. We can implement this functionality using the following steps:
So, the following code defines the mapping for the categories field:
PUT test-features
{
"mappings": {
"properties": {
"categories": {
"type": "feature_vector"
}
}
}
}
- We can now store some documents in the index by using the following commands:
PUT test-features/_doc/1
{
"categories": {
"sport": 14.2,
"economic": 24.3
}
}
PUT test-features/_doc/2
{
"categories": {
"sport": 19.2,
"economic": 23.1
}
}
- Now, we can search based on saved feature values, as shown here:
GET test-features/_search
{
"query": {
"feature": {
"field": "categories.sport"
}
}
}