If you want to get all unique values without any approximation or setting a magic number (size: 500
), then use COMPOSITE AGGREGATION (ES 6.5+).
From official documentation:
"If you want to retrieve all terms or all combinations of terms in a nested terms aggregation you should use the COMPOSITE AGGREGATION which allows to paginate over all possible terms rather than setting a size greater than the cardinality of the field in the terms aggregation. The terms aggregation is meant to return the top terms and does not allow pagination."
Implementation example in JavaScript:
const ITEMS_PER_PAGE = 1000;const body = {"size": 0, // Returning only aggregation results: https://www.elastic.co/guide/en/elasticsearch/reference/current/returning-only-agg-results.html"aggs" : {"langs": {"composite" : {"size": ITEMS_PER_PAGE,"sources" : [ { "language": { "terms" : { "field": "language" } } } ] } } }};const uniqueLanguages = [];while (true) { const result = await es.search(body); const currentUniqueLangs = result.aggregations.langs.buckets.map(bucket => bucket.key); uniqueLanguages.push(...currentUniqueLangs); const after = result.aggregations.langs.after_key; if (after) { // continue paginating unique items body.aggs.langs.composite.after = after; } else { break; }}console.log(uniqueLanguages);