cumulative-cardinality-aggregation.asciidoc 7.1 KB

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