nested-aggregation.asciidoc 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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. "properties" : {
  12. "resellers" : { <1>
  13. "type" : "nested",
  14. "properties" : {
  15. "name" : { "type" : "text" },
  16. "price" : { "type" : "double" }
  17. }
  18. }
  19. }
  20. }
  21. }
  22. --------------------------------------------------
  23. // CONSOLE
  24. // TESTSETUP
  25. <1> The `resellers` is an array that holds nested documents under the `product` object.
  26. The following aggregations will return the minimum price products can be purchased in:
  27. [source,js]
  28. --------------------------------------------------
  29. GET /_search
  30. {
  31. "query" : {
  32. "match" : { "name" : "led tv" }
  33. },
  34. "aggs" : {
  35. "resellers" : {
  36. "nested" : {
  37. "path" : "resellers"
  38. },
  39. "aggs" : {
  40. "min_price" : { "min" : { "field" : "resellers.price" } }
  41. }
  42. }
  43. }
  44. }
  45. --------------------------------------------------
  46. // CONSOLE
  47. // TEST[s/GET \/_search/GET \/_search\?filter_path=aggregations/]
  48. // TEST[s/^/PUT index\/_doc\/0\?refresh\n{"name":"led", "resellers": [{"name": "foo", "price": 350.00}, {"name": "bar", "price": 500.00}]}\n/]
  49. As you can see above, the nested aggregation requires the `path` of the nested documents within the top level documents.
  50. Then one can define any type of aggregation over these nested documents.
  51. Response:
  52. [source,js]
  53. --------------------------------------------------
  54. {
  55. ...
  56. "aggregations": {
  57. "resellers": {
  58. "doc_count": 0,
  59. "min_price": {
  60. "value": 350
  61. }
  62. }
  63. }
  64. }
  65. --------------------------------------------------
  66. // TESTRESPONSE[s/\.\.\.//]
  67. // TESTRESPONSE[s/: [0-9]+/: $body.$_path/]