nested-aggregation.asciidoc 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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 a 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. {
  9. ...
  10. "product" : {
  11. "properties" : {
  12. "resellers" : { <1>
  13. "type" : "nested",
  14. "properties" : {
  15. "name" : { "type" : "string" },
  16. "price" : { "type" : "double" }
  17. }
  18. }
  19. }
  20. }
  21. }
  22. --------------------------------------------------
  23. <1> The `resellers` is an array that holds nested documents under the `product` object.
  24. The following aggregations will return the minimum price products can be purchased in:
  25. [source,js]
  26. --------------------------------------------------
  27. {
  28. "query" : {
  29. "match" : { "name" : "led tv" }
  30. },
  31. "aggs" : {
  32. "resellers" : {
  33. "nested" : {
  34. "path" : "resellers"
  35. },
  36. "aggs" : {
  37. "min_price" : { "min" : { "field" : "resellers.price" } }
  38. }
  39. }
  40. }
  41. }
  42. --------------------------------------------------
  43. As you can see above, the nested aggregation requires the `path` of the nested documents within the top level documents.
  44. Then one can define any type of aggregation over these nested documents.
  45. Response:
  46. [source,js]
  47. --------------------------------------------------
  48. {
  49. "aggregations": {
  50. "resellers": {
  51. "min_price": {
  52. "value" : 350
  53. }
  54. }
  55. }
  56. }
  57. --------------------------------------------------