# Get number of days in a month to aggregate data

**URL:** https://discuss.elastic.co/t/get-number-of-days-in-a-month-to-aggregate-data/206028
**Category:** Elasticsearch
**Created:** [October 31, 2019, 11:42am UTC](https://discuss.elastic.co/t/get-number-of-days-in-a-month-to-aggregate-data/206028 "2019-10-31T11:42:09Z")
**Posts on this page:** 8
**Page:** 1

<div class="post-metadata">

### Author: ![Quang\_D\_Tr\_n](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/quang_d_tr_n/32/54722_2.png) [@Quang\_D\_Tr\_n](https://discuss.elastic.co/u/Quang_D_Tr_n)
#### Post date: [October 31, 2019, 11:42am UTC](https://discuss.elastic.co/t/get-number-of-days-in-a-month-to-aggregate-data/206028/1 "2019-10-31T11:42:10Z")

</div>

I am trying to get the number of days of a month from a timestamp field, and then, divide the one numeric field by this number of days.  
Right now, my implementation is like this:

```auto
{
    "query" : {...},
    "script_fields": {
        "days_of_month": {
            "script": {
                "source": "Calendar cal = Calendar.getInstance(); cal.setTimeInMillis(doc['timestamp'].value); return cal.getActualMaximum(Calendar.DAY_OF_MONTH);"
            }
        }
    },
    "aggregations": {...}
}

```

My intention is to create a new field named `days_of_month` and then later put it in `bucket_script params` to use in aggregation part. But the result does not returns any `days_of_month`.  
If I put the `script_fields` under `aggregation` then it the server can't parse the json.

Any idea to implement this kind of aggregation would be very much appreciated.

Thank you!

* * *

UPDATE:  
Since I set the size as 0 because I only care about the aggregation result so the `days_of_month` is missing. Set the size as 1 and this will return the number of days in the month.

---

<div class="post-metadata">

### Author: ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)
#### Post date: [October 31, 2019, 3:04pm UTC](https://discuss.elastic.co/t/get-number-of-days-in-a-month-to-aggregate-data/206028/2 "2019-10-31T15:04:37Z")

</div>

is there any chance you can use a script in aggregation (like terms) to figure out the right logic to bucket.. maybe you can explain what you are after in more detail.

---

<div class="post-metadata">

### Author: ![Quang\_D\_Tr\_n](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/quang_d_tr_n/32/54722_2.png) [@Quang\_D\_Tr\_n](https://discuss.elastic.co/u/Quang_D_Tr_n)
#### Post date: [October 31, 2019, 3:24pm UTC](https://discuss.elastic.co/t/get-number-of-days-in-a-month-to-aggregate-data/206028/3 "2019-10-31T15:24:09Z")

</div>

@spinscale Thanks Alex, I figure out what I am missing. Since I set the size as 0 (I use result in aggregation only) so that scripted field is missing. After set the size as 1, it comes back as expected.

In `aggregation` part, I group data daily in a month. I already have the `_source.month_price` so scripted field above is for division to get daily price. After that, I will multiply the field `extra_usage` with the `daily_price` to have daily daily charge. That is what I am after.

However, scripted field is not able to be reused in aggregation. Hence, there will be duplicate calculation for the value of the scrpited field if I put the whole script (get days of a month) in aggregation.

Maybe I have to do it in the client side. 🙂

Cheers!

---

<div class="post-metadata">

### Author: ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)
#### Post date: [November 4, 2019, 9:51am UTC](https://discuss.elastic.co/t/get-number-of-days-in-a-month-to-aggregate-data/206028/4 "2019-11-04T09:51:30Z")

</div>

it seems to me, that you could do that scripting calculation as part of the aggregation without any need of a scripted field

---

<div class="post-metadata">

### Author: ![Quang\_D\_Tr\_n](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/quang_d_tr_n/32/54722_2.png) [@Quang\_D\_Tr\_n](https://discuss.elastic.co/u/Quang_D_Tr_n)
#### Post date: [November 5, 2019, 4:19am UTC](https://discuss.elastic.co/t/get-number-of-days-in-a-month-to-aggregate-data/206028/5 "2019-11-05T04:19:20Z")

</div>

yes Alex, but if the script as a part of aggregation then the result will be calculated for many times, depending on the hit bucket size, leading to duplication.  
That is why I want it to be calculated once only and then use it in aggregation later but it seems to be a challenge.

---

<div class="post-metadata">

### Author: ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)
#### Post date: [November 5, 2019, 9:35am UTC](https://discuss.elastic.co/t/get-number-of-days-in-a-month-to-aggregate-data/206028/6 "2019-11-05T09:35:18Z")

</div>

I am not sure I understand the last post. A script field will also be executed for every hit.

---

<div class="post-metadata">

### Author: ![Quang\_D\_Tr\_n](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/quang_d_tr_n/32/54722_2.png) [@Quang\_D\_Tr\_n](https://discuss.elastic.co/u/Quang_D_Tr_n)
#### Post date: [November 6, 2019, 7:40am UTC](https://discuss.elastic.co/t/get-number-of-days-in-a-month-to-aggregate-data/206028/7 "2019-11-06T07:40:59Z")

</div>

Really? I notice in the result json, there is only one section for field script if I put it outside aggregation. If I put it inside aggregation, it will return the same number of output as aggregation hits.  
So I assume that putting field script outside of the aggregation section, it will be executed once only.

Is that so?

```auto
        "days_of_month": {
            "script": {
                "source": "Calendar cal = Calendar.getInstance(); cal.setTimeInMillis(doc['timestamp'].value); return cal.getActualMaximum(Calendar.DAY_OF_MONTH);"
            }
        }
    },
    "aggregations": {...}

//Inside aggregation

    "aggregations": {
        "script_fields": {
                "days_of_month": {
                     "script": {
                        "source": "Calendar cal = Calendar.getInstance();cal.setTimeInMillis(doc['timestamp'].value); return cal.getActualMaximum(Calendar.DAY_OF_MONTH);"
                    }
                }
          },...
}
```

---

<div class="post-metadata">

### Author: ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)
#### Post date: [December 4, 2019, 7:41am UTC](https://discuss.elastic.co/t/get-number-of-days-in-a-month-to-aggregate-data/206028/8 "2019-12-04T07:41:00Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
