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

filter: No Scores

Fast yes or no.

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

Is the price under one hundred? Is it in stock? These are yes or no questions. They shouldn't change how relevant a product is. That's what the filter clause is for.

  • Yes or no
  • Not relevance
  • filter

Search plus filters

Must holds the full text search, for lightweight. Filter holds the yes or no rules. In stock is true, and the price is under one hundred. Run it. Every result passes the filters, ranked by how well it matches lightweight.

Dev Tools Consoletext
GET products/_search?filter_path=hits.hits._score,hits.hits._source
{
  "query": {
    "bool": {
      "must": { "match": { "description": "lightweight" } },
      "filter": [
        { "term": { "in_stock": true } },
        { "range": { "price": { "lt": 100 } } }
      ]
    }
  },
  "_source": ["name", "price"]
}
Output
{
  "hits": {
    "hits": [
      {
        "_score": 2.1676,
        "_source": {
          "name": "Dry 70m Climbing Rope",
          "price": 90.99
        }
      },
      {
        "_score": 2.0816,
        "_source": {
          "name": "Hooded Down Jacket",
          "price": 53.99
        }
      }
    ]
  }
}
Also uses _load
products

Why filter is faster

Filters don't calculate scores, so they're cheaper. Elasticsearch can cache filter results, and reuse them for the next search. So put every yes or no condition in filter, and keep must for text that should be ranked.

keyvalue
No scoringless work
Cachedreused across searches
Use foryes/no conditions
Keep must forranked text

Summary

Put yes or no conditions in filter. They don't change scores, and they can be cached. Next, should clauses and boosting.

Key takeaways
  • filter
  • No score
  • Cached
Next up: should and Boosting — nice to have