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