Aggregation 聚合

POST /api/:target/_search

搜索文档的同时进行聚合操作

聚合支持:

Bucketing(分桶)

term - 支持类型为text、keyword、numeric的字段

range - 支持类型为numeric的字段

date_range - 支持类型为time的字段

Metrics(指标)

min、max、count、sum - 支持类型为numeric的字段

avg、weighted_vg - 支持类型为numeric的字段

请求示例

POST http://localhost:4080/api/olympics/_search

请求体:

  1. {
  2. "search_type": "match",
  3. "query": {
  4. "term": "Ice Hockey"
  5. },
  6. "sort_fields": ["-@timestamp"],
  7. "from": 0,
  8. "max_results": 20,
  9. "aggs": {
  10. "Medal": {
  11. "agg_type": "term",
  12. "field": "Medal",
  13. "size": 10
  14. },
  15. "Year": {
  16. "agg_type": "range",
  17. "field": "Year",
  18. "size": 10,
  19. "ranges": [
  20. {"from": 1900, "to": 1920},
  21. {"from": 1921, "to": 1950},
  22. {"from": 1951, "to": 2000},
  23. {"from": 2000, "to": 2021}
  24. ]
  25. },
  26. "@timestamp": {
  27. "agg_type": "date_range",
  28. "field": "@timestamp",
  29. "size": 10,
  30. "date_ranges": [
  31. {
  32. "from": "2020-01-21T09:22:50.604Z",
  33. "to": "2021-01-21T09:22:50.604Z"
  34. },
  35. {
  36. "from": "2021-01-22T09:22:50.604Z",
  37. "to": "2023-01-21T09:22:50.604Z"
  38. }
  39. ]
  40. },
  41. "max_Year": {
  42. "agg_type": "max",
  43. "field": "Year"
  44. },
  45. "min_Year": {
  46. "agg_type": "min",
  47. "field": "Year"
  48. },
  49. "avg_Year": {
  50. "agg_type": "avg",
  51. "field": "Year"
  52. },
  53. "weighted_avg_Year": {
  54. "agg_type": "weighted_avg",
  55. "field": "Year",
  56. "weight_field": "Year"
  57. },
  58. "sum_Year": {
  59. "agg_type": "sum",
  60. "field": "Year"
  61. },
  62. "count_Sport": {
  63. "agg_type": "count",
  64. "field": "Sport"
  65. }
  66. }
  67. }

响应

  1. {
  2. "took": 250,
  3. "timed_out": false,
  4. "hits": {
  5. "total": {
  6. "value": 3051
  7. },
  8. "max_score": 17.82873837347811,
  9. "hits": null
  10. },
  11. "aggregations": {
  12. "@timestamp": {
  13. "buckets": [
  14. {
  15. "key": "[2020-01-21T09:22:50Z,2021-01-21T09:22:50Z)",
  16. "doc_count": 0
  17. },
  18. {
  19. "key": "[2021-01-22T09:22:50Z,2023-01-21T09:22:50Z)",
  20. "doc_count": 6102
  21. }
  22. ]
  23. },
  24. "Medal": {
  25. "buckets": [
  26. {
  27. "key": "gold",
  28. "doc_count": 1026
  29. },
  30. {
  31. "key": "silver",
  32. "doc_count": 1024
  33. },
  34. {
  35. "key": "bronze",
  36. "doc_count": 1001
  37. }
  38. ]
  39. },
  40. "Year": {
  41. "buckets": [
  42. {
  43. "key": "[1900.000000,1920.000000)",
  44. "doc_count": 132
  45. },
  46. {
  47. "key": "[1921.000000,1950.000000)",
  48. "doc_count": 2292
  49. },
  50. {
  51. "key": "[1951.000000,2000.000000)",
  52. "doc_count": 9822
  53. },
  54. {
  55. "key": "[2000.000000,2021.000000)",
  56. "doc_count": 5646
  57. }
  58. ]
  59. },
  60. "avg_Year": {
  61. "value": 1980.4818092428711
  62. },
  63. "count_Sport": {
  64. "value": 3051
  65. },
  66. "max_Year": {
  67. "value": 2014
  68. },
  69. "min_Year": {
  70. "value": 1908
  71. },
  72. "sum_Year": {
  73. "value": 36254700
  74. },
  75. "weighted_avg_Year": {
  76. "value": 1980.4818092428711
  77. }
  78. },
  79. "error": ""
  80. }