11-Elasticsearch Metrics聚合

Elasticsearch Metrics聚合

Metrics聚合概述

Metrics聚合是基于从正在聚合的文档中以一种或另一种方式提取的值来计算度量。

DELETE test_metrics
POST test_metrics/_bulk
{"index":{}}
{"stuno":"1001","grade":80,"weight":0.5}
{"index":{}}
{"stuno":"1002","grade":88,"weight":0.2}
{"index":{}}
{"stuno":"1003","grade":90,"weight":0.4}
{"index":{}}
{"stuno":"1003","grade":95,"weight":0.4}
加权平均值

查询:

GET test_metrics/_search
{
  "size": 0,
  "aggs": {
    "weight_grade": {
      "weighted_avg": {
        "value": {
          "field": "grade"
        },
        "weight": {
          "field": "weight"
        }
      }
    }
  }
}

结果:

{
  "took" : 48,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 3,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "weight_grade" : {
      "value" : 85.09090912539112
    }
  }
}
去重求数据总量

查询:

GET test_metrics/_search
{
  "size": 0,
  "aggs": {
    "distinct_stuno": {
      "cardinality": {
        "field": "stuno.keyword"
      }
    }
  }
}

结果:

{
  "took" : 54,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 4,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "distinct_stuno" : {
      "value" : 3
    }
  }
}
绝对中位差

查询:

GET test_metrics/_search
{
  "size": 0,
  "aggs": {
    "grade_avg": {
      "avg": {
        "field": "grade"
      }
    },
    "grade_variability":{
      "median_absolute_deviation":{
        "field": "grade"
      }
    }
  }
}

结果:

{
  "took" : 156,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 4,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "grade_avg" : {
      "value" : 88.25
    },
    "grade_variability" : {
      "value" : 3.5
    }
  }
}
聚合统计最大值,最小值,平均值等信息

查询:

GET test_metrics/_search
{
  "size": 0,
  "aggs": {
    "grade_stats": {
      "stats": {
        "field": "grade"
      }
    }
  }
}

结果:

{
  "took" : 21,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 4,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "grade_stats" : {
      "count" : 4,
      "min" : 80.0,
      "max" : 95.0,
      "avg" : 88.25,
      "sum" : 353.0
    }
  }
}
字符串统计

查询:

GET string_stats/_search
{
  "size": 0,
  "aggs": {
    "content_stats": {
      "string_stats": {
        "field": "content.keyword"
      }
    }
  }
}

结果:

{
  "took" : 64,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 4,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "content_stats" : {
      "count" : 4,
      "min_length" : 10,
      "max_length" : 28,
      "avg_length" : 17.25,
      "entropy" : 5.304034963961837
    }
  }
}
百分位数统计

查询:

GET test_metrics/_search
{
  "size": 0,
  "aggs": {
    "grade_percent": {
      "percentiles": {
        "field": "grade"
      }
    }
  }
}

结果:

{
  "took" : 87,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 4,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "grade_percent" : {
      "values" : {
        "1.0" : 80.00000000000001,
        "5.0" : 80.0,
        "25.0" : 84.0,
        "50.0" : 89.0,
        "75.0" : 92.5,
        "95.0" : 95.0,
        "99.0" : 95.0
      }
    }
  }
}
百分位等级计算

查询:


GET test_metrics/_search
{
  "size": 0,
  "aggs": {
    "grade_rank": {
      "percentile_ranks": {
        "field": "grade",
        "values": [
          80,
          88,
          95,
          90
        ]
      }
    }
  }
}

结果:

{
  "took" : 12,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 4,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "grade_rank" : {
      "values" : {
        "80.0" : 12.5,
        "88.0" : 45.0,
        "90.0" : 57.14285714285714,
        "95.0" : 100.0
      }
    }
  }
}
聚合后返回前N条数据

查询:

GET test_metrics/_search
{
  "size": 0,
  "aggs": {
    "stuno": {
      "terms": {
        "field": "stuno.keyword"
      },
      "aggs": {
        "top_2": {
          "top_hits": {
            "sort": [
              {
                "grade": {
                  "order": "asc"
                }
              }
            ],
            "size": 1
          }
        }
      }
    }
  }
}

结果:

GET test_metrics/_search
{
  "size": 0,
  "aggs": {
    "stuno": {
      "terms": {
        "field": "stuno.keyword"
      },
      "aggs": {
        "top_2": {
          "top_hits": {
            "sort": [
              {
                "grade": {
                  "order": "asc"
                }
              }
            ],
            "size": 1
          }
        }
      }
    }
  }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值