cumulative-cardinality-aggregation.asciidoc 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. [role="xpack"]
  2. [[search-aggregations-pipeline-cumulative-cardinality-aggregation]]
  3. === Cumulative cardinality aggregation
  4. ++++
  5. <titleabbrev>Cumulative cardinality</titleabbrev>
  6. ++++
  7. A parent pipeline aggregation which calculates the Cumulative Cardinality in a parent histogram (or date_histogram)
  8. aggregation. The specified metric must be a cardinality aggregation and the enclosing histogram
  9. must have `min_doc_count` set to `0` (default for `histogram` aggregations).
  10. The `cumulative_cardinality` agg is useful for finding "total new items", like the number of new visitors to your
  11. website each day. A regular cardinality aggregation will tell you how many unique visitors came each day, but doesn't
  12. differentiate between "new" or "repeat" visitors. The Cumulative Cardinality aggregation can be used to determine
  13. how many of each day's unique visitors are "new".
  14. ==== Syntax
  15. A `cumulative_cardinality` aggregation looks like this in isolation:
  16. [source,js]
  17. --------------------------------------------------
  18. {
  19. "cumulative_cardinality": {
  20. "buckets_path": "my_cardinality_agg"
  21. }
  22. }
  23. --------------------------------------------------
  24. // NOTCONSOLE
  25. [[cumulative-cardinality-params]]
  26. .`cumulative_cardinality` Parameters
  27. [options="header"]
  28. |===
  29. |Parameter Name |Description |Required |Default Value
  30. |`buckets_path` |The path to the cardinality aggregation we wish to find the cumulative cardinality for (see <<buckets-path-syntax>> for more
  31. details) |Required |
  32. |`format` |format to apply to the output value of this aggregation |Optional |`null`
  33. |===
  34. The following snippet calculates the cumulative cardinality of the total daily `users`:
  35. [source,console]
  36. --------------------------------------------------
  37. GET /user_hits/_search
  38. {
  39. "size": 0,
  40. "aggs": {
  41. "users_per_day": {
  42. "date_histogram": {
  43. "field": "timestamp",
  44. "calendar_interval": "day"
  45. },
  46. "aggs": {
  47. "distinct_users": {
  48. "cardinality": {
  49. "field": "user_id"
  50. }
  51. },
  52. "total_new_users": {
  53. "cumulative_cardinality": {
  54. "buckets_path": "distinct_users" <1>
  55. }
  56. }
  57. }
  58. }
  59. }
  60. }
  61. --------------------------------------------------
  62. // TEST[setup:user_hits]
  63. <1> `buckets_path` instructs this aggregation to use the output of the `distinct_users` aggregation for the cumulative cardinality
  64. And the following may be the response:
  65. [source,console-result]
  66. --------------------------------------------------
  67. {
  68. "took": 11,
  69. "timed_out": false,
  70. "_shards": ...,
  71. "hits": ...,
  72. "aggregations": {
  73. "users_per_day": {
  74. "buckets": [
  75. {
  76. "key_as_string": "2019-01-01T00:00:00.000Z",
  77. "key": 1546300800000,
  78. "doc_count": 2,
  79. "distinct_users": {
  80. "value": 2
  81. },
  82. "total_new_users": {
  83. "value": 2
  84. }
  85. },
  86. {
  87. "key_as_string": "2019-01-02T00:00:00.000Z",
  88. "key": 1546387200000,
  89. "doc_count": 2,
  90. "distinct_users": {
  91. "value": 2
  92. },
  93. "total_new_users": {
  94. "value": 3
  95. }
  96. },
  97. {
  98. "key_as_string": "2019-01-03T00:00:00.000Z",
  99. "key": 1546473600000,
  100. "doc_count": 3,
  101. "distinct_users": {
  102. "value": 3
  103. },
  104. "total_new_users": {
  105. "value": 4
  106. }
  107. }
  108. ]
  109. }
  110. }
  111. }
  112. --------------------------------------------------
  113. // TESTRESPONSE[s/"took": 11/"took": $body.took/]
  114. // TESTRESPONSE[s/"_shards": \.\.\./"_shards": $body._shards/]
  115. // TESTRESPONSE[s/"hits": \.\.\./"hits": $body.hits/]
  116. Note how the second day, `2019-01-02`, has two distinct users but the `total_new_users` metric generated by the
  117. cumulative pipeline agg only increments to three. This means that only one of the two users that day were
  118. new, the other had already been seen in the previous day. This happens again on the third day, where only
  119. one of three users is completely new.
  120. ==== Incremental cumulative cardinality
  121. The `cumulative_cardinality` agg will show you the total, distinct count since the beginning of the time period
  122. being queried. Sometimes, however, it is useful to see the "incremental" count. Meaning, how many new users
  123. are added each day, rather than the total cumulative count.
  124. This can be accomplished by adding a `derivative` aggregation to our query:
  125. [source,console]
  126. --------------------------------------------------
  127. GET /user_hits/_search
  128. {
  129. "size": 0,
  130. "aggs": {
  131. "users_per_day": {
  132. "date_histogram": {
  133. "field": "timestamp",
  134. "calendar_interval": "day"
  135. },
  136. "aggs": {
  137. "distinct_users": {
  138. "cardinality": {
  139. "field": "user_id"
  140. }
  141. },
  142. "total_new_users": {
  143. "cumulative_cardinality": {
  144. "buckets_path": "distinct_users"
  145. }
  146. },
  147. "incremental_new_users": {
  148. "derivative": {
  149. "buckets_path": "total_new_users"
  150. }
  151. }
  152. }
  153. }
  154. }
  155. }
  156. --------------------------------------------------
  157. // TEST[setup:user_hits]
  158. And the following may be the response:
  159. [source,console-result]
  160. --------------------------------------------------
  161. {
  162. "took": 11,
  163. "timed_out": false,
  164. "_shards": ...,
  165. "hits": ...,
  166. "aggregations": {
  167. "users_per_day": {
  168. "buckets": [
  169. {
  170. "key_as_string": "2019-01-01T00:00:00.000Z",
  171. "key": 1546300800000,
  172. "doc_count": 2,
  173. "distinct_users": {
  174. "value": 2
  175. },
  176. "total_new_users": {
  177. "value": 2
  178. }
  179. },
  180. {
  181. "key_as_string": "2019-01-02T00:00:00.000Z",
  182. "key": 1546387200000,
  183. "doc_count": 2,
  184. "distinct_users": {
  185. "value": 2
  186. },
  187. "total_new_users": {
  188. "value": 3
  189. },
  190. "incremental_new_users": {
  191. "value": 1.0
  192. }
  193. },
  194. {
  195. "key_as_string": "2019-01-03T00:00:00.000Z",
  196. "key": 1546473600000,
  197. "doc_count": 3,
  198. "distinct_users": {
  199. "value": 3
  200. },
  201. "total_new_users": {
  202. "value": 4
  203. },
  204. "incremental_new_users": {
  205. "value": 1.0
  206. }
  207. }
  208. ]
  209. }
  210. }
  211. }
  212. --------------------------------------------------
  213. // TESTRESPONSE[s/"took": 11/"took": $body.took/]
  214. // TESTRESPONSE[s/"_shards": \.\.\./"_shards": $body._shards/]
  215. // TESTRESPONSE[s/"hits": \.\.\./"hits": $body.hits/]