composite-aggregation.asciidoc 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847
  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. <<request-body-search-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. "nested": {
  31. "type": "nested",
  32. "properties": {
  33. "product": {
  34. "type": "keyword"
  35. },
  36. "timestamp": {
  37. "type": "date"
  38. },
  39. "price": {
  40. "type": "long"
  41. },
  42. "shop": {
  43. "type": "keyword"
  44. }
  45. }
  46. }
  47. }
  48. }
  49. }
  50. POST /sales/_bulk?refresh
  51. {"index":{"_id":0}}
  52. {"product": "mad max", "price": "20", "timestamp": "2017-05-09T14:35"}
  53. {"index":{"_id":1}}
  54. {"product": "mad max", "price": "25", "timestamp": "2017-05-09T12:35"}
  55. {"index":{"_id":2}}
  56. {"product": "rocky", "price": "10", "timestamp": "2017-05-08T09:10"}
  57. {"index":{"_id":3}}
  58. {"product": "mad max", "price": "27", "timestamp": "2017-05-10T07:07"}
  59. {"index":{"_id":4}}
  60. {"product": "apocalypse now", "price": "10", "timestamp": "2017-05-11T08:35"}
  61. -------------------------------------------------
  62. // NOTCONSOLE
  63. // TESTSETUP
  64. //////////////////////////
  65. For instance the following document:
  66. [source,js]
  67. --------------------------------------------------
  68. {
  69. "keyword": ["foo", "bar"],
  70. "number": [23, 65, 76]
  71. }
  72. --------------------------------------------------
  73. // NOTCONSOLE
  74. \... creates the following composite buckets when `keyword` and `number` are used as values source
  75. for the aggregation:
  76. [source,js]
  77. --------------------------------------------------
  78. { "keyword": "foo", "number": 23 }
  79. { "keyword": "foo", "number": 65 }
  80. { "keyword": "foo", "number": 76 }
  81. { "keyword": "bar", "number": 23 }
  82. { "keyword": "bar", "number": 65 }
  83. { "keyword": "bar", "number": 76 }
  84. --------------------------------------------------
  85. // NOTCONSOLE
  86. ==== Values source
  87. The `sources` parameter controls the sources that should be used to build the composite buckets.
  88. The order that the `sources` are defined is important because it also controls the order
  89. the keys are returned.
  90. The name given to each sources must be unique.
  91. There are three different types of values source:
  92. [[_terms]]
  93. ===== Terms
  94. The `terms` value source is equivalent to a simple `terms` aggregation.
  95. The values are extracted from a field or a script exactly like the `terms` aggregation.
  96. Example:
  97. [source,console,id=composite-aggregation-terms-field-example]
  98. --------------------------------------------------
  99. GET /_search
  100. {
  101. "size": 0,
  102. "aggs" : {
  103. "my_buckets": {
  104. "composite" : {
  105. "sources" : [
  106. { "product": { "terms" : { "field": "product" } } }
  107. ]
  108. }
  109. }
  110. }
  111. }
  112. --------------------------------------------------
  113. Like the `terms` aggregation it is also possible to use a script to create the values for the composite buckets:
  114. [source,console,id=composite-aggregation-terms-script-example]
  115. --------------------------------------------------
  116. GET /_search
  117. {
  118. "size": 0,
  119. "aggs" : {
  120. "my_buckets": {
  121. "composite" : {
  122. "sources" : [
  123. {
  124. "product": {
  125. "terms" : {
  126. "script" : {
  127. "source": "doc['product'].value",
  128. "lang": "painless"
  129. }
  130. }
  131. }
  132. }
  133. ]
  134. }
  135. }
  136. }
  137. }
  138. --------------------------------------------------
  139. [[_histogram]]
  140. ===== Histogram
  141. The `histogram` value source can be applied on numeric values to build fixed size
  142. interval over the values. The `interval` parameter defines how the numeric values should be
  143. transformed. For instance an `interval` set to 5 will translate any numeric values to its closest interval,
  144. a value of `101` would be translated to `100` which is the key for the interval between 100 and 105.
  145. Example:
  146. [source,console,id=composite-aggregation-histogram-field-example]
  147. --------------------------------------------------
  148. GET /_search
  149. {
  150. "size": 0,
  151. "aggs" : {
  152. "my_buckets": {
  153. "composite" : {
  154. "sources" : [
  155. { "histo": { "histogram" : { "field": "price", "interval": 5 } } }
  156. ]
  157. }
  158. }
  159. }
  160. }
  161. --------------------------------------------------
  162. The values are built from a numeric field or a script that return numerical values:
  163. [source,console,id=composite-aggregation-histogram-script-example]
  164. --------------------------------------------------
  165. GET /_search
  166. {
  167. "size": 0,
  168. "aggs" : {
  169. "my_buckets": {
  170. "composite" : {
  171. "sources" : [
  172. {
  173. "histo": {
  174. "histogram" : {
  175. "interval": 5,
  176. "script" : {
  177. "source": "doc['price'].value",
  178. "lang": "painless"
  179. }
  180. }
  181. }
  182. }
  183. ]
  184. }
  185. }
  186. }
  187. }
  188. --------------------------------------------------
  189. [[_date_histogram]]
  190. ===== Date histogram
  191. The `date_histogram` is similar to the `histogram` value source except that the interval
  192. is specified by date/time expression:
  193. [source,console,id=composite-aggregation-datehistogram-example]
  194. --------------------------------------------------
  195. GET /_search
  196. {
  197. "size": 0,
  198. "aggs" : {
  199. "my_buckets": {
  200. "composite" : {
  201. "sources" : [
  202. { "date": { "date_histogram" : { "field": "timestamp", "calendar_interval": "1d" } } }
  203. ]
  204. }
  205. }
  206. }
  207. }
  208. --------------------------------------------------
  209. The example above creates an interval per day and translates all `timestamp` values to the start of its closest intervals.
  210. Available expressions for interval: `year`, `quarter`, `month`, `week`, `day`, `hour`, `minute`, `second`
  211. Time values can also be specified via abbreviations supported by <<time-units,time units>> parsing.
  212. Note that fractional time values are not supported, but you can address this by shifting to another
  213. time unit (e.g., `1.5h` could instead be specified as `90m`).
  214. *Format*
  215. Internally, a date is represented as a 64 bit number representing a timestamp in milliseconds-since-the-epoch.
  216. These timestamps are returned as the bucket keys. It is possible to return a formatted date string instead using
  217. the format specified with the format parameter:
  218. [source,console,id=composite-aggregation-datehistogram-format-example]
  219. --------------------------------------------------
  220. GET /_search
  221. {
  222. "size": 0,
  223. "aggs" : {
  224. "my_buckets": {
  225. "composite" : {
  226. "sources" : [
  227. {
  228. "date": {
  229. "date_histogram" : {
  230. "field": "timestamp",
  231. "calendar_interval": "1d",
  232. "format": "yyyy-MM-dd" <1>
  233. }
  234. }
  235. }
  236. ]
  237. }
  238. }
  239. }
  240. }
  241. --------------------------------------------------
  242. <1> Supports expressive date <<date-format-pattern,format pattern>>
  243. *Time Zone*
  244. Date-times are stored in Elasticsearch in UTC. By default, all bucketing and
  245. rounding is also done in UTC. The `time_zone` parameter can be used to indicate
  246. that bucketing should use a different time zone.
  247. Time zones may either be specified as an ISO 8601 UTC offset (e.g. `+01:00` or
  248. `-08:00`) or as a timezone id, an identifier used in the TZ database like
  249. `America/Los_Angeles`.
  250. *Offset*
  251. include::datehistogram-aggregation.asciidoc[tag=offset-explanation]
  252. [source,console,id=composite-aggregation-datehistogram-offset-example]
  253. ----
  254. PUT my_index/_doc/1?refresh
  255. {
  256. "date": "2015-10-01T05:30:00Z"
  257. }
  258. PUT my_index/_doc/2?refresh
  259. {
  260. "date": "2015-10-01T06:30:00Z"
  261. }
  262. GET my_index/_search?size=0
  263. {
  264. "aggs": {
  265. "my_buckets": {
  266. "composite" : {
  267. "sources" : [
  268. {
  269. "date": {
  270. "date_histogram" : {
  271. "field": "date",
  272. "calendar_interval": "day",
  273. "offset": "+6h",
  274. "format": "iso8601"
  275. }
  276. }
  277. }
  278. ]
  279. }
  280. }
  281. }
  282. }
  283. ----
  284. include::datehistogram-aggregation.asciidoc[tag=offset-result-intro]
  285. [source,console-result]
  286. ----
  287. {
  288. ...
  289. "aggregations": {
  290. "my_buckets": {
  291. "after_key": { "date": "2015-10-01T06:00:00.000Z" },
  292. "buckets": [
  293. {
  294. "key": { "date": "2015-09-30T06:00:00.000Z" },
  295. "doc_count": 1
  296. },
  297. {
  298. "key": { "date": "2015-10-01T06:00:00.000Z" },
  299. "doc_count": 1
  300. }
  301. ]
  302. }
  303. }
  304. }
  305. ----
  306. // TESTRESPONSE[s/\.\.\./"took": $body.took,"timed_out": false,"_shards": $body._shards,"hits": $body.hits,/]
  307. include::datehistogram-aggregation.asciidoc[tag=offset-note]
  308. [[_geotile_grid]]
  309. ===== GeoTile grid
  310. The `geotile_grid` value source works on `geo_point` fields and groups points into buckets that represent
  311. cells in a grid. The resulting grid can be sparse and only contains cells
  312. that have matching data. Each cell corresponds to a
  313. https://en.wikipedia.org/wiki/Tiled_web_map[map tile] as used by many online map
  314. sites. Each cell is labeled using a "{zoom}/{x}/{y}" format, where zoom is equal
  315. to the user-specified precision.
  316. [source,console,id=composite-aggregation-geotilegrid-example]
  317. --------------------------------------------------
  318. GET /_search
  319. {
  320. "size": 0,
  321. "aggs" : {
  322. "my_buckets": {
  323. "composite" : {
  324. "sources" : [
  325. { "tile": { "geotile_grid" : { "field": "location", "precision": 8 } } }
  326. ]
  327. }
  328. }
  329. }
  330. }
  331. --------------------------------------------------
  332. *Precision*
  333. The highest-precision geotile of length 29 produces cells that cover
  334. less than 10cm by 10cm of land. This precision is uniquely suited for composite aggregations as each
  335. tile does not have to be generated and loaded in memory.
  336. See https://wiki.openstreetmap.org/wiki/Zoom_levels[Zoom level documentation]
  337. on how precision (zoom) correlates to size on the ground. Precision for this
  338. aggregation can be between 0 and 29, inclusive.
  339. *Bounding box filtering*
  340. The geotile source can optionally be constrained to a specific geo bounding box, which reduces
  341. the range of tiles used. These bounds are useful when only a specific part of a geographical area needs high
  342. precision tiling.
  343. [source,console,id=composite-aggregation-geotilegrid-boundingbox-example]
  344. --------------------------------------------------
  345. GET /_search
  346. {
  347. "size": 0,
  348. "aggs" : {
  349. "my_buckets": {
  350. "composite" : {
  351. "sources" : [
  352. {
  353. "tile": {
  354. "geotile_grid" : {
  355. "field" : "location",
  356. "precision" : 22,
  357. "bounds": {
  358. "top_left" : "52.4, 4.9",
  359. "bottom_right" : "52.3, 5.0"
  360. }
  361. }
  362. }
  363. }
  364. ]
  365. }
  366. }
  367. }
  368. }
  369. --------------------------------------------------
  370. ===== Mixing different values source
  371. The `sources` parameter accepts an array of values source.
  372. It is possible to mix different values source to create composite buckets.
  373. For example:
  374. [source,console,id=composite-aggregation-mixing-sources-example]
  375. --------------------------------------------------
  376. GET /_search
  377. {
  378. "size": 0,
  379. "aggs" : {
  380. "my_buckets": {
  381. "composite" : {
  382. "sources" : [
  383. { "date": { "date_histogram": { "field": "timestamp", "calendar_interval": "1d" } } },
  384. { "product": { "terms": {"field": "product" } } }
  385. ]
  386. }
  387. }
  388. }
  389. }
  390. --------------------------------------------------
  391. This will create composite buckets from the values created by two values source, a `date_histogram` and a `terms`.
  392. Each bucket is composed of two values, one for each value source defined in the aggregation.
  393. Any type of combinations is allowed and the order in the array is preserved
  394. in the composite buckets.
  395. [source,console,id=composite-aggregation-mixing-three-sources-example]
  396. --------------------------------------------------
  397. GET /_search
  398. {
  399. "size": 0,
  400. "aggs" : {
  401. "my_buckets": {
  402. "composite" : {
  403. "sources" : [
  404. { "shop": { "terms": {"field": "shop" } } },
  405. { "product": { "terms": { "field": "product" } } },
  406. { "date": { "date_histogram": { "field": "timestamp", "calendar_interval": "1d" } } }
  407. ]
  408. }
  409. }
  410. }
  411. }
  412. --------------------------------------------------
  413. ==== Order
  414. By default the composite buckets are sorted by their natural ordering. Values are sorted
  415. in ascending order of their values. When multiple value sources are requested, the ordering is done per value
  416. 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
  417. next values in the composite bucket are used for tie-breaking. This means that the composite bucket
  418. `[foo, 100]` is considered smaller than `[foobar, 0]` because `foo` is considered smaller than `foobar`.
  419. It is possible to define the direction of the sort for each value source by setting `order` to `asc` (default value)
  420. or `desc` (descending order) directly in the value source definition.
  421. For example:
  422. [source,console,id=composite-aggregation-order-example]
  423. --------------------------------------------------
  424. GET /_search
  425. {
  426. "size": 0,
  427. "aggs" : {
  428. "my_buckets": {
  429. "composite" : {
  430. "sources" : [
  431. { "date": { "date_histogram": { "field": "timestamp", "calendar_interval": "1d", "order": "desc" } } },
  432. { "product": { "terms": {"field": "product", "order": "asc" } } }
  433. ]
  434. }
  435. }
  436. }
  437. }
  438. --------------------------------------------------
  439. \... will sort the composite bucket in descending order when comparing values from the `date_histogram` source
  440. and in ascending order when comparing values from the `terms` source.
  441. ==== Missing bucket
  442. By default documents without a value for a given source are ignored.
  443. It is possible to include them in the response by setting `missing_bucket` to
  444. `true` (defaults to `false`):
  445. [source,console,id=composite-aggregation-missing-bucket-example]
  446. --------------------------------------------------
  447. GET /_search
  448. {
  449. "size": 0,
  450. "aggs" : {
  451. "my_buckets": {
  452. "composite" : {
  453. "sources" : [
  454. { "product_name": { "terms" : { "field": "product", "missing_bucket": true } } }
  455. ]
  456. }
  457. }
  458. }
  459. }
  460. --------------------------------------------------
  461. In the example above the source `product_name` will emit an explicit `null` value
  462. for documents without a value for the field `product`.
  463. The `order` specified in the source dictates whether the `null` values should rank
  464. first (ascending order, `asc`) or last (descending order, `desc`).
  465. ==== Size
  466. The `size` parameter can be set to define how many composite buckets should be returned.
  467. Each composite bucket is considered as a single bucket so setting a size of 10 will return the
  468. first 10 composite buckets created from the values source.
  469. The response contains the values for each composite bucket in an array containing the values extracted
  470. from each value source.
  471. ==== Pagination
  472. If the number of composite buckets is too high (or unknown) to be returned in a single response
  473. it is possible to split the retrieval in multiple requests.
  474. Since the composite buckets are flat by nature, the requested `size` is exactly the number of composite buckets
  475. that will be returned in the response (assuming that they are at least `size` composite buckets to return).
  476. If all composite buckets should be retrieved it is preferable to use a small size (`100` or `1000` for instance)
  477. and then use the `after` parameter to retrieve the next results.
  478. For example:
  479. [source,console,id=composite-aggregation-after-key-example]
  480. --------------------------------------------------
  481. GET /_search
  482. {
  483. "size": 0,
  484. "aggs" : {
  485. "my_buckets": {
  486. "composite" : {
  487. "size": 2,
  488. "sources" : [
  489. { "date": { "date_histogram": { "field": "timestamp", "calendar_interval": "1d" } } },
  490. { "product": { "terms": {"field": "product" } } }
  491. ]
  492. }
  493. }
  494. }
  495. }
  496. --------------------------------------------------
  497. // TEST[s/_search/_search\?filter_path=aggregations/]
  498. \... returns:
  499. [source,console-result]
  500. --------------------------------------------------
  501. {
  502. ...
  503. "aggregations": {
  504. "my_buckets": {
  505. "after_key": {
  506. "date": 1494288000000,
  507. "product": "mad max"
  508. },
  509. "buckets": [
  510. {
  511. "key": {
  512. "date": 1494201600000,
  513. "product": "rocky"
  514. },
  515. "doc_count": 1
  516. },
  517. {
  518. "key": {
  519. "date": 1494288000000,
  520. "product": "mad max"
  521. },
  522. "doc_count": 2
  523. }
  524. ]
  525. }
  526. }
  527. }
  528. --------------------------------------------------
  529. // TESTRESPONSE[s/\.\.\.//]
  530. To get the next set of buckets, resend the same aggregation with the `after`
  531. parameter set to the `after_key` value returned in the response.
  532. For example, this request uses the `after_key` value provided in the previous response:
  533. [source,console,id=composite-aggregation-after-example]
  534. --------------------------------------------------
  535. GET /_search
  536. {
  537. "size": 0,
  538. "aggs" : {
  539. "my_buckets": {
  540. "composite" : {
  541. "size": 2,
  542. "sources" : [
  543. { "date": { "date_histogram": { "field": "timestamp", "calendar_interval": "1d", "order": "desc" } } },
  544. { "product": { "terms": {"field": "product", "order": "asc" } } }
  545. ],
  546. "after": { "date": 1494288000000, "product": "mad max" } <1>
  547. }
  548. }
  549. }
  550. }
  551. --------------------------------------------------
  552. <1> Should restrict the aggregation to buckets that sort **after** the provided values.
  553. NOTE: The `after_key` is *usually* the key to the last bucket returned in
  554. the response, but that isn't guaranteed. Always use the returned `after_key` instead
  555. of derriving it from the buckets.
  556. ==== Early termination
  557. For optimal performance the <<index-modules-index-sorting,index sort>> should be set on the index so that it matches
  558. parts or fully the source order in the composite aggregation.
  559. For instance the following index sort:
  560. [source,console]
  561. --------------------------------------------------
  562. PUT twitter
  563. {
  564. "settings" : {
  565. "index" : {
  566. "sort.field" : ["username", "timestamp"], <1>
  567. "sort.order" : ["asc", "desc"] <2>
  568. }
  569. },
  570. "mappings": {
  571. "properties": {
  572. "username": {
  573. "type": "keyword",
  574. "doc_values": true
  575. },
  576. "timestamp": {
  577. "type": "date"
  578. }
  579. }
  580. }
  581. }
  582. --------------------------------------------------
  583. <1> This index is sorted by `username` first then by `timestamp`.
  584. <2> ... in ascending order for the `username` field and in descending order for the `timestamp` field.
  585. .. could be used to optimize these composite aggregations:
  586. [source,console]
  587. --------------------------------------------------
  588. GET /_search
  589. {
  590. "size": 0,
  591. "aggs" : {
  592. "my_buckets": {
  593. "composite" : {
  594. "sources" : [
  595. { "user_name": { "terms" : { "field": "user_name" } } } <1>
  596. ]
  597. }
  598. }
  599. }
  600. }
  601. --------------------------------------------------
  602. <1> `user_name` is a prefix of the index sort and the order matches (`asc`).
  603. [source,console]
  604. --------------------------------------------------
  605. GET /_search
  606. {
  607. "size": 0,
  608. "aggs" : {
  609. "my_buckets": {
  610. "composite" : {
  611. "sources" : [
  612. { "user_name": { "terms" : { "field": "user_name" } } }, <1>
  613. { "date": { "date_histogram": { "field": "timestamp", "calendar_interval": "1d", "order": "desc" } } } <2>
  614. ]
  615. }
  616. }
  617. }
  618. }
  619. --------------------------------------------------
  620. <1> `user_name` is a prefix of the index sort and the order matches (`asc`).
  621. <2> `timestamp` matches also the prefix and the order matches (`desc`).
  622. In order to optimize the early termination it is advised to set `track_total_hits` in the request
  623. to `false`. The number of total hits that match the request can be retrieved on the first request
  624. and it would be costly to compute this number on every page:
  625. [source,console]
  626. --------------------------------------------------
  627. GET /_search
  628. {
  629. "size": 0,
  630. "track_total_hits": false,
  631. "aggs" : {
  632. "my_buckets": {
  633. "composite" : {
  634. "sources" : [
  635. { "user_name": { "terms" : { "field": "user_name" } } },
  636. { "date": { "date_histogram": { "field": "timestamp", "calendar_interval": "1d", "order": "desc" } } }
  637. ]
  638. }
  639. }
  640. }
  641. }
  642. --------------------------------------------------
  643. Note that the order of the source is important, in the example below switching the `user_name` with the `timestamp`
  644. would deactivate the sort optimization since this configuration wouldn't match the index sort specification.
  645. If the order of sources do not matter for your use case you can follow these simple guidelines:
  646. * Put the fields with the highest cardinality first.
  647. * Make sure that the order of the field matches the order of the index sort.
  648. * Put multi-valued fields last since they cannot be used for early termination.
  649. WARNING: <<index-modules-index-sorting,index sort>> can slowdown indexing, it is very important to test index sorting
  650. with your specific use case and dataset to ensure that it matches your requirement. If it doesn't note that `composite`
  651. aggregations will also try to early terminate on non-sorted indices if the query matches all document (`match_all` query).
  652. ==== Sub-aggregations
  653. Like any `multi-bucket` aggregations the `composite` aggregation can hold sub-aggregations.
  654. These sub-aggregations can be used to compute other buckets or statistics on each composite bucket created by this
  655. parent aggregation.
  656. For instance the following example computes the average value of a field
  657. per composite bucket:
  658. [source,console,id=composite-aggregation-subaggregations-example]
  659. --------------------------------------------------
  660. GET /_search
  661. {
  662. "size": 0,
  663. "aggs" : {
  664. "my_buckets": {
  665. "composite" : {
  666. "sources" : [
  667. { "date": { "date_histogram": { "field": "timestamp", "calendar_interval": "1d", "order": "desc" } } },
  668. { "product": { "terms": {"field": "product" } } }
  669. ]
  670. },
  671. "aggregations": {
  672. "the_avg": {
  673. "avg": { "field": "price" }
  674. }
  675. }
  676. }
  677. }
  678. }
  679. --------------------------------------------------
  680. // TEST[s/_search/_search\?filter_path=aggregations/]
  681. \... returns:
  682. [source,console-result]
  683. --------------------------------------------------
  684. {
  685. ...
  686. "aggregations": {
  687. "my_buckets": {
  688. "after_key": {
  689. "date": 1494201600000,
  690. "product": "rocky"
  691. },
  692. "buckets": [
  693. {
  694. "key": {
  695. "date": 1494460800000,
  696. "product": "apocalypse now"
  697. },
  698. "doc_count": 1,
  699. "the_avg": {
  700. "value": 10.0
  701. }
  702. },
  703. {
  704. "key": {
  705. "date": 1494374400000,
  706. "product": "mad max"
  707. },
  708. "doc_count": 1,
  709. "the_avg": {
  710. "value": 27.0
  711. }
  712. },
  713. {
  714. "key": {
  715. "date": 1494288000000,
  716. "product" : "mad max"
  717. },
  718. "doc_count": 2,
  719. "the_avg": {
  720. "value": 22.5
  721. }
  722. },
  723. {
  724. "key": {
  725. "date": 1494201600000,
  726. "product": "rocky"
  727. },
  728. "doc_count": 1,
  729. "the_avg": {
  730. "value": 10.0
  731. }
  732. }
  733. ]
  734. }
  735. }
  736. }
  737. --------------------------------------------------
  738. // TESTRESPONSE[s/\.\.\.//]
  739. ==== Pipeline aggregations
  740. The composite agg is not currently compatible with pipeline aggregations, nor does it make sense in most cases.
  741. E.g. due to the paging nature of composite aggs, a single logical partition (one day for example) might be spread
  742. over multiple pages. Since pipeline aggregations are purely post-processing on the final list of buckets,
  743. running something like a derivative on a composite page could lead to inaccurate results as it is only taking into
  744. account a "partial" result on that page.
  745. Pipeline aggs that are self contained to a single bucket (such as `bucket_selector`) might be supported in the future.