繁体   English   中英

ElasticSearch数据的分组、计算和排序

[英]Group, calculation and order of ElasticSearch data

我有很多以以下格式存储的数据(我简化了数据以解释问题)。

在此处输入图像描述

我需要的是:

  • 按“操作 ID”字段对所有数据进行分组
  • 计算每个组的“创建时间”的最大值和最小值之间的差异(来自上一个操作)
  • 按计算字段排序结果(“操作持续时间” - 最大值和最小值之间的差异)

我使用 NEST (C#) 来查询 ElasticSearch。 我认为,如果您可以帮助我使用本机 Elastic 查询,它也会非常有帮助,我会将其翻译为 C#。

谢谢你。

如果您的映射如下所示:

PUT /index
{
  "mappings": {
    "doc": {
      "properties": {
        "ActionId": {
          "type": "text",
          "fielddata": true
        },
        "CreatedDate":{
          "type": "date"
        },
        "SubActionName":{
          "type": "text",
          "fielddata": true
        }
      }
    }
  }
}

您的 elasticsearch 查询应如下所示:

GET index/_search
{
  "size": 0,
  "aggs": {

    "actions": {
      "terms": {
        "field": "ActionId"
      },
      "aggs": {
        "date_created": {
          "date_histogram": {
            "field": "CreatedDate",
            "interval": "hour"
          },
          "aggs": {
            "the_max": {
              "max": {
                "field": "CreatedDate"
              }
            },
            "the_min": {
              "min": {
                "field": "CreatedDate"
              }
            },
            "diff_max_min": {
              "bucket_script": {
                "buckets_path": {
                  "max": "the_max",
                  "min": "the_min"
                },
                "script": "params.max - params.min"
              }
            }

          }
        }
      }
    }
  }
}

您可以在此处阅读有关管道聚合的更多信息

希望有帮助

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM