I have an index with ~100 million documents. Sometimes I want to retrieve all documents, sometimes I want to group by a particular field, selecting only one document for each unique value. Something like:
SELECT * FROM documents GROUP BY my_field;
To illustrate, sometimes I want to get:
|id|my_field|thing |
|--|--------|-------|
|1 |a |red |
|2 |b |yellow |
|3 |b |green |
|4 |c |brown |
and sometimes
|id|my_field|thing |
|--|--------|-------|
|1 |a |red |
|3 |b |green |
|4 |c |brown |
The ElasticSearch documents suggest that Terms Aggregation fits this use. However, there is the suggestion that this is only suitable for small cardinalities of the aggregated term, i.e. a small number of 'buckets':
Sometimes there are too many unique terms to process in a single request/response pair so it can be useful to break the analysis up into multiple requests
In addition to this, the Top Hits aggregation returns data in a format that doesn't look like it can be paginated easily.
If I had, for example, a million distinct terms, I looks like it wouldn't be well aligned with the intended design. Is this correct?
My alternative is to maintain an index and flag duplicates in my own code, but this is error-prone, subject to concurrency issues etc.
Is there a way to achieve what I want that plays to the strengths of ElasticSearch?