composite-aggregation.asciidoc 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571
  1. [[search-aggregations-bucket-composite-aggregation]]
  2. === Composite Aggregation
  3. A multi-bucket aggregation that creates composite buckets from different sources.
  4. Unlike the other `multi-bucket` aggregation the `composite` aggregation can be used
  5. to paginate **all** buckets from a multi-level aggregation efficiently. This aggregation
  6. provides a way to stream **all** buckets of a specific aggregation similarly to what
  7. <<search-request-scroll, scroll>> does for documents.
  8. The composite buckets are built from the combinations of the
  9. values extracted/created for each document and each combination is considered as
  10. a composite bucket.
  11. //////////////////////////
  12. [source,js]
  13. --------------------------------------------------
  14. PUT /sales
  15. {
  16. "mappings": {
  17. "properties": {
  18. "product": {
  19. "type": "keyword"
  20. },
  21. "timestamp": {
  22. "type": "date"
  23. },
  24. "price": {
  25. "type": "long"
  26. },
  27. "shop": {
  28. "type": "keyword"
  29. }
  30. }
  31. }
  32. }
  33. POST /sales/_bulk?refresh
  34. {"index":{"_id":0}}
  35. {"product": "mad max", "price": "20", "timestamp": "2017-05-09T14:35"}
  36. {"index":{"_id":1}}
  37. {"product": "mad max", "price": "25", "timestamp": "2017-05-09T12:35"}
  38. {"index":{"_id":2}}
  39. {"product": "rocky", "price": "10", "timestamp": "2017-05-08T09:10"}
  40. {"index":{"_id":3}}
  41. {"product": "mad max", "price": "27", "timestamp": "2017-05-10T07:07"}
  42. {"index":{"_id":4}}
  43. {"product": "apocalypse now", "price": "10", "timestamp": "2017-05-11T08:35"}
  44. -------------------------------------------------
  45. // NOTCONSOLE
  46. // TESTSETUP
  47. //////////////////////////
  48. For instance the following document:
  49. [source,js]
  50. --------------------------------------------------
  51. {
  52. "keyword": ["foo", "bar"],
  53. "number": [23, 65, 76]
  54. }
  55. --------------------------------------------------
  56. // NOTCONSOLE
  57. \... creates the following composite buckets when `keyword` and `number` are used as values source
  58. for the aggregation:
  59. [source,js]
  60. --------------------------------------------------
  61. { "keyword": "foo", "number": 23 }
  62. { "keyword": "foo", "number": 65 }
  63. { "keyword": "foo", "number": 76 }
  64. { "keyword": "bar", "number": 23 }
  65. { "keyword": "bar", "number": 65 }
  66. { "keyword": "bar", "number": 76 }
  67. --------------------------------------------------
  68. // NOTCONSOLE
  69. ==== Values source
  70. The `sources` parameter controls the sources that should be used to build the composite buckets.
  71. There are three different types of values source:
  72. ===== Terms
  73. The `terms` value source is equivalent to a simple `terms` aggregation.
  74. The values are extracted from a field or a script exactly like the `terms` aggregation.
  75. Example:
  76. [source,js]
  77. --------------------------------------------------
  78. GET /_search
  79. {
  80. "aggs" : {
  81. "my_buckets": {
  82. "composite" : {
  83. "sources" : [
  84. { "product": { "terms" : { "field": "product" } } }
  85. ]
  86. }
  87. }
  88. }
  89. }
  90. --------------------------------------------------
  91. // CONSOLE
  92. Like the `terms` aggregation it is also possible to use a script to create the values for the composite buckets:
  93. [source,js]
  94. --------------------------------------------------
  95. GET /_search
  96. {
  97. "aggs" : {
  98. "my_buckets": {
  99. "composite" : {
  100. "sources" : [
  101. {
  102. "product": {
  103. "terms" : {
  104. "script" : {
  105. "source": "doc['product'].value",
  106. "lang": "painless"
  107. }
  108. }
  109. }
  110. }
  111. ]
  112. }
  113. }
  114. }
  115. }
  116. --------------------------------------------------
  117. // CONSOLE
  118. ===== Histogram
  119. The `histogram` value source can be applied on numeric values to build fixed size
  120. interval over the values. The `interval` parameter defines how the numeric values should be
  121. transformed. For instance an `interval` set to 5 will translate any numeric values to its closest interval,
  122. a value of `101` would be translated to `100` which is the key for the interval between 100 and 105.
  123. Example:
  124. [source,js]
  125. --------------------------------------------------
  126. GET /_search
  127. {
  128. "aggs" : {
  129. "my_buckets": {
  130. "composite" : {
  131. "sources" : [
  132. { "histo": { "histogram" : { "field": "price", "interval": 5 } } }
  133. ]
  134. }
  135. }
  136. }
  137. }
  138. --------------------------------------------------
  139. // CONSOLE
  140. The values are built from a numeric field or a script that return numerical values:
  141. [source,js]
  142. --------------------------------------------------
  143. GET /_search
  144. {
  145. "aggs" : {
  146. "my_buckets": {
  147. "composite" : {
  148. "sources" : [
  149. {
  150. "histo": {
  151. "histogram" : {
  152. "interval": 5,
  153. "script" : {
  154. "source": "doc['price'].value",
  155. "lang": "painless"
  156. }
  157. }
  158. }
  159. }
  160. ]
  161. }
  162. }
  163. }
  164. }
  165. --------------------------------------------------
  166. // CONSOLE
  167. ===== Date Histogram
  168. The `date_histogram` is similar to the `histogram` value source except that the interval
  169. is specified by date/time expression:
  170. [source,js]
  171. --------------------------------------------------
  172. GET /_search
  173. {
  174. "aggs" : {
  175. "my_buckets": {
  176. "composite" : {
  177. "sources" : [
  178. { "date": { "date_histogram" : { "field": "timestamp", "interval": "1d" } } }
  179. ]
  180. }
  181. }
  182. }
  183. }
  184. --------------------------------------------------
  185. // CONSOLE
  186. The example above creates an interval per day and translates all `timestamp` values to the start of its closest intervals.
  187. Available expressions for interval: `year`, `quarter`, `month`, `week`, `day`, `hour`, `minute`, `second`
  188. Time values can also be specified via abbreviations supported by <<time-units,time units>> parsing.
  189. Note that fractional time values are not supported, but you can address this by shifting to another
  190. time unit (e.g., `1.5h` could instead be specified as `90m`).
  191. *Format*
  192. Internally, a date is represented as a 64 bit number representing a timestamp in milliseconds-since-the-epoch.
  193. These timestamps are returned as the bucket keys. It is possible to return a formatted date string instead using
  194. the format specified with the format parameter:
  195. [source,js]
  196. --------------------------------------------------
  197. GET /_search
  198. {
  199. "aggs" : {
  200. "my_buckets": {
  201. "composite" : {
  202. "sources" : [
  203. {
  204. "date": {
  205. "date_histogram" : {
  206. "field": "timestamp",
  207. "interval": "1d",
  208. "format": "yyyy-MM-dd" <1>
  209. }
  210. }
  211. }
  212. ]
  213. }
  214. }
  215. }
  216. }
  217. --------------------------------------------------
  218. // CONSOLE
  219. <1> Supports expressive date <<date-format-pattern,format pattern>>
  220. *Time Zone*
  221. Date-times are stored in Elasticsearch in UTC. By default, all bucketing and
  222. rounding is also done in UTC. The `time_zone` parameter can be used to indicate
  223. that bucketing should use a different time zone.
  224. Time zones may either be specified as an ISO 8601 UTC offset (e.g. `+01:00` or
  225. `-08:00`) or as a timezone id, an identifier used in the TZ database like
  226. `America/Los_Angeles`.
  227. ===== Mixing different values source
  228. The `sources` parameter accepts an array of values source.
  229. It is possible to mix different values source to create composite buckets.
  230. For example:
  231. [source,js]
  232. --------------------------------------------------
  233. GET /_search
  234. {
  235. "aggs" : {
  236. "my_buckets": {
  237. "composite" : {
  238. "sources" : [
  239. { "date": { "date_histogram": { "field": "timestamp", "interval": "1d" } } },
  240. { "product": { "terms": {"field": "product" } } }
  241. ]
  242. }
  243. }
  244. }
  245. }
  246. --------------------------------------------------
  247. // CONSOLE
  248. This will create composite buckets from the values created by two values source, a `date_histogram` and a `terms`.
  249. Each bucket is composed of two values, one for each value source defined in the aggregation.
  250. Any type of combinations is allowed and the order in the array is preserved
  251. in the composite buckets.
  252. [source,js]
  253. --------------------------------------------------
  254. GET /_search
  255. {
  256. "aggs" : {
  257. "my_buckets": {
  258. "composite" : {
  259. "sources" : [
  260. { "shop": { "terms": {"field": "shop" } } },
  261. { "product": { "terms": { "field": "product" } } },
  262. { "date": { "date_histogram": { "field": "timestamp", "interval": "1d" } } }
  263. ]
  264. }
  265. }
  266. }
  267. }
  268. --------------------------------------------------
  269. // CONSOLE
  270. ==== Order
  271. By default the composite buckets are sorted by their natural ordering. Values are sorted
  272. in ascending order of their values. When multiple value sources are requested, the ordering is done per value
  273. source, the first value of the composite bucket is compared to the first value of the other composite bucket and if they are equals the
  274. next values in the composite bucket are used for tie-breaking. This means that the composite bucket
  275. `[foo, 100]` is considered smaller than `[foobar, 0]` because `foo` is considered smaller than `foobar`.
  276. It is possible to define the direction of the sort for each value source by setting `order` to `asc` (default value)
  277. or `desc` (descending order) directly in the value source definition.
  278. For example:
  279. [source,js]
  280. --------------------------------------------------
  281. GET /_search
  282. {
  283. "aggs" : {
  284. "my_buckets": {
  285. "composite" : {
  286. "sources" : [
  287. { "date": { "date_histogram": { "field": "timestamp", "interval": "1d", "order": "desc" } } },
  288. { "product": { "terms": {"field": "product", "order": "asc" } } }
  289. ]
  290. }
  291. }
  292. }
  293. }
  294. --------------------------------------------------
  295. // CONSOLE
  296. \... will sort the composite bucket in descending order when comparing values from the `date_histogram` source
  297. and in ascending order when comparing values from the `terms` source.
  298. ==== Missing bucket
  299. By default documents without a value for a given source are ignored.
  300. It is possible to include them in the response by setting `missing_bucket` to
  301. `true` (defaults to `false`):
  302. [source,js]
  303. --------------------------------------------------
  304. GET /_search
  305. {
  306. "aggs" : {
  307. "my_buckets": {
  308. "composite" : {
  309. "sources" : [
  310. { "product_name": { "terms" : { "field": "product", "missing_bucket": true } } }
  311. ]
  312. }
  313. }
  314. }
  315. }
  316. --------------------------------------------------
  317. // CONSOLE
  318. In the example above the source `product_name` will emit an explicit `null` value
  319. for documents without a value for the field `product`.
  320. The `order` specified in the source dictates whether the `null` values should rank
  321. first (ascending order, `asc`) or last (descending order, `desc`).
  322. ==== Size
  323. The `size` parameter can be set to define how many composite buckets should be returned.
  324. Each composite bucket is considered as a single bucket so setting a size of 10 will return the
  325. first 10 composite buckets created from the values source.
  326. The response contains the values for each composite bucket in an array containing the values extracted
  327. from each value source.
  328. ==== After
  329. If the number of composite buckets is too high (or unknown) to be returned in a single response
  330. it is possible to split the retrieval in multiple requests.
  331. Since the composite buckets are flat by nature, the requested `size` is exactly the number of composite buckets
  332. that will be returned in the response (assuming that they are at least `size` composite buckets to return).
  333. If all composite buckets should be retrieved it is preferable to use a small size (`100` or `1000` for instance)
  334. and then use the `after` parameter to retrieve the next results.
  335. For example:
  336. [source,js]
  337. --------------------------------------------------
  338. GET /_search
  339. {
  340. "aggs" : {
  341. "my_buckets": {
  342. "composite" : {
  343. "size": 2,
  344. "sources" : [
  345. { "date": { "date_histogram": { "field": "timestamp", "interval": "1d" } } },
  346. { "product": { "terms": {"field": "product" } } }
  347. ]
  348. }
  349. }
  350. }
  351. }
  352. --------------------------------------------------
  353. // CONSOLE
  354. // TEST[s/_search/_search\?filter_path=aggregations/]
  355. \... returns:
  356. [source,js]
  357. --------------------------------------------------
  358. {
  359. ...
  360. "aggregations": {
  361. "my_buckets": {
  362. "after_key": { <1>
  363. "date": 1494288000000,
  364. "product": "mad max"
  365. },
  366. "buckets": [
  367. {
  368. "key": {
  369. "date": 1494201600000,
  370. "product": "rocky"
  371. },
  372. "doc_count": 1
  373. },
  374. {
  375. "key": {
  376. "date": 1494288000000,
  377. "product": "mad max"
  378. },
  379. "doc_count": 2
  380. }
  381. ]
  382. }
  383. }
  384. }
  385. --------------------------------------------------
  386. // TESTRESPONSE[s/\.\.\.//]
  387. <1> The last composite bucket returned by the query.
  388. NOTE: The `after_key` is equals to the last bucket returned in the response before
  389. any filtering that could be done by <<search-aggregations-pipeline, Pipeline aggregations>>.
  390. If all buckets are filtered/removed by a pipeline aggregation, the `after_key` will contain
  391. the last bucket before filtering.
  392. The `after` parameter can be used to retrieve the composite buckets that are **after**
  393. the last composite buckets returned in a previous round.
  394. For the example below the last bucket can be found in `after_key` and the next
  395. round of result can be retrieved with:
  396. [source,js]
  397. --------------------------------------------------
  398. GET /_search
  399. {
  400. "aggs" : {
  401. "my_buckets": {
  402. "composite" : {
  403. "size": 2,
  404. "sources" : [
  405. { "date": { "date_histogram": { "field": "timestamp", "interval": "1d", "order": "desc" } } },
  406. { "product": { "terms": {"field": "product", "order": "asc" } } }
  407. ],
  408. "after": { "date": 1494288000000, "product": "mad max" } <1>
  409. }
  410. }
  411. }
  412. }
  413. --------------------------------------------------
  414. // CONSOLE
  415. <1> Should restrict the aggregation to buckets that sort **after** the provided values.
  416. ==== Sub-aggregations
  417. Like any `multi-bucket` aggregations the `composite` aggregation can hold sub-aggregations.
  418. These sub-aggregations can be used to compute other buckets or statistics on each composite bucket created by this
  419. parent aggregation.
  420. For instance the following example computes the average value of a field
  421. per composite bucket:
  422. [source,js]
  423. --------------------------------------------------
  424. GET /_search
  425. {
  426. "aggs" : {
  427. "my_buckets": {
  428. "composite" : {
  429. "sources" : [
  430. { "date": { "date_histogram": { "field": "timestamp", "interval": "1d", "order": "desc" } } },
  431. { "product": { "terms": {"field": "product" } } }
  432. ]
  433. },
  434. "aggregations": {
  435. "the_avg": {
  436. "avg": { "field": "price" }
  437. }
  438. }
  439. }
  440. }
  441. }
  442. --------------------------------------------------
  443. // CONSOLE
  444. // TEST[s/_search/_search\?filter_path=aggregations/]
  445. \... returns:
  446. [source,js]
  447. --------------------------------------------------
  448. {
  449. ...
  450. "aggregations": {
  451. "my_buckets": {
  452. "after_key": {
  453. "date": 1494201600000,
  454. "product": "rocky"
  455. },
  456. "buckets": [
  457. {
  458. "key": {
  459. "date": 1494460800000,
  460. "product": "apocalypse now"
  461. },
  462. "doc_count": 1,
  463. "the_avg": {
  464. "value": 10.0
  465. }
  466. },
  467. {
  468. "key": {
  469. "date": 1494374400000,
  470. "product": "mad max"
  471. },
  472. "doc_count": 1,
  473. "the_avg": {
  474. "value": 27.0
  475. }
  476. },
  477. {
  478. "key": {
  479. "date": 1494288000000,
  480. "product" : "mad max"
  481. },
  482. "doc_count": 2,
  483. "the_avg": {
  484. "value": 22.5
  485. }
  486. },
  487. {
  488. "key": {
  489. "date": 1494201600000,
  490. "product": "rocky"
  491. },
  492. "doc_count": 1,
  493. "the_avg": {
  494. "value": 10.0
  495. }
  496. }
  497. ]
  498. }
  499. }
  500. }
  501. --------------------------------------------------
  502. // TESTRESPONSE[s/\.\.\.//]