AI Hints
Home›Elasticsearch Tutorial›Better Ranking and Aggregations
Elasticsearch · Lesson 67 of 120

Practice: Better Ranking

Put it together.

⏱ 2 min read▶ Video at 1:03:25🎓 Free
Watch this lesson in the Elasticsearch courseWatch on YouTube ↗Full Elasticsearch course

Let's put the ranking ideas together for Trailhead's main search box. Typos allowed, names matter most, popular products get a gentle lift, and out of stock items are hidden.

  • Typos OK
  • Names first
  • Popular lift
  • Hide out of stock

The ranking query

The inner query searches name and description, with name boosted and fuzziness on. The filter hides out of stock items. Field value factor gives a gentle lift by rating. Run it. A typo, tent with three n's, still finds well rated tents first.

Dev Tools Consoletext
GET products/_search?filter_path=hits.hits._source
{
  "query": { "function_score": {
    "query": { "bool": {
      "must": { "multi_match": { "query": "tennt",
        "fields": ["name^3", "description"], "fuzziness": "AUTO" } },
      "filter": { "term": { "in_stock": true } } } },
    "field_value_factor": { "field": "rating", "modifier": "sqrt" } } },
  "_source": ["name", "rating"], "size": 3
}
Output
{
  "hits": {
    "hits": [
      {
        "_source": {
          "name": "Basecamp 4-Person Tent",
          "rating": 4.7
        }
      },
      {
        "_source": {
          "name": "Ultralight Solo Tent",
          "rating": 3.6
        }
      },
      {
        "_source": {
          "name": "Basecamp 2-Person Tent",
          "rating": 4.4
        }
      }
    ]
  }
}
Also uses _load
catalog

A ranking recipe

Start with good text matching, and the right fields. Boost the important fields. Add business signals gently. And always test with real searches from real customers.

keyvalue
1good text match
2boost key fields
3gentle business signals
4test real searches

Summary

Good ranking is text first, then field boosts, then gentle business signals, all tested on real searches. Next, aggregations, the analytics side of Elasticsearch.

Key takeaways
  • Text first
  • Boosts
  • Signals
Next up: Aggregations — numbers, not documents