elasticsearch/docs/reference/esql/functions/count-distinct.asciidoc
Abdon Pijpelink 8e0d4e1846
[DOCS] Improve ES|QL functions reference for functions A-D (#103447)
* Functions starting with A

* Functions starting with 'C'

* More 'C' functions

* Fix tests

* Fix missing snippet

* DATE_* functions

* Apply suggestions from code review

Co-authored-by: Bogdan Pintea <pintea@mailbox.org>

---------

Co-authored-by: Bogdan Pintea <pintea@mailbox.org>
2023-12-19 15:59:02 +01:00

67 lines
1.8 KiB
Text

[discrete]
[[esql-agg-count-distinct]]
=== `COUNT_DISTINCT`
*Syntax*
[source,esql]
----
COUNT_DISTINCT(column[, precision])
----
*Parameters*
`column`::
Column for which to count the number of distinct values.
`precision`::
Precision. Refer to <<esql-agg-count-distinct-approximate>>.
*Description*
Returns the approximate number of distinct values.
[discrete]
[[esql-agg-count-distinct-approximate]]
==== Counts are approximate
Computing exact counts requires loading values into a 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 `COUNT_DISTINCT` function is based on the
https://static.googleusercontent.com/media/research.google.com/fr//pubs/archive/40671.pdf[HyperLogLog++]
algorithm, which counts based on the hashes of the values with some interesting
properties:
include::../../aggregations/metrics/cardinality-aggregation.asciidoc[tag=explanation]
The `COUNT_DISTINCT` function takes an optional second parameter to configure the
precision.
*Supported types*
Can take any field type as input.
*Examples*
[source.merge.styled,esql]
----
include::{esql-specs}/stats_count_distinct.csv-spec[tag=count-distinct]
----
[%header.monospaced.styled,format=dsv,separator=|]
|===
include::{esql-specs}/stats_count_distinct.csv-spec[tag=count-distinct-result]
|===
With the optional second parameter to configure the precision:
[source.merge.styled,esql]
----
include::{esql-specs}/stats_count_distinct.csv-spec[tag=count-distinct-precision]
----
[%header.monospaced.styled,format=dsv,separator=|]
|===
include::{esql-specs}/stats_count_distinct.csv-spec[tag=count-distinct-precision-result]
|===