Elasticsearch · Lesson 117 of 120
Project: Autocomplete and Ranking
The finishing touches.
Two finishing touches. Autocomplete as the shopper types. And ranking that gently prefers well rated products that are in stock.
- Autocomplete
- Better ranking
- Finished!
Autocomplete
As the shopper types sle, we match against the auto subfield. Edge n grams make prefixes match instantly. Run it. Sleeping bags, after just three letters.
Dev Tools Consoletext
GET trailhead/_search?filter_path=hits.hits._source.name
{
"query": { "match": { "name.auto": "sle" } },
"_source": ["name"], "size": 3
}Output
{
"hits": {
"hits": [
{"_source": {"name": "Arctic Down Sleeping Bag"}},
{"_source": {"name": "Alpine Down Sleeping Bag"}},
{"_source": {"name": "Summer Down Sleeping Bag"}}
]
}
}Also uses _load
productsAlso uses _setup.http
PUT trailhead?filter_path=acknowledged
{ "settings": { "analysis": {
"filter": { "edge": { "type": "edge_ngram", "min_gram": 2, "max_gram": 10 } },
"analyzer": { "auto": { "tokenizer": "standard", "filter": ["lowercase", "edge"] } } } },
"mappings": { "properties": {
"name": { "type": "text", "analyzer": "english", "fields": {
"auto": { "type": "text", "analyzer": "auto", "search_analyzer": "standard" },
"keyword": { "type": "keyword" } } },
"description": { "type": "text", "analyzer": "english" },
"brand": { "type": "keyword" }, "category": { "type": "keyword" },
"price": { "type": "double" }, "rating": { "type": "double" },
"in_stock": { "type": "boolean" } } } }
POST _reindex?refresh=true&filter_path=created
{ "source": { "index": "products" }, "dest": { "index": "trailhead" } }Ranking with business signals
Function score wraps the text query. Field value factor lifts higher ratings gently. A weight function boosts in stock items. Run it. Well rated, in stock jackets rise to the top.
Dev Tools Consoletext
GET trailhead/_search?filter_path=hits.hits._source
{
"query": { "function_score": {
"query": { "match": { "name": "jacket" } },
"functions": [
{ "field_value_factor": { "field": "rating", "modifier": "sqrt" } },
{ "filter": { "term": { "in_stock": true } }, "weight": 1.5 } ] } },
"_source": ["name", "rating"], "size": 3
}Output
{
"hits": {
"hits": [
{"_source": {"name": "Breeze Rain Jacket", "rating": 4.8}},
{"_source": {"name": "Storm Rain Jacket", "rating": 4.7}},
{"_source": {"name": "Hooded Down Jacket", "rating": 4.5}}
]
}
}Also uses _load
productsAlso uses _setup.http
PUT trailhead?filter_path=acknowledged
{ "settings": { "analysis": {
"filter": { "edge": { "type": "edge_ngram", "min_gram": 2, "max_gram": 10 } },
"analyzer": { "auto": { "tokenizer": "standard", "filter": ["lowercase", "edge"] } } } },
"mappings": { "properties": {
"name": { "type": "text", "analyzer": "english", "fields": {
"auto": { "type": "text", "analyzer": "auto", "search_analyzer": "standard" },
"keyword": { "type": "keyword" } } },
"description": { "type": "text", "analyzer": "english" },
"brand": { "type": "keyword" }, "category": { "type": "keyword" },
"price": { "type": "double" }, "rating": { "type": "double" },
"in_stock": { "type": "boolean" } } } }
POST _reindex?refresh=true&filter_path=created
{ "source": { "index": "products" }, "dest": { "index": "trailhead" } }Summary
Trailhead search is done, with a good mapping, search with facets, autocomplete, and smart ranking. Next, a cheat sheet of everything.
Key takeaways- Mapping
- Search
- Autocomplete
- Ranking
Next up: Cheat Sheet — everything on one page