nested-aggregation.asciidoc 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. [[search-aggregations-bucket-nested-aggregation]]
  2. === Nested Aggregation
  3. A special single bucket aggregation that enables aggregating nested documents.
  4. For example, lets say we have an index of products, and each product holds the list of resellers - each having its own
  5. price for the product. The mapping could look like:
  6. [source,js]
  7. --------------------------------------------------
  8. PUT /index
  9. {
  10. "mappings": {
  11. "product" : {
  12. "properties" : {
  13. "resellers" : { <1>
  14. "type" : "nested",
  15. "properties" : {
  16. "name" : { "type" : "text" },
  17. "price" : { "type" : "double" }
  18. }
  19. }
  20. }
  21. }
  22. }
  23. }
  24. --------------------------------------------------
  25. // CONSOLE
  26. // TESTSETUP
  27. <1> The `resellers` is an array that holds nested documents under the `product` object.
  28. The following aggregations will return the minimum price products can be purchased in:
  29. [source,js]
  30. --------------------------------------------------
  31. GET /_search
  32. {
  33. "query" : {
  34. "match" : { "name" : "led tv" }
  35. },
  36. "aggs" : {
  37. "resellers" : {
  38. "nested" : {
  39. "path" : "resellers"
  40. },
  41. "aggs" : {
  42. "min_price" : { "min" : { "field" : "resellers.price" } }
  43. }
  44. }
  45. }
  46. }
  47. --------------------------------------------------
  48. // CONSOLE
  49. // TEST[s/GET \/_search/GET \/_search\?filter_path=aggregations/]
  50. // TEST[s/^/PUT index\/product\/0\?refresh\n{"name":"led", "resellers": [{"name": "foo", "price": 350.00}, {"name": "bar", "price": 500.00}]}\n/]
  51. As you can see above, the nested aggregation requires the `path` of the nested documents within the top level documents.
  52. Then one can define any type of aggregation over these nested documents.
  53. Response:
  54. [source,js]
  55. --------------------------------------------------
  56. {
  57. ...
  58. "aggregations": {
  59. "resellers": {
  60. "doc_count": 0,
  61. "min_price": {
  62. "value": 350
  63. }
  64. }
  65. }
  66. }
  67. --------------------------------------------------
  68. // TESTRESPONSE[s/\.\.\.//]
  69. // TESTRESPONSE[s/: [0-9]+/: $body.$_path/]