The 30 second answer
Each shard returns only its top shard_size terms, default size times 1.5 plus 10, and the coordinator merges them, so a term that's big overall but small on one shard can be undercounted. doc_count_error_upper_bound shows the possible error and sum_other_doc_count what was cut. Raise shard_size before size, route related data to one shard for exact counts, use composite to page all values, and cardinality for approximate distinct counts.
From the basics. A terms aggregation builds one bucket per distinct value (brand, colour) with a document count. In a sharded index, each shard counts its own documents, and the coordinating node merges the lists. To keep that merge small, a shard doesn't send every bucket, only its top ones.
Where the error comes from. The docs: "the terms agg fetches more than the top size terms from each shard. It fetches the top shard_size terms, which defaults to size * 1.5 + 10." A brand that is fifth overall but just outside the top list on one shard loses that shard's count. The response tells you:
sum_other_doc_count: documents in buckets that didn't make the returned top list.doc_count_error_upper_bound(withshow_term_doc_count_error: true): an upper bound on the missing count, "the sum of the size of the largest bucket on each shard that didn't fit intoshard_size".
The docs add: "It is much cheaper to increase the shard_size than to increase the size", and even then counts "may be approximate".
Memory. On keyword fields, the default execution uses global ordinals: a per-shard mapping from each distinct term to a number, held on the heap. The docs warn that "aggregations on high cardinality fields can use a lot of memory and trigger the field data circuit breaker". By default they're built at the first aggregation after a refresh; eager_global_ordinals: true builds them at refresh time instead, which "shifts the cost of building global ordinals from search to index-time".
Badly written. Return every brand by setting a huge size, and count distinct shoppers with a terms aggregation:
{ "size": 0, "aggs": {
"all_brands": { "terms": { "field": "brand_id", "size": 100000 } },
"buyers": { "terms": { "field": "customer_id", "size": 1000000 } } } }Better. Top facet values with a modest shard_size bump, composite to page through all values for an export, cardinality for a distinct count:
{ "size": 0, "aggs": {
"top_brands": { "terms": { "field": "brand_id", "size": 30, "shard_size": 100,
"show_term_doc_count_error": true } },
"distinct_models": { "cardinality": { "field": "model_id", "precision_threshold": 3000 } } } }
{ "size": 0, "aggs": { "all_brands": { "composite": {
"size": 1000, "sources": [ { "brand": { "terms": { "field": "brand_id" } } } ],
"after": { "brand": "levis" } } } } }cardinality uses HyperLogLog++. precision_threshold "defines a unique count below which counts are expected to be close to accurate"; the default is 3000 and the maximum supported value is 40000.
Trade-offs.
- Accuracy costs memory and network: bigger
shard_sizemeans bigger per-shard lists to send and merge. - If all documents for a key live on one shard (custom routing), counts for that key are exact, at the risk of uneven shards.
eager_global_ordinalsmakes facets on a hot field fast after each refresh but slows refresh. It pays off on read-heavy indices with a moderate refresh rate.
Alternate interview scenarios
"The brand facet shows 'Adidas 4,980' but filtering on Adidas shows 5,012 products. Bug?"
Probably shard-level truncation. Check doc_count_error_upper_bound, raise shard_size, and explain that facet counts on a broad query can be approximate.
"Analytics wants all distinct colours per category, tens of thousands of combinations."
Use composite with paging (after), run it off the shopper path, and keep size and frequency limited.