es - elasticsearch - aggs - pipeline - serial

es - elasticsearch - aggs - pipeline - serial,第1张

es - elasticsearch - aggs - pipeline - serial

世界上并没有完美的程序,但是我们并不因此而沮丧,因为写程序就是一个不断追求完美的过程。

文章目录
  • serial_diff
    • 场景
    • 作用
    • 配置项
    • 使用
      • 索引
      • 查询

serial_diff 场景
  • 嵌入到histogram和date_histogram内部
作用
  • 计算相隔n个bucket之间的差异值
配置项
  • buckets_path

    • bucket路径
  • lag

    • 指定计算相隔n个的bucket的差异
  • gap_policy

    • 出现间隙的策略,默认insert_zero
  • format

    • 返回值的格式
使用 索引
DELETE /serial_diff_test

PUT /serial_diff_test
{
  "mappings" : {
    "properties" : {
      "type" : {"type" : "integer"},
      "num"  : {"type" : "integer"},
      "date" : {"type" : "date"}
    }
  }
}

POST /serial_diff_test/_bulk
{"index" : {"_id" : 1}}
{"type" : 1, "num" : 40, "date" : "2001-01-10"}
{"index" : {"_id" : 2}}
{"type" : 2, "num" : 45, "date" : "2001-01-20"}
{"index" : {"_id" : 3}}
{"type" : 1, "num" : 58, "date" : "2001-02-10"}
{"index" : {"_id" : 4}}
{"type" : 2, "num" : 99, "date" : "2001-03-20"}
{"index" : {"_id" : 5}}
{"type" : 1, "num" : 66, "date" : "2001-04-21"}
{"index" : {"_id" : 6}}
{"type" : 1, "num" : 68, "date" : "2001-05-21"}
查询
GET /serial_diff_test/_search
{
  "size" : 0,
  "aggs" : {
    "month_aggs" : {
      "date_histogram": {
        "field" : "date",
        "calendar_interval" : "month"
      },
      "aggs" : {
        "sum_aggs" : {
          "sum": {
            "field": "num"
          }
        },
        "serial_diff_aggs" : {
          "serial_diff": {
            "buckets_path" : "sum_aggs",
            "lag" : 2, 
            "format" : "00.00"
          }
        }
      }
    }
  }
}
  • 返回结果
{
  "took" : 8,
  "timed_out" : false,
  "_shards" : {
    "total" : 1,
    "successful" : 1,
    "skipped" : 0,
    "failed" : 0
  },
  "hits" : {
    "total" : {
      "value" : 6,
      "relation" : "eq"
    },
    "max_score" : null,
    "hits" : [ ]
  },
  "aggregations" : {
    "month_aggs" : {
      "buckets" : [
        {
          "key_as_string" : "2001-01-01T00:00:00.000Z",
          "key" : 978307200000,
          "doc_count" : 2,
          "sum_aggs" : {
            "value" : 85.0
          }
        },
        {
          "key_as_string" : "2001-02-01T00:00:00.000Z",
          "key" : 980985600000,
          "doc_count" : 1,
          "sum_aggs" : {
            "value" : 58.0
          }
        },
        {
          "key_as_string" : "2001-03-01T00:00:00.000Z",
          "key" : 983404800000,
          "doc_count" : 1,
          "sum_aggs" : {
            "value" : 99.0
          },
          "serial_diff_aggs" : {
            "value" : 14.0,
            "value_as_string" : "14.00"
          }
        },
        {
          "key_as_string" : "2001-04-01T00:00:00.000Z",
          "key" : 986083200000,
          "doc_count" : 1,
          "sum_aggs" : {
            "value" : 66.0
          },
          "serial_diff_aggs" : {
            "value" : 8.0,
            "value_as_string" : "08.00"
          }
        },
        {
          "key_as_string" : "2001-05-01T00:00:00.000Z",
          "key" : 988675200000,
          "doc_count" : 1,
          "sum_aggs" : {
            "value" : 68.0
          },
          "serial_diff_aggs" : {
            "value" : -31.0,
            "value_as_string" : "-31.00"
          }
        }
      ]
    }
  }
}

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5682192.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-17
下一篇 2022-12-17

发表评论

登录后才能评论

评论列表(0条)

保存