ElasticSearch数据的组,计算和顺序

问题描述 投票:0回答:1

我以下列格式存储了大量数据(我简化了数据以解释问题)。

enter image description here

我需要的是:

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

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

谢谢。

c# elasticsearch nest elasticsearch-net
1个回答
1
投票

情况下您的映射看起来像这样:

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"
              }
            }

          }
        }
      }
    }
  }
}

您可以阅读有关管道聚集here的更多信息>

希望有所帮助

© www.soinside.com 2019 - 2024. All rights reserved.