AI Hints
Home›Elasticsearch Tutorial›The bool Query
Elasticsearch · Lesson 36 of 120

should and Boosting

Nice to have.

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

Some things aren't required, but they're a bonus. A tent that's in stock and highly rated should rank above one that isn't. Should clauses add to the score when they match.

  • Not required
  • A bonus
  • Boost the score

Prefer highly rated

Must matches tents. The should clause gives a bonus to products rated four point five or more. Boost two doubles that bonus. Run it. Highly rated tents rise to the top.

Dev Tools Consoletext
GET products/_search?filter_path=hits.hits._score,hits.hits._source
{
  "query": {
    "bool": {
      "must": { "match": { "name": "tent" } },
      "should": {
        "range": { "rating": { "gte": 4.5, "boost": 2 } }
      }
    }
  },
  "_source": ["name", "rating"], "size": 3
}
Output
{
  "hits": {
    "hits": [
      {
        "_score": 4.1453,
        "_source": {
          "name": "Basecamp Family Tent",
          "rating": 4.5
        }
      },
      {
        "_score": 3.9049,
        "_source": {
          "name": "Basecamp 4-Person Tent",
          "rating": 4.7
        }
      },
      {
        "_score": 2.1453,
        "_source": {
          "name": "Ultralight Solo Tent",
          "rating": 3.6
        }
      }
    ]
  }
}
Also uses _load
products

should rules

If a bool has must or filter, should clauses are optional bonuses. If it has only should clauses, at least one must match. Minimum should match changes that number. And boost makes any clause count more.

keyvalue
With must/filtershould = bonus
Only shouldat least one must match
minimum_should_matchhow many
boostweight a clause

Summary

Should clauses boost the score when they match, and boost makes them count more. Next, excluding results.

Key takeaways
  • should
  • Bonus score
  • boost
Next up: must_not — leave things out