Open Source, Open Future!
  menu
107 文章
ღゝ◡╹)ノ❤️

elasticsearch---聚合

数据准备

测试数据

Sum Aggregation

用于计算总和。
计算所有商品的价格总和

GET /product/sku/_search
{
 "size": 0,
 "aggs": {
   "sum_price": {
     "sum": {"field": "price"}
   }
 }
}

不需要返回文档集可以加 "size": 0
返回结果:

  "aggregations": {
    "total_price": {
      "value": 1109.97
    }
  }

Min Aggregation

用于统计最小值。
查询价格最低的商品:

GET /product/sku/_search
{
  "size": 0,
  "aggs": {
    "min_price": {
      "min": {"field": "price"}
    }
  }
}

Max Aggregation

用于统计最大值。
查询价格最高的商品:

GET /product/sku/_search
{
  "size": 0,
  "aggs": {
    "max_price": {
      "max": {"field": "price"}
    }
  }
}

Avg Aggregation

用于计算平均值。
查询平均价格:

GET /product/sku/_search
{
  "size": 0,
  "aggs": {
    "avg_price": {
      "avg": {"field": "price"}
    }
  }
}

Cardinality Aggregation

用于基数统计。比如性别只有男和女,那么基数就是2。
统计品牌数量:

GET /product/sku/_search
{
  "size": 0,
  "aggs": {
    "all_lan": {
      "cardinality": {"field": "brand"}
    }
  }
}

返回结果:

  "aggregations": {
    "all_lan": {
      "value": 2
    }
  }

##Stats Aggregation
用于基本统计。会一次返回countmaxminavgsum这5个指标。

GET /product/sku/_search
{
  "size": 0,
  "aggs": {
    "grades_stats": {
      "stats": {"field": "price"}
    }
  }
}

返回结果:

  "aggregations": {
    "grades_stats": {
      "count": 3,
      "min": 9.99,
      "max": 999.99,
      "avg": 369.99,
      "sum": 1109.97
    }
  }

Stats Aggregation

用于高级统计。比基本统计的统计结果多了4个:平方和、方差、平均值加/减两个标准差的区间。

GET /product/sku/_search
{
  "size": 0,
  "aggs": {
    "grades_stats": {
      "extended_stats": {"field": "price"}
    }
  }
}

返回结果:

  "aggregations": {
    "grades_stats": {
      "count": 3,
      "min": 9.99,
      "max": 999.99,
      "avg": 369.99,
      "sum": 1109.97,
      "sum_of_squares": 1010077.8003,
      "variance": 199800,
      "std_deviation": 446.9899327725402,
      "std_deviation_bounds": {
        "upper": 1263.9698655450804,
        "lower": -523.9898655450804
      }
    }
  }

Percentiles Aggregation

用于百分位统计。

Value Count Aggregation

按字段统计文档数量。
统计有price字段的文档数量:

GET /product/sku/_search
{
  "size": 0,
  "aggs": {
    "doc_count": {
      "value_count": {"field": "price"}
    }
  }
}

Terms Aggregation

用于分组聚合。
1、按品牌对商品进行分组,并统计各个品牌的商品数量:

GET /product/sku/_search
{
  "size": 0,
  "aggs": {
    "per_count": {
      "terms": {"field": "brand"}
    }
  }
}

返回结果:

  "aggregations": {
    "per_count": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "huawei",
          "doc_count": 2
        },
        {
          "key": "sanxing",
          "doc_count": 1
        }
      ]
    }
  }

2、按品牌对商品进行分组,并统计各个品牌的商品平均价格:

GET /product/sku/_search
{
  "size": 0,
  "aggs": {
    "per_count": {
      "terms": {"field": "brand"},
      "aggs": {
        "avg_price": {
          "avg": {"field": "price"}
        }
      }
    }
  }
}

返回结果:

  "aggregations": {
    "per_count": {
      "doc_count_error_upper_bound": 0,
      "sum_other_doc_count": 0,
      "buckets": [
        {
          "key": "huawei",
          "doc_count": 2,
          "avg_price": {
            "value": 549.99
          }
        },
        {
          "key": "sanxing",
          "doc_count": 1,
          "avg_price": {
            "value": 9.99
          }
        }
      ]
    }
  }

Filter Aggregation

用于过滤器聚合。

1、统计商品描述中有abc的商品数量:

GET /product/sku/_search
{
  "size": 0,
  "aggs": {
    "sku_count": {
      "filter": {"term": {"desc": "abc"}}
    }
  }
}

返回结果:

  "aggregations": {
    "sku_count": {
      "doc_count": 2
    }
  }

Filters Aggregation

用于多过滤器聚合。

Range Aggregation

用于范围聚合。
统计商品价格分别在 0 ~ 10、10 ~ 100、100 +三个区间的商品数量:

GET /product/sku/_search
{
  "size": 0,
  "aggs": {
    "price_ranges": {
      "range": {
        "field": "price",
        "ranges": [
          { "to": 10 },
          { "from": 10,"to": 100 },
          { "from": 100 }
        ]
      }
    }
  }
}

返回结果:

  "aggregations": {
    "price_ranges": {
      "buckets": [
        {
          "key": "*-10.0",
          "to": 10,
          "doc_count": 1
        },
        {
          "key": "10.0-100.0",
          "from": 10,
          "to": 100,
          "doc_count": 1
        },
        {
          "key": "100.0-*",
          "from": 100,
          "doc_count": 1
        }
      ]
    }
  }

Date Range Aggregation

专门用于日期类型的范围聚合。

Date Histogram Aggregation

时间直方图聚合,常用于按照日期对文档进行统计并绘制条形图。

Missing Aggregation

空值聚合。

Children Aggregation

一种特殊的单桶聚合,可以根据父子文档关系进行分桶。

Geo Distance Aggregation

对地理点做范围统计。比如:以当前位置,分别统计距离范围在100m内,100m ~500m之间、500m ~1000m之间的电影院。

IP Range Aggregation

用于对IP类型数据范围聚合。