【Elasticsearch】指标聚合概述
以下是elasticsearch指标聚合类型的描述、示例查询以及相应的响应示例:
### 1. Avg
**功能**:计算指定字段的平均值。
**示例**:
```json
GET /sales/_search
{
"size": 0,
"aggs": {
"average_price": {
"avg": {
"field": "price"
}
}
}
}
```
**响应**:
```json
{
"aggregations": {
"average_price": {
"value": 150.5
}
}
}
```
### 2. Boxplot
**功能**:生成指定字段的箱线图统计数据。
**示例**:
```json
GET /sales/_search
{
"size": 0,
"aggs": {
"boxplot_price": {
"boxplot": {
"field": "price"
}
}
}
}
```
**响应**:
```json
{
"aggregations": {
"boxplot_price": {
"min": 100.0,
"q1": 120.0,
"median": 150.0,
"q3": 180.0,
"max": 200.0
}
}
}
```
### 3. Cardinality
**功能**:计算指定字段的唯一值数量。
**示例**:
```json
GET /users/_search
{
"size": 0,
"aggs": {
"unique_users": {
"cardinality": {
"field": "user_id"
}
}
}
}
```
**响应**:
```json
{
"aggregations": {
"unique_users": {
"value": 100
}
}
}
```
### 4. Extended Stats
**功能**:提供指定字段的扩展统计信息。
**示例**:
```json
GET /sales/_search
{
"size": 0,
"aggs": {
"extended_stats_price": {
"extended_stats": {
"field": "price"
}
}
}
}
```
**响应**:
```json
{
"aggregations": {
"extended_stats_price": {
"count": 100,
"min": 100.0,
"max": 200.0,
"avg": 150.0,
"sum": 15000.0,
"sum_of_squares": 2250000.0,
"variance": 1250.0,
"std_deviation": 35.35533905932738,
"std_deviation_bounds": {
"upper": 220.71067811865476,
"lower": 79.28932188134524
}
}
}
}
```
### 5. Geo-bounds
**功能**:计算指定地理字段的边界框。
**示例**:
```json
GET /locations/_search
{
"size": 0,
"aggs": {
"geo_bounds": {
"geo_bounds": {
"field": "location"
}
}
}
}
```
**响应**:
```json
{
"aggregations": {
"geo_bounds": {
"bounds": {
"top_left": {
"lat": 40.712776,
"lon": -74.005974
},
"bottom_right": {
"lat": 40.712776,
"lon": -74.005974
}
}
}
}
}
```
### 6. Geo-centroid
**功能**:计算指定地理字段的中心点。
**示例**:
```json
GET /locations/_search
{
"size": 0,
"aggs": {
"geo_centroid": {
"geo_centroid": {
"field": "location"
}
}
}
}
```
**响应**:
```json
{
"aggregations": {
"geo_centroid": {
"location": {
"lat": 40.712776,
"lon": -74.005974
}
}
}
}
```
### 7. Geo-line
**功能**:生成指定地理字段的线段路径。
**示例**:
```json
GET /tracks/_search
{
"size": 0,
"aggs": {
"geo_line": {
"geo_line": {
"field": "path"
}
}
}
}
```
**响应**:
```json
{
"aggregations": {
"geo_line": {
"path": [
{
"lat": 40.712776,
"lon": -74.005974
},
{
"lat": 40.712777,
"lon": -74.005975
}
]
}
}
}
```
### 8. Cartesian-bounds
**功能**:计算指定笛卡尔坐标字段的边界框。
**示例**:
```json
GET /cartesian_points/_search
{
"size": 0,
"aggs": {
"cartesian_bounds": {
"cartesian_bounds": {
"field": "point"
}
}
}
}
```
**响应**:
```json
{
"aggregations": {
"cartesian_bounds": {
"bounds": {
"top_left": {
"x": 10.0,
"y": 20.0
},
"bottom_right": {
"x": 30.0,
"y": 40.0
}
}
}
}
}
```
### 9. Cartesian-centroid
**功能**:计算指定笛卡尔坐标字段的中心点。
**示例**:
```json
GET /cartesian_points/_search
{
"size": 0,
"aggs": {
"cartesian_centroid": {
"cartesian_centroid": {
"field": "point"
}
}
}
}
```
**响应**:
```json
{
"aggregations": {
"cartesian_centroid": {
"point": {
"x": 20.0,
"y": 30.0
}
}
}
}
```
### 10. Matrix Stats
**功能**:计算指定字段的矩阵统计信息。
**示例**:
```json
GET /data_points/_search
{
"size": 0,
"aggs": {
"matrix_stats": {
"matrix_stats": {
"fields": ["x", "y"]
}
}
}
}
```
**响应**:
```json
{
"aggregations": {
"matrix_stats": {
"count": 100,
"mean": {
"x": 10.0,
"y": 20.0
},
"covariance": {
"x": {
"x": 100.0,
"y": 50.0
},
"y": {
"x": 50.0,
"y": 200.0
}
},
"correlation": {
"x": {
"x": 1.0,
"y": 0.5
},
"y": {
"x": 0.5,
"y": 1.0
}
}
}
}
}
```
### 11. Max
**功能**:计算指定字段的最大值。
**示例**:
```json
GET /sales/_search
{
"size": 0,
"aggs": {
"max_price": {
"max": {
"field": "price"
}
}
}
}
```
**响应**:
```json
{
"aggregations": {
"max_price": {
"value": 200.0
}
}
}
```
### 12. Median Absolute Deviation
**功能**:计算指定字段的中位数绝对偏差。
**示例**:
```json
GET /sales/_search
{
"size": 0,
"aggs": {
"mad_price": {
"median_absolute_deviation": {
"field": "price"
}
}
}
}
```
**响应**:
```json
{
"aggregations": {
"mad_price": {
"value": 10.0
}