Igor
Igor

Reputation: 281

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).

enter image description here

What I need is:

I use NEST (C#) to query the 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#.

Thank you.

Upvotes: 0

Views: 374

Answers (1)

Assael Azran
Assael Azran

Reputation: 2993

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:

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

Upvotes: 1

Related Questions