AI Hints
Home›Elasticsearch Tutorial›Excluding and Context
Elasticsearch · Lesson 37 of 120

must_not

Leave things out.

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

A shopper wants a jacket, but not a down one, because they're allergic to feathers. Must not removes any document that matches.

  • Jackets
  • Not down
  • must_not

Jackets, but not down

The filter keeps the Jackets category. Must not removes anything with down in the name. Run it. Rain and fleece jackets, and no down jackets.

Dev Tools Consoletext
GET products/_search?filter_path=hits.hits._source.name
{
  "query": {
    "bool": {
      "filter": { "term": { "category.keyword": "Jackets" } },
      "must_not": { "match": { "name": "down" } }
    }
  }
}
Output
{
  "hits": {
    "hits": [
      {"_source": {"name": "Storm Rain Jacket"}},
      {"_source": {"name": "Breeze Rain Jacket"}},
      {"_source": {"name": "Cloudburst Rain Jacket"}},
      {"_source": {"name": "Grid Fleece Jacket"}},
      {"_source": {"name": "Classic Fleece Jacket"}}
    ]
  }
}
Also uses _load
products

About must_not

Must not works like a filter. It doesn't score, and it can be cached. It's great for hiding out of stock items, or blocked brands. A bool with only must not matches everything except those.

keyvalue
Scoringnone, like filter
Cachedyes
Use forhide, exclude, block
Aloneeverything except

Summary

Must not removes documents that match, without affecting scores. Next, the idea behind all of this, query and filter context.

Key takeaways
  • must_not
  • Exclude
  • No score
Next up: Query vs Filter Context — does it score?