ElasticSearch 聚合查询(2)桶聚合学习

1. Terms Aggregation 根据字段值项分组聚合
GET /megacorp/employee/_search?size=0 HTTP/1.1
Host: 192.168.31.233:9200
Content-Type: application/json

{
    "aggs": {
        "age_terms": {
            "terms": {
                "field": "age"
            }
        }
    }
}

Results:

{
    "took": 42,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 7,
        "max_score": 0.0,
        "hits": []
    },
    "aggregations": {
        "age_terms": {
            "doc_count_error_upper_bound": 0,
            "sum_other_doc_count": 0,
            "buckets": [
                {
                    "key": 35,
                    "doc_count": 3
                },
                {
                    "key": 25,
                    "doc_count": 2
                },
                {
                    "key": 32,
                    "doc_count": 2
                }
            ]
        }
    }
}

结果说明:

“doc_count_error_upper_bound”: 0:文档计数的最大偏差值

“sum_other_doc_count”: 463:未返回的其他项的文档数

默认情况下返回按文档计数从高到低的前10个分组:

2. filter Aggregation 对满足过滤查询的文档进行聚合计算
POST /megacorp/employee/_search?size=0
{
  "aggs": {
    "age_terms": {
      "filter": {"match":{"gender":"F"}},
      "aggs": {
        "avg_age": {
          "avg": {
            "field": "age"
          }
        }
      }
    }
  }
}
3. Range Aggregation 范围分组聚合
GET /megacorp/employee/_search?size=0 HTTP/1.1
Host: 192.168.31.233:9200
Content-Type: application/json

{
    "aggs": {
        "age_range": {
            "range": {
                "field": "age",
                "ranges": [
                    {
                        "to": 25
                    },
                    {
                        "from": 25,
                        "to": 35
                    },
                    {
                        "from": 35
                    }
                ]
            }
        }
    }
}

Results:

{
    "took": 7,
    "timed_out": false,
    "_shards": {
        "total": 5,
        "successful": 5,
        "skipped": 0,
        "failed": 0
    },
    "hits": {
        "total": 7,
        "max_score": 0.0,
        "hits": []
    },
    "aggregations": {
        "age_range": {
            "buckets": [
                {
                    "key": "*-25.0",
                    "to": 25.0,
                    "doc_count": 0
                },
                {
                    "key": "25.0-35.0",
                    "from": 25.0,
                    "to": 35.0,
                    "doc_count": 4
                },
                {
                    "key": "35.0-*",
                    "from": 35.0,
                    "doc_count": 3
                }
            ]
        }
    }
}
5. Date Range Aggregation 时间范围分组聚合
6. Date Histogram Aggregation 时间直方图(柱状)聚合
7. Missing Aggregation 缺失值的桶聚合
8. Geo Distance Aggregation 地理距离分区聚合

参考官网链接:
https://www.elastic.co/guide/en/elasticsearch/reference/current/search-aggregations-bucket-geodistance-aggregation.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值