Metrics Aggregations [指标聚合]

avg aggregation

cardinality aggregation

max aggregation

min aggregation

sum aggregation

value count aggregation

extended stats aggregation

Bucket Aggregations [桶聚合]

Missing Aggregation

统计某个不存在字段notExistsField的数量

  1. GET index/type/_search
  2. {
  3. "query": {
  4. "match_all": {}
  5. },
  6. "aggs": {
  7. "missing_example" : {
  8. "missing": {
  9. "field": "notExistsField"
  10. }
  11. }
  12. }
  13. }

Range Aggregation

范围聚合,统计每个范围内的文档数量

  1. GET index/type/_search
  2. {
  3. "query": {
  4. "match_all": {}
  5. },
  6. "aggs": {
  7. "range_example": {
  8. "range": {
  9. "field": "amount",
  10. "keyed": true,
  11. "ranges": [
  12. {
  13. "to": 10
  14. },
  15. {
  16. "from": 11,
  17. "to": 20
  18. },
  19. {
  20. "from": 21,
  21. "to": 30
  22. }
  23. ]
  24. }
  25. }
  26. }
  27. }

Terms Aggregation

类似于MySQL的 group by,按sex统计文档数量

  1. GET index/type/_search
  2. {
  3. "query": {
  4. "match_all": {}
  5. },
  6. "aggs": {
  7. "terms_example": {
  8. "terms": {
  9. "field": "sex",
  10. "size": 10
  11. }
  12. }
  13. }
  14. }

Significant Terms Aggregation

与Terms的区别: 返回结果中多了doc_count、bg_count

  1. GET index/type/_search
  2. {
  3. "size": 0,
  4. "query": {
  5. "match_all": {}
  6. },
  7. "aggs": {
  8. "terms_example": {
  9. "significant_terms": {
  10. "field": "state"
  11. }
  12. }
  13. }
  14. }
  15. // 返回结果
  16. {
  17. "took": 14,
  18. "timed_out": false,
  19. "_shards": {
  20. "total": 5,
  21. "successful": 5,
  22. "failed": 0
  23. },
  24. "hits": {
  25. "total": 264729,
  26. "max_score": 0,
  27. "hits": []
  28. },
  29. "aggregations": {
  30. "terms_example": {
  31. "doc_count": 264729, //文档数量
  32. "bg_count": 266199, //未知
  33. "buckets": [
  34. {
  35. "key": "1",
  36. "doc_count": 103122, //key1的文档数量
  37. "score": 0.0021440541483472653,
  38. "bg_count": 103127 //未知
  39. }
  40. ]
  41. }
  42. }
  43. }