cardinality-aggregation.asciidoc 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264
  1. [[search-aggregations-metrics-cardinality-aggregation]]
  2. === Cardinality aggregation
  3. ++++
  4. <titleabbrev>Cardinality</titleabbrev>
  5. ++++
  6. A `single-value` metrics aggregation that calculates an approximate count of
  7. distinct values.
  8. Assume you are indexing store sales and would like to count the unique number of sold products that match a query:
  9. [source,console]
  10. --------------------------------------------------
  11. POST /sales/_search?size=0
  12. {
  13. "aggs": {
  14. "type_count": {
  15. "cardinality": {
  16. "field": "type"
  17. }
  18. }
  19. }
  20. }
  21. --------------------------------------------------
  22. // TEST[setup:sales]
  23. Response:
  24. [source,console-result]
  25. --------------------------------------------------
  26. {
  27. ...
  28. "aggregations": {
  29. "type_count": {
  30. "value": 3
  31. }
  32. }
  33. }
  34. --------------------------------------------------
  35. // TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]
  36. ==== Precision control
  37. This aggregation also supports the `precision_threshold` option:
  38. [source,console]
  39. --------------------------------------------------
  40. POST /sales/_search?size=0
  41. {
  42. "aggs": {
  43. "type_count": {
  44. "cardinality": {
  45. "field": "type",
  46. "precision_threshold": 100 <1>
  47. }
  48. }
  49. }
  50. }
  51. --------------------------------------------------
  52. // TEST[setup:sales]
  53. <1> The `precision_threshold` options allows to trade memory for accuracy, and
  54. defines a unique count below which counts are expected to be close to
  55. accurate. Above this value, counts might become a bit more fuzzy. The maximum
  56. supported value is 40000, thresholds above this number will have the same
  57. effect as a threshold of 40000. The default value is +3000+.
  58. ==== Counts are approximate
  59. Computing exact counts requires loading values into a hash set and returning its
  60. size. This doesn't scale when working on high-cardinality sets and/or large
  61. values as the required memory usage and the need to communicate those
  62. per-shard sets between nodes would utilize too many resources of the cluster.
  63. This `cardinality` aggregation is based on the
  64. https://static.googleusercontent.com/media/research.google.com/fr//pubs/archive/40671.pdf[HyperLogLog++]
  65. algorithm, which counts based on the hashes of the values with some interesting
  66. properties:
  67. // tag::explanation[]
  68. * configurable precision, which decides on how to trade memory for accuracy,
  69. * excellent accuracy on low-cardinality sets,
  70. * fixed memory usage: no matter if there are tens or billions of unique values,
  71. memory usage only depends on the configured precision.
  72. For a precision threshold of `c`, the implementation that we are using requires
  73. about `c * 8` bytes.
  74. The following chart shows how the error varies before and after the threshold:
  75. ////
  76. To generate this chart use this gnuplot script:
  77. [source,gnuplot]
  78. -------
  79. #!/usr/bin/gnuplot
  80. reset
  81. set terminal png size 1000,400
  82. set xlabel "Actual cardinality"
  83. set logscale x
  84. set ylabel "Relative error (%)"
  85. set yrange [0:8]
  86. set title "Cardinality error"
  87. set grid
  88. set style data lines
  89. plot "test.dat" using 1:2 title "threshold=100", \
  90. "" using 1:3 title "threshold=1000", \
  91. "" using 1:4 title "threshold=10000"
  92. #
  93. -------
  94. and generate data in a 'test.dat' file using the below Java code:
  95. [source,java]
  96. -------
  97. private static double error(HyperLogLogPlusPlus h, long expected) {
  98. double actual = h.cardinality(0);
  99. return Math.abs(expected - actual) / expected;
  100. }
  101. public static void main(String[] args) {
  102. HyperLogLogPlusPlus h100 = new HyperLogLogPlusPlus(precisionFromThreshold(100), BigArrays.NON_RECYCLING_INSTANCE, 1);
  103. HyperLogLogPlusPlus h1000 = new HyperLogLogPlusPlus(precisionFromThreshold(1000), BigArrays.NON_RECYCLING_INSTANCE, 1);
  104. HyperLogLogPlusPlus h10000 = new HyperLogLogPlusPlus(precisionFromThreshold(10000), BigArrays.NON_RECYCLING_INSTANCE, 1);
  105. int next = 100;
  106. int step = 10;
  107. for (int i = 1; i <= 10000000; ++i) {
  108. long h = BitMixer.mix64(i);
  109. h100.collect(0, h);
  110. h1000.collect(0, h);
  111. h10000.collect(0, h);
  112. if (i == next) {
  113. System.out.println(i + " " + error(h100, i)*100 + " " + error(h1000, i)*100 + " " + error(h10000, i)*100);
  114. next += step;
  115. if (next >= 100 * step) {
  116. step *= 10;
  117. }
  118. }
  119. }
  120. }
  121. -------
  122. ////
  123. image:images/cardinality_error.png[]
  124. For all 3 thresholds, counts have been accurate up to the configured threshold.
  125. Although not guaranteed, this is likely to be the case. Accuracy in practice depends
  126. on the dataset in question. In general, most datasets show consistently good
  127. accuracy. Also note that even with a threshold as low as 100, the error
  128. remains very low (1-6% as seen in the above graph) even when counting millions of items.
  129. The HyperLogLog++ algorithm depends on the leading zeros of hashed
  130. values, the exact distributions of hashes in a dataset can affect the
  131. accuracy of the cardinality.
  132. // end::explanation[]
  133. ==== Pre-computed hashes
  134. On string fields that have a high cardinality, it might be faster to store the
  135. hash of your field values in your index and then run the cardinality aggregation
  136. on this field. This can either be done by providing hash values from client-side
  137. or by letting Elasticsearch compute hash values for you by using the
  138. {plugins}/mapper-murmur3.html[`mapper-murmur3`] plugin.
  139. NOTE: Pre-computing hashes is usually only useful on very large and/or
  140. high-cardinality fields as it saves CPU and memory. However, on numeric
  141. fields, hashing is very fast and storing the original values requires as much
  142. or less memory than storing the hashes. This is also true on low-cardinality
  143. string fields, especially given that those have an optimization in order to
  144. make sure that hashes are computed at most once per unique value per segment.
  145. ==== Script
  146. If you need the cardinality of the combination of two fields,
  147. create a <<runtime,runtime field>> combining them and aggregate it.
  148. [source,console]
  149. ----
  150. POST /sales/_search?size=0
  151. {
  152. "runtime_mappings": {
  153. "type_and_promoted": {
  154. "type": "keyword",
  155. "script": "emit(doc['type'].value + ' ' + doc['promoted'].value)"
  156. }
  157. },
  158. "aggs": {
  159. "type_promoted_count": {
  160. "cardinality": {
  161. "field": "type_and_promoted"
  162. }
  163. }
  164. }
  165. }
  166. ----
  167. // TEST[setup:sales]
  168. // TEST[s/size=0/size=0&filter_path=aggregations/]
  169. ////
  170. [source,console-result]
  171. --------------------------------------------------
  172. {
  173. "aggregations": {
  174. "type_promoted_count": {
  175. "value": 5
  176. }
  177. }
  178. }
  179. --------------------------------------------------
  180. ////
  181. ==== Missing value
  182. The `missing` parameter defines how documents that are missing a value should be treated.
  183. By default they will be ignored but it is also possible to treat them as if they
  184. had a value.
  185. [source,console]
  186. --------------------------------------------------
  187. POST /sales/_search?size=0
  188. {
  189. "aggs": {
  190. "tag_cardinality": {
  191. "cardinality": {
  192. "field": "tag",
  193. "missing": "N/A" <1>
  194. }
  195. }
  196. }
  197. }
  198. --------------------------------------------------
  199. // TEST[setup:sales]
  200. <1> Documents without a value in the `tag` field will fall into the same bucket as documents that have the value `N/A`.
  201. ==== Execution Hint
  202. There are different mechanisms by which cardinality aggregations can be executed:
  203. - by using field values directly (`direct`)
  204. - by using global ordinals of the field and resolving those values after
  205. finishing a shard (`global_ordinals`)
  206. - by using segment ordinal values and resolving those values after each
  207. segment (`segment_ordinals`)
  208. Additionally, there are two "heuristic based" modes. These modes will cause
  209. Elasticsearch to use some data about the state of the index to choose an
  210. appropriate execution method. The two heuristics are:
  211. - `save_time_heuristic` - this is the default in Elasticsearch 8.4 and later.
  212. - `save_memory_heuristic` - this was the default in Elasticsearch 8.3 and
  213. earlier
  214. When not specified, Elasticsearch will apply a heuristic to chose the
  215. appropriate mode. Also note that some data (i.e. non-ordinal fields), `direct`
  216. is the only option, and the hint will be ignored in these cases. Generally
  217. speaking, it should not be necessary to set this value.