Cardinality Aggregation

A single-value metrics aggregation that calculates an approximate count of distinct values. Values can be extracted either from specific fields in the document or generated by a script.

Assume you are indexing store sales and would like to count the unique number of sold products that match a query:

  1. POST /sales/_search?size=0
  2. {
  3. "aggs": {
  4. "type_count": {
  5. "cardinality": {
  6. "field": "type"
  7. }
  8. }
  9. }
  10. }

Response:

  1. {
  2. ...
  3. "aggregations": {
  4. "type_count": {
  5. "value": 3
  6. }
  7. }
  8. }

Precision control

This aggregation also supports the precision_threshold option:

  1. POST /sales/_search?size=0
  2. {
  3. "aggs": {
  4. "type_count": {
  5. "cardinality": {
  6. "field": "type",
  7. "precision_threshold": 100
  8. }
  9. }
  10. }
  11. }

The precision_threshold options allows to trade memory for accuracy, and defines a unique count below which counts are expected to be close to accurate. Above this value, counts might become a bit more fuzzy. The maximum supported value is 40000, thresholds above this number will have the same effect as a threshold of 40000. The default value is 3000.

Counts are approximate

Computing exact counts requires loading values into a hash set and returning its size. This doesn’t scale when working on high-cardinality sets and/or large values as the required memory usage and the need to communicate those per-shard sets between nodes would utilize too many resources of the cluster.

This cardinality aggregation is based on the HyperLogLog++ algorithm, which counts based on the hashes of the values with some interesting properties:

  • configurable precision, which decides on how to trade memory for accuracy,
  • excellent accuracy on low-cardinality sets,
  • fixed memory usage: no matter if there are tens or billions of unique values, memory usage only depends on the configured precision.

For a precision threshold of c, the implementation that we are using requires about c * 8 bytes.

The following chart shows how the error varies before and after the threshold:

cardinality error

For all 3 thresholds, counts have been accurate up to the configured threshold. Although not guaranteed, this is likely to be the case. Accuracy in practice depends on the dataset in question. In general, most datasets show consistently good accuracy. Also note that even with a threshold as low as 100, the error remains very low (1-6% as seen in the above graph) even when counting millions of items.

The HyperLogLog++ algorithm depends on the leading zeros of hashed values, the exact distributions of hashes in a dataset can affect the accuracy of the cardinality.

Please also note that even with a threshold as low as 100, the error remains very low, even when counting millions of items.

Pre-computed hashes

On string fields that have a high cardinality, it might be faster to store the hash of your field values in your index and then run the cardinality aggregation on this field. This can either be done by providing hash values from client-side or by letting Elasticsearch compute hash values for you by using the mapper-murmur3 plugin.

Pre-computing hashes is usually only useful on very large and/or high-cardinality fields as it saves CPU and memory. However, on numeric fields, hashing is very fast and storing the original values requires as much or less memory than storing the hashes. This is also true on low-cardinality string fields, especially given that those have an optimization in order to make sure that hashes are computed at most once per unique value per segment.

Script

The cardinality metric supports scripting, with a noticeable performance hit however since hashes need to be computed on the fly.

  1. POST /sales/_search?size=0
  2. {
  3. "aggs": {
  4. "type_promoted_count": {
  5. "cardinality": {
  6. "script": {
  7. "lang": "painless",
  8. "source": "doc['type'].value + ' ' + doc['promoted'].value"
  9. }
  10. }
  11. }
  12. }
  13. }

This will interpret the script parameter as an inline script with the painless script language and no script parameters. To use a stored script use the following syntax:

  1. POST /sales/_search?size=0
  2. {
  3. "aggs": {
  4. "type_promoted_count": {
  5. "cardinality": {
  6. "script": {
  7. "id": "my_script",
  8. "params": {
  9. "type_field": "type",
  10. "promoted_field": "promoted"
  11. }
  12. }
  13. }
  14. }
  15. }
  16. }

Missing value

The missing parameter defines how documents that are missing a value should be treated. By default they will be ignored but it is also possible to treat them as if they had a value.

  1. POST /sales/_search?size=0
  2. {
  3. "aggs": {
  4. "tag_cardinality": {
  5. "cardinality": {
  6. "field": "tag",
  7. "missing": "N/A"
  8. }
  9. }
  10. }
  11. }

Documents without a value in the tag field will fall into the same bucket as documents that have the value N/A.