当前位置: 首页 > news >正文

ES中的数据类型学习之Aggregate metric(聚合计算)

Aggregate metric field type | Elasticsearch Guide [7.17] | Elastic

对于object类型的字段来说,可以存子字段为 min/max/sum/value_count

PUT my-index
{"mappings": {"properties": {"my-agg-metric-field": { -- 字段名"type": "aggregate_metric_double", --字段类型"metrics": [ "min", "max", "sum", "value_count" ], --那些聚合操作"default_metric": "max"        -- 默认显示的聚合字段 用来query的}}}
}

Parameters for aggregate_metric_double fields 

metrics

(Required, array of strings) Array of metric sub-fields to store. Each value corresponds to a metric aggregation. Valid values are min, max, sum, and value_count. You must specify at least one value.

--只能是min/max/sum/value_count

default_metric

(Required, string) Default metric sub-field to use for queries, scripts, and aggregations that don’t use a sub-field. Must be a value from the metrics array.

-- 必须是metrics里的

time_series_metric

[preview] This functionality is in technical preview and may be changed or removed in a future release. Elastic will work to fix any issues, but features in technical preview are not subject to the support SLA of official GA features. (Optional, string)

For internal use by Elastic only.

--还没开放。

Uses

We designed aggregate_metric_double fields for use with the following aggregations:

  • A min aggregation returns the minimum value of all min sub-fields.
  • A max aggregation returns the maximum value of all max sub-fields.
  • A sum aggregation returns the sum of the values of all sum sub-fields.
  • A value_count aggregation returns the sum of the values of all value_count sub-fields.
  • A avg aggregation. There is no avg sub-field; the result of the avg aggregation is computed using the sum and value_count metrics. To run an avg aggregation, the field must contain both sum and value_count metric sub-field.

 -- 这里的sum和value_count都是 sum,但是value_count的用处是来计算avg

Running any other aggregation on an aggregate_metric_double field will fail with an "unsupported aggregation" error.

Finally, an aggregate_metric_double field supports the following queries for which it behaves as a double by delegating its behavior to its default_metric sub-field:

  • exists
  • range
  • term
  • terms

 -- 目前type=aggregate_metric_double 只持支持min/max/sum/value_count,其他类型会报错,目前也只支持下面4种查询

实战开始。

PUT stats-index
{
  "mappings": {
    "properties": {
      "agg_metric": {
        "type": "aggregate_metric_double",
        "metrics": [ "min", "max", "sum", "value_count" ],
        "default_metric": "max"
      }
    }
  }
}

PUT stats-index/_doc/1
{
  "agg_metric": {
    "min": -302.50,
    "max": 702.30,
    "sum": 200.0,
    "value_count": 25
  }
}

PUT stats-index/_doc/2
{
  "agg_metric": {
    "min": -93.00,
    "max": 1702.30,
    "sum": 300.00,
    "value_count": 25
  }
}

查询数据

POST stats-index/_search?size=0
{
  "aggs": {
    "metric_min": { "min": { "field": "agg_metric" } },
    "metric_max": { "max": { "field": "agg_metric" } },
    "metric_value_count": { "value_count": { "field": "agg_metric" } },
    "metric_sum": { "sum": { "field": "agg_metric" } },
    "metric_avg": { "avg": { "field": "agg_metric" } }
  }
}

-- 说下这个查询

-- size=0 意思是不看基础数据,只看聚合后的结果数据

-- aggs 是类似 term must 代表现在查的聚合数据

-- metric_min 代表聚合后的字段名,

--min 代表是哪个聚合方式min/max。。。

--"field": "agg_metric" 代表的是对哪个字段进行聚合可能有agg_metric1,agg_metric2

查询基础数据

GET stats-index/_search
{
  "query": {
    "term": {
      "agg_metric": {
        "value": 1702.30
      }
    }
  }
}

注意这里查询的是 max的值 也就是上文提到的default_metric

 

http://www.lryc.cn/news/404815.html

相关文章:

  • 看准JS逆向案例:webpack逆向解析
  • 【C语言】 利用栈完成十进制转二进制(分文件编译,堆区申请空间malloc)
  • 如何解决ChromeDriver 126找不到chromedriver.exe问题
  • Anaconda下安装配置Jupyter
  • 蓝队黑名单IP解封提取脚本
  • 共享充电桩语音ic方案,展现它的“说话”的能力
  • ARM 单片机裸机任务调度框架
  • .Net 8 控制台程序部署(Linux篇)
  • LeetCode:x的平方根(C语言)
  • 深入浅出WebRTC—DelayBasedBwe
  • JAVA开发工具IDEA如何连接操作数据库
  • 简化AI模型:PyTorch量化技术在边缘计算中的应用
  • 拥抱AI时代:解锁Prompt技术的无限潜力与深远影响
  • 第123天:内网安全-域防火墙入站出站规则不出网隧道上线组策略对象同步
  • 博客建站4 - ssh远程连接服务器
  • MySQL--索引(3)
  • sql_exporter通过sql收集业务数据并通过prometheus+grafana展示
  • pytorch 笔记:torch.optim.Adam
  • 开源AI智能名片小程序:深度剖析体验优化策略,激活小程序生命力的运营之道
  • ML.Net 学习之使用经过训练的模型进行预测
  • 为什么 centos 下使用 tree 命令看不见 .env 文件
  • 数据库基础与性能概述及相关术语
  • docker基于外部缓存加速构建方案
  • 【C语言】 作业11 链表+实现函数封装
  • 【Ubuntu】Ubuntu20修改MAC地址
  • ClickHouse集成LDAP实现简单的用户认证
  • C语言-预处理详解
  • 计算机网络-VLAN间通信(三层通信)模拟实现
  • 【JAVA】数据类型及变量
  • 微软蓝屏事件暴露的网络安全问题