The 30 second answer
Use field collapsing on model_id, a single-valued keyword field with doc values: Elasticsearch keeps only the top-scoring document per model. Collapsing is cheap, but inner_hits to show the other colours sends an extra query per collapsed hit, so I keep it small or fetch variants separately. Collapsing doesn't affect aggregations or hits.total, so counts are per variant unless I add a cardinality aggregation.
The setup. With one document per colour variant, a search for "white sneakers" can return the same model five times in different shades, pushing other models off the first page. The shopper wants one tile per model, with colour swatches.
Field collapsing. collapse on a field keeps the top document for each value. The docs say the field "must be a single valued keyword or numeric field with doc_values activated".
GET products/_search
{ "size": 48,
"query": { "bool": {
"must": [ { "match": { "name": "white sneakers" } } ],
"filter": [ { "term": { "market": "de" } } ] } },
"collapse": { "field": "model_id",
"inner_hits": { "name": "colours", "size": 5, "_source": ["color", "image_url"] },
"max_concurrent_group_searches": 4 },
"aggs": { "models": { "cardinality": { "field": "model_id" } } } }What it costs and what it doesn't do.
inner_hits: the docs say expansion "is done by sending an additional query for eachinner_hitrequest for each collapsed hit returned in the response. This can significantly slow your search if you have too many groups orinner_hitrequests." For a page of 48 tiles, that's 48 extra queries.- "Collapsing is applied to the top hits only and does not affect aggregations." Facet counts still count variants.
- "The total number of hits in the response indicates the number of matching documents without collapsing. The total number of distinct group is unknown." Use a
cardinalityaggregation for an approximate model count. collapsecan't be used withscroll. Withsearch_after, it works only when sorting and collapsing on the same field, with no secondary sort.
Badly written. Fetch 500 variants and de-duplicate in the application. The page ends up with too few tiles when one model has many variants, and the request is expensive.
Alternatives.
- Index one document per model with variant attributes as arrays, so no collapse is needed. Cheaper queries, but variant-level matching (white AND size 38 on the same variant) needs
nested, and stock changes rewrite the model document. The variant-modelling question on the Zalando company page covers this decision. - **Collapse without
inner_hits, then fetch swatches** for the 48 models with onetermsquery onmodel_id, or from a cache.
Alternate interview scenarios
"Facet counts show 1,200 white sneakers but the grid shows 300 tiles."
Collapsing doesn't touch aggregations. Either show counts per model with a cardinality sub-aggregation (approximate), or index per model.
"Deep scrolling a collapsed listing breaks."
search_after with collapse requires sorting on the collapse field itself, which is rarely the shopper's sort. Limit collapsed listings to the first pages, or switch the listing to per-model documents.