Skip to content
Tutorials Center
My learning

Topics

Lesson 25 of 43 ยท Aggregations and Analytics

Faceted Search and Filters

Build shop-style faceted search in Elasticsearch: filters, post_filter, global aggregations and multi-select facet counts, with a runnable Python facet counter.

Open any online shop and search for "shoe". Down the left side you see facets: Brand (Acme 12, Zoom 7), Color (Black 9, Red 4), Size, Price. Each line shows how many results you would get if you clicked it. Facets are one of the most requested search features, and they are built from the same aggregations you already know, plus one clever trick for what happens after the user picks a value.

The tricky part is not counting. It is deciding which documents each count should include. If a user ticks "Acme" and then looks at the Brand facet, should it show only Acme (everything else 0), or the other brands too, so they can add a second one? This lesson shows the options and the requests behind them. The real requests are read-only; the Python stand-ins do the counting with plain loops so you can see the logic.

Anatomy of a faceted page

Query What the user typed.
Selections Facet values already ticked.
Hits Results that match everything.
Facet counts A number next to each option.
1 / 2

Step one: filters and aggregations

Start with the simplest build: a query, selections as filters inside a bool query, and aggregations for the facets.

JSON
GET /products/_search
{
  "query": {
    "bool": {
      "must":   [{ "match": { "title": "shoe" } }],
      "filter": [{ "term": { "brand": "acme" } }]
    }
  },
  "aggs": {
    "brands": { "terms": { "field": "brand" } },
    "colors": { "terms": { "field": "color" } }
  }
}

A filter clause does not affect the score and is cacheable (see Bool Queries and Compound Logic). But there is a catch: aggregations run on the documents that match the whole query, filter included. So the brands facet now shows only acme, because everything else was filtered out. For a single-select facet that may be fine, but shoppers expect to see the alternatives.

post_filter: filter the hits but not the aggregations

The post_filter runs after aggregations are computed. It narrows the hits but leaves the aggregation input alone.

JSON
GET /products/_search
{
  "query": { "match": { "title": "shoe" } },
  "post_filter": { "term": { "brand": "acme" } },
  "aggs": {
    "brands": { "terms": { "field": "brand" } },
    "colors": { "terms": { "field": "color" } }
  }
}

Now the hits contain only Acme shoes, while brands counts every brand that matches "shoe". But look at colors: it now counts colors over all brands too, so "Black 9" is not the number of black Acme shoes. That is wrong when the user expects the facets to reflect what they selected elsewhere.

Python Filter vs post_filter facet counts

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

In version A the brand facet collapses to acme only. In version B the brand facet is right, but the color facet counts black shoes from every brand (4 black) while the hits only include Acme (2 black). Neither is fully correct. The next section combines them.

Multi-select facets: every facet ignores itself

The pattern used by serious shops is simple to state: each facet's counts apply all the selections except the facet's own. Brand counts respect the chosen colors, color counts respect the chosen brands, and the result list respects everything.

In Elasticsearch you do this with post_filter for the hits plus a filter aggregation per facet that holds the other selections:

JSON
GET /products/_search
{
  "query": { "match": { "title": "shoe" } },
  "post_filter": {
    "bool": { "filter": [
      { "term": { "brand": "acme" } },
      { "term": { "color": "black" } }
    ] }
  },
  "aggs": {
    "brands": {
      "filter": { "term": { "color": "black" } },
      "aggs": { "values": { "terms": { "field": "brand" } } }
    },
    "colors": {
      "filter": { "term": { "brand": "acme" } },
      "aggs": { "values": { "terms": { "field": "color" } } }
    }
  }
}

The brands facet applies only the color selection, and the colors facet applies only the brand selection. The post filter takes care of the hits. A facet that has no other selections needs no filter, so you can leave out the filter wrapper for it.

The global aggregation is a related tool. It ignores the query entirely and aggregates over all documents in the index, which is useful for "store-wide" counts such as "12,480 products" next to your filtered numbers. Inside a global aggregation you can then add your own filter to rebuild exactly the subset you want.

Python Multi-select facets: each facet ignores its own selection

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Notice that inside one facet the selected values combine with OR (Acme or Zoom), while different facets combine with AND (a brand and a color). The brand facet still shows nova with a count of 1, because it ignores its own ticks but respects "black". The size facet has no selection of its own, so it ignores nothing: it is counted over documents that match both the brand and color selections, which are exactly the hits.

Building the request from state

You rarely write these requests by hand. Your application keeps the selection state and generates the request. Here is that generator, which produces the same structure as the JSON above for any selection.

Python Generate the faceted request from selections

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The generated JSON has one post_filter holding every selection, a filter wrapper on brand (only the color selection), on color (only the brand selection) and on size (both selections, since size itself has none). A facet gets a bare terms only when no other facet has a selection. The terms filter for the two brands is how "Acme or Zoom" is expressed.

Practical tips

  • Use keyword fields for facet values and keep them lowercase and normalized, so "Acme" and "acme" do not become two facets.
  • Counts can be approximate on large, multi-shard indexes (see the previous lessons). That is rarely a problem for a shop, but do not show facet counts as accounting data.
  • Price ranges are a range aggregation with named buckets; dates use date_histogram; nested attributes such as size-and-stock pairs need nested fields and a nested aggregation.
  • Cost grows with facets. Each facet is another aggregation over the matching set. Offer the facets users actually click, and cache whole responses on your side when many people ask the same thing.
  • Elasticsearch has no "empty facet" memory: a value with zero matches disappears from terms unless you add min_doc_count: 0, which then needs a known value list. Many shops show zero-count options greyed out using their own catalog list.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why does a selection placed as a filter inside the query make the same facet look useless?

    Aggregations see exactly the documents that match the whole query. Once the filter removes other brands, the brand facet can only count the selected one.
  2. 2. What does post_filter do?

    post_filter applies to the hits only. Aggregations are computed on the documents matching the main query.
  3. 3. In a multi-select facet design, which documents are counted for the Color facet?

    Each facet ignores its own selection and respects all the others, so users can add another value to the same facet.
  4. 4. How are two ticked values within one facet usually combined, and two different facets?

    Ticking Acme and Zoom means either brand; ticking a brand and a color means both conditions. A terms filter inside a bool filter list expresses that.

Summary

  • Facets are aggregations (terms, range, date_histogram) shown next to results, with counts.
  • A filter inside the query also narrows the aggregations; post_filter narrows only the hits.
  • Multi-select facets: put every selection in post_filter, and wrap each facet's aggregation in a filter with all the other selections.
  • global aggregations ignore the query when you need store-wide numbers.
  • Generate the request from your selection state, use normalized keyword fields, and treat counts as approximate on big indexes.

Next: Vector and Semantic Search.