简体   繁体   中英

Aggregated output from ElasticSearch

I am using ElasticSearch to store around 50 million data. The format of the data is as below.

_id:5e444fc1c5e86c84d1044aeb
meter_id:"jhk"
date:2017-06-17T18:39:28.795+00:00
activePower:Object
unit:"kwh"
Active Power L1:"0"
Active Power L2:"0"
Active Power L3:"0"
Total Active Power:"5"
reactivePower:Object
apparentPower:Object
frequency:Object
thd:Object

Now I want to take a part of data between two dates and group it according to a single date and add total active power field a specific date group.

expected output

[
{ date: '04-19', totalActivePower: 5000},
{ date: '05-19', totalActivePower: 6000}
]

You can use date_histogram aggregation

Query

{
  "size": 0, 
  "query": {
    "range": {    --> to filter between dates, you can also use filter aggregation
      "date": {
        "gte": "2017-06-16",
        "lte": "2017-06-17"
      }
    }
  },
  "aggs": {
    "perday": {  
      "date_histogram": {   ---> will split documents on per day basis
        "field": "date",
        "interval": "day"
      },
      "aggs": {
        "totalacticepower": { -- sum of field Total Active Power on that day
          "sum": {
            "field": "Total Active Power"
          }
        }
      }
    }
  }
}

Result:

"aggregations" : {
    "perday" : {
      "buckets" : [
        {
          "key_as_string" : "2017-06-17T00:00:00.000Z",
          "key" : 1497657600000,
          "doc_count" : 1,
          "totalacticepower" : {
            "value" : 5.0
          }
        }
      ]
    }
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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