简体   繁体   English

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

[英]Group, calculation and order of ElasticSearch data

I have a lot of data stored in the following format (I simplified the data to explain the problem).我有很多以以下格式存储的数据(我简化了数据以解释问题)。

在此处输入图像描述

What I need is:我需要的是:

  • group all the data by "Action Id" field按“操作 ID”字段对所有数据进行分组
  • calculate the difference between max and min values of "Created Time" for each group (from the previous action)计算每个组的“创建时间”的最大值和最小值之间的差异(来自上一个操作)
  • order the results by the calculated field ("Action duration" - difference between max and min)按计算字段排序结果(“操作持续时间” - 最大值和最小值之间的差异)

I use NEST (C#) to query the ElasticSearch.我使用 NEST (C#) 来查询 ElasticSearch。 I think that if you can help me with native Elastic query it also will be very helpful, I'll translate it to C#.我认为,如果您可以帮助我使用本机 Elastic 查询,它也会非常有帮助,我会将其翻译为 C#。

Thank you.谢谢你。

Case your mappings looks like that:如果您的映射如下所示:

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

Your elasticsearch query should look like that:您的 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"
              }
            }

          }
        }
      }
    }
  }
}

You can read more about Pipeline Aggregetions here您可以在此处阅读有关管道聚合的更多信息

Hope that helps希望有帮助

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

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