System design Level 2 This level · 13 of 48
Hard search elasticsearch

Write the Elasticsearch request for a product listing page with facets, sorting and pagination.

The 30 second answer

One request with a bool query: the shopper's text in must, market and category in filter. Selected facet values go in post_filter, so facet counts are still computed over the wider result set. Each facet is a terms aggregation wrapped in a filter aggregation that applies every selected facet except its own. Sort by score or price with a unique tiebreaker, and page with search_after, not deep from.

Could you say this in an interview?

This is the most likely "live" Elasticsearch task for a fashion shop: the catalog page for "women's sneakers in Germany", where the shopper picked brand Nike and size 38, sorted by price, and is on page two. Zalando's engineering blog describes the same split in production: for every search, its Catalog API "issues a separate call to fetch the filters (facets) for that query: brand, size, color, price bucket", and those calls are "aggregation-heavy queries by design".

Step one: the parts of the page and where each goes.

Page elementRequest partWhy
Search textquery.bool.mustAffects ranking
Market, category, in stockquery.bool.filterHard limits for both hits and facets, cacheable
Selected facets (brand, size)post_filterLimits hits, but facet counts still show other options
Facet listsaggsCounts per value
Sortsort with a tiebreakerStable order for paging
Page twosearch_afterCheap at any depth

Step two: the naive request. Selected facets in the main filter, so the brand facet only shows Nike (the shopper can no longer see or pick Adidas), plus from for paging and no tiebreaker:

GET products/_search
{ "from": 48, "size": 48,
  "query": { "bool": { "filter": [
      { "term": { "market": "de" } }, { "term": { "category_id": "women-sneakers" } },
      { "term": { "brand_id": "nike" } }, { "term": { "size": "38" } } ] } },
  "sort": [ { "price": "asc" } ],
  "aggs": { "brands": { "terms": { "field": "brand_id" } },
            "sizes":  { "terms": { "field": "size" } } } }

Step three: the production shape.

GET products/_search
{
  "size": 48,
  "track_total_hits": 10000,
  "_source": ["name", "brand", "price", "image_url", "model_id"],
  "query": { "bool": {
    "filter": [ { "term": { "market": "de" } },
                { "term": { "category_id": "women-sneakers" } },
                { "term": { "in_stock": true } } ] } },
  "post_filter": { "bool": { "filter": [
    { "terms": { "brand_id": ["nike"] } },
    { "terms": { "size": ["38"] } } ] } },
  "aggs": {
    "brands": { "filter": { "terms": { "size": ["38"] } },
                "aggs": { "values": { "terms": { "field": "brand_id", "size": 30 } } } },
    "sizes":  { "filter": { "terms": { "brand_id": ["nike"] } },
                "aggs": { "values": { "terms": { "field": "size", "size": 40 } } } },
    "price_ranges": { "filter": { "bool": { "filter": [
                        { "terms": { "brand_id": ["nike"] } }, { "terms": { "size": ["38"] } } ] } },
                      "aggs": { "values": { "range": { "field": "price",
                        "ranges": [ { "to": 5000 }, { "from": 5000, "to": 10000 }, { "from": 10000 } ] } } } }
  },
  "sort": [ { "price": "asc" }, { "sku": "asc" } ],
  "search_after": [ 7995, "SKU-123456" ]
}

How to explain each choice:

  • **No must here** because it's a category page. On a search page, the text query goes in must and the default sort is _score.
  • **post_filter** applies brand and size only to the hits, after the aggregations. Each facet aggregation applies the other selected facets. That's the usual "disjunctive facet" pattern: the brand list shows every brand that has size 38, and the size list shows every size Nike has.
  • **price_ranges** applies both selected facets, since price isn't selected yet.
  • **sort plus a unique tiebreaker** (sku) gives a stable order. search_after passes the last hit's sort values to fetch the next page. For long browsing sessions, open a point in time (PIT) so refreshes don't shift results between pages.
  • **track_total_hits: 10000** (the default) is enough for a "10,000+ items" label. An exact count is more work.
  • **_source filtering** returns only what the tile needs.

Trade-offs and extensions.

  • Facets and hits in one request means one round trip, but the hits wait for the aggregations. Vinted's engineering blog reports testing both options: sending facets as a separate request used about 20% more Elasticsearch CPU, but item search was about 71 ms faster, and they chose the separate request. Zalando's catalog layer also calls facets separately.
  • Facet size and the fields you facet on are the cost knobs. Never facet on an identifier such as SKU; the Zalando outage question on the company page shows why.
  • Page two to N rarely need new facets. Cache facet results per query and filter set, or skip them on pagination requests.

Alternate interview scenarios

"Facet counts take most of the request time on broad categories. How do you cut it?"

Split facets into their own request and cache them, lower facet size, facet only on curated fields, and consider terminate_after on the facet request. Vinted reports setting terminate_after to 100,000 because it shows counts only up to 500 per facet; the trade is that counts on broad queries become partial.

"The shopper switches sort to 'newest'. What changes?"

Only sort (for example created_at desc plus the tiebreaker) and the search_after values. Scores aren't needed, so the query can stay entirely in filter context.

Sources (5)
  1. Zalando Engineering: The Day Our Own Queries DoS'ed Us: Inside Zalando Search (December 2025)engineering.zalando.com
  2. Vinted Engineering: Faceted search using Elasticsearch (March 2023)vinted.engineering
  3. Elasticsearch docs: Filter search results (post_filter)elastic.co
  4. Elasticsearch docs: Paginate search results (search_after and PIT)elastic.co
  5. Elasticsearch docs: Search API (track_total_hits, terminate_after)elastic.co
Previous
Next