2014-02-16 13:20:33 -05:00
[[search-aggregations-metrics-cardinality-aggregation]]
2014-05-12 19:35:58 -04:00
=== Cardinality Aggregation
2014-02-16 13:20:33 -05:00
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.
2018-01-12 10:36:48 -05:00
Assume you are indexing store sales and would like to count the unique number of sold products that match a query:
2014-02-16 13:20:33 -05:00
2019-09-05 10:11:25 -04:00
[source,console]
2014-02-16 13:20:33 -05:00
--------------------------------------------------
2017-05-16 17:24:26 -04:00
POST /sales/_search?size=0
2014-02-16 13:20:33 -05:00
{
2020-07-20 15:59:00 -04:00
"aggs": {
"type_count": {
"cardinality": {
"field": "type"
}
2014-02-16 13:20:33 -05:00
}
2020-07-20 15:59:00 -04:00
}
2014-02-16 13:20:33 -05:00
}
--------------------------------------------------
2017-05-16 17:24:26 -04:00
// TEST[setup:sales]
Response:
2019-09-06 16:09:09 -04:00
[source,console-result]
2017-05-16 17:24:26 -04:00
--------------------------------------------------
{
2020-07-20 15:59:00 -04:00
...
"aggregations": {
"type_count": {
"value": 3
2017-05-16 17:24:26 -04:00
}
2020-07-20 15:59:00 -04:00
}
2017-05-16 17:24:26 -04:00
}
--------------------------------------------------
// TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]
2014-02-16 13:20:33 -05:00
2015-02-04 09:05:26 -05:00
==== Precision control
2015-08-17 06:47:14 -04:00
This aggregation also supports the `precision_threshold` option:
2014-02-16 13:20:33 -05:00
2019-09-05 10:11:25 -04:00
[source,console]
2014-02-16 13:20:33 -05:00
--------------------------------------------------
2017-05-16 17:24:26 -04:00
POST /sales/_search?size=0
2014-02-16 13:20:33 -05:00
{
2020-07-20 15:59:00 -04:00
"aggs": {
"type_count": {
"cardinality": {
"field": "type",
"precision_threshold": 100 <1>
}
2014-02-16 13:20:33 -05:00
}
2020-07-20 15:59:00 -04:00
}
2014-02-16 13:20:33 -05:00
}
--------------------------------------------------
2017-05-16 17:24:26 -04:00
// TEST[setup:sales]
2014-02-16 13:20:33 -05:00
2015-04-26 12:49:15 -04:00
<1> The `precision_threshold` options allows to trade memory for accuracy, and
2014-02-16 13:20:33 -05:00
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
2018-05-08 10:12:04 -04:00
effect as a threshold of 40000. The default value is +3000+.
2014-02-16 13:20:33 -05:00
==== 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
2020-07-31 16:16:31 -04:00
https://static.googleusercontent.com/media/research.google.com/fr//pubs/archive/40671.pdf[HyperLogLog++]
2014-02-16 13:20:33 -05:00
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.
2014-05-12 19:35:58 -04:00
2014-02-16 13:20:33 -05:00
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:
2016-07-20 04:36:06 -04:00
////
To generate this chart use this gnuplot script:
2016-08-29 17:33:25 -04:00
[source,gnuplot]
2016-07-20 04:36:06 -04:00
-------
#!/usr/bin/gnuplot
reset
set terminal png size 1000,400
set xlabel "Actual cardinality"
set logscale x
set ylabel "Relative error (%)"
set yrange [0:8]
set title "Cardinality error"
set grid
set style data lines
plot "test.dat" using 1:2 title "threshold=100", \
"" using 1:3 title "threshold=1000", \
"" using 1:4 title "threshold=10000"
#
-------
and generate data in a 'test.dat' file using the below Java code:
2016-08-29 17:33:25 -04:00
[source,java]
2016-07-20 04:36:06 -04:00
-------
private static double error(HyperLogLogPlusPlus h, long expected) {
double actual = h.cardinality(0);
return Math.abs(expected - actual) / expected;
}
public static void main(String[] args) {
HyperLogLogPlusPlus h100 = new HyperLogLogPlusPlus(precisionFromThreshold(100), BigArrays.NON_RECYCLING_INSTANCE, 1);
HyperLogLogPlusPlus h1000 = new HyperLogLogPlusPlus(precisionFromThreshold(1000), BigArrays.NON_RECYCLING_INSTANCE, 1);
HyperLogLogPlusPlus h10000 = new HyperLogLogPlusPlus(precisionFromThreshold(10000), BigArrays.NON_RECYCLING_INSTANCE, 1);
int next = 100;
int step = 10;
for (int i = 1; i <= 10000000; ++i) {
long h = BitMixer.mix64(i);
h100.collect(0, h);
h1000.collect(0, h);
h10000.collect(0, h);
if (i == next) {
System.out.println(i + " " + error(h100, i)*100 + " " + error(h1000, i)*100 + " " + error(h10000, i)*100);
next += step;
if (next >= 100 * step) {
step *= 10;
}
}
}
}
-------
////
2014-02-16 13:20:33 -05:00
image:images/cardinality_error.png[]
2018-10-22 13:15:45 -04:00
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.
2014-02-16 13:20:33 -05:00
==== Pre-computed hashes
2015-08-17 06:47:14 -04:00
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
2017-11-29 03:44:25 -05:00
or by letting Elasticsearch compute hash values for you by using the
2016-08-04 09:56:28 -04:00
{plugins}/mapper-murmur3.html[`mapper-murmur3`] plugin.
2014-02-16 13:20:33 -05:00
NOTE: 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.
2019-09-05 10:11:25 -04:00
[source,console]
2014-02-16 13:20:33 -05:00
--------------------------------------------------
2017-05-16 17:24:26 -04:00
POST /sales/_search?size=0
2014-02-16 13:20:33 -05:00
{
2020-07-20 15:59:00 -04:00
"aggs": {
"type_promoted_count": {
"cardinality": {
"script": {
"lang": "painless",
"source": "doc['type'].value + ' ' + doc['promoted'].value"
2014-02-16 13:20:33 -05:00
}
2020-07-20 15:59:00 -04:00
}
2014-02-16 13:20:33 -05:00
}
2020-07-20 15:59:00 -04:00
}
2014-02-16 13:20:33 -05:00
}
--------------------------------------------------
2017-05-16 17:24:26 -04:00
// TEST[setup:sales]
2015-04-26 11:30:38 -04:00
2017-05-17 17:42:25 -04:00
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:
2015-05-12 05:37:22 -04:00
2019-09-05 10:11:25 -04:00
[source,console]
2015-05-12 05:37:22 -04:00
--------------------------------------------------
2017-05-16 17:24:26 -04:00
POST /sales/_search?size=0
2015-05-12 05:37:22 -04:00
{
2020-07-20 15:59:00 -04:00
"aggs": {
"type_promoted_count": {
"cardinality": {
"script": {
"id": "my_script",
"params": {
"type_field": "type",
"promoted_field": "promoted"
}
2015-05-12 05:37:22 -04:00
}
2020-07-20 15:59:00 -04:00
}
2015-05-12 05:37:22 -04:00
}
2020-07-20 15:59:00 -04:00
}
2015-05-12 05:37:22 -04:00
}
--------------------------------------------------
2017-05-16 17:24:26 -04:00
// TEST[skip:no script]
2015-05-12 05:37:22 -04:00
2015-05-07 10:46:40 -04:00
==== 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.
2019-09-05 10:11:25 -04:00
[source,console]
2015-05-07 10:46:40 -04:00
--------------------------------------------------
2017-05-16 17:24:26 -04:00
POST /sales/_search?size=0
2015-05-07 10:46:40 -04:00
{
2020-07-20 15:59:00 -04:00
"aggs": {
"tag_cardinality": {
"cardinality": {
"field": "tag",
"missing": "N/A" <1>
}
2015-05-07 10:46:40 -04:00
}
2020-07-20 15:59:00 -04:00
}
2015-05-07 10:46:40 -04:00
}
--------------------------------------------------
2017-05-16 17:24:26 -04:00
// TEST[setup:sales]
2015-05-07 10:46:40 -04:00
<1> Documents without a value in the `tag` field will fall into the same bucket as documents that have the value `N/A`.