nested-aggregation.asciidoc 2.1 KB

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