How to get Sum of a particular field for current month and sum of that field for the last month till date?


(anusha) #1

Hi All,

"_index": "xmlactivity_prod_201501", This index is for 2015-jan month for feb :" xmlactivity_prod_201502" and so on.
"_type": "xmlactivityv2",

I have my sample data as shown below:

      "created": "2015-01-15T17:08:45.000Z",
      "netamt": 0

Have two fields and their mapping as shown :
"created": {
"type": "date",
"format": "dateOptionalTime"
}
"netamt": {
"type": "double"
}

I would like to calculate netamt sum for current month current year and for the last month current year till date

For this am using a query:

GET xmlactivity_prod_2015*/_search
{
"size": 0,
"aggs": {
"articles_over_time": {
"date_histogram": {
"field": "created",
"format": "yyyy-MM-dd",
"interval": "month",
"min_doc_count": 1
},
"aggs": {
"netamt_sum": {
"sum": {
"field": "netamt"
}
}
}
}
}
}

This is giving the output as complete month netamt sum for every month.

Response:
"buckets": [
{
"key_as_string": "2015-07-01",
"key": 1435708800000,
"doc_count": 7614791,
"netamt_sum": {
"value": 23869820.07780247
}
},
{
"key_as_string": "2015-08-01",
"key": 1438387200000,
"doc_count": 7311611,
"netamt_sum": {
"value": 22785676.593002092
}
},
{
"key_as_string": "2015-09-01",
"key": 1441065600000,
"doc_count": 3896206,
"netamt_sum": {
"value": 12117393.990000023
}
}
]

But I would like to get current month netamt sum and last month netamt sum till date, similarly for current year netamt sum and last year netamt sum till date ??????????

Can any one help me out for to resolve this scenario?????


(system) #2