# Cannot use scripted field in a date range bucket aggregation

**URL:** <https://discuss.elastic.co/t/cannot-use-scripted-field-in-a-date-range-bucket-aggregation/258711>\
**Category:** Kibana\
**Created:** [December 15, 2020, 12:10pm UTC](https://discuss.elastic.co/t/cannot-use-scripted-field-in-a-date-range-bucket-aggregation/258711 "2020-12-15T12:10:00Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![kspe](https://avatars.discourse-cdn.com/v4/letter/k/e79b87/32.png) [@kspe](https://discuss.elastic.co/u/kspe)\
**Post date:** [December 15, 2020, 12:10pm UTC](https://discuss.elastic.co/t/cannot-use-scripted-field-in-a-date-range-bucket-aggregation/258711/1 "2020-12-15T12:10:00Z")

</div>

Hey,

I have a `time` field in my document which is a timestamp number. It is not set as a `date` type field so cannot use it in the date range bucket aggregation directly (Kibana complains that the index does not contain any compatible field types: date).

My idea was so create a scripted field e.g. `parsed_date` with type `date` on the same index and return a `Date` object based on the timestamp. I've managed to add it with painless script as following:

```auto
return new Date(doc["time"].value * 1000);

```

So far all good, the timestamp is parsed properly and on the preview, I can see that my new field is returned:

 ![Screen Shot 2020-12-15 at 13.04.39](https://us1.discourse-cdn.com/elastic/original/3X/d/6/d6b654dfdcaae4fce710df7a9f5aefc2eb842dd6.png)

The issue is that the scripted field is always returned as an array hence the result I get is:

```auto
"parsed_date": [
   "2020-11-19T00:00:00.000Z"
  ]

```

instead of:

```auto
"parsed_date": "2020-11-19T00:00:00.000Z"

```

I've found this brief explanation of why is it so: [Script\_fields always returns array. how to return an object or simple data type?](https://discuss.elastic.co/t/script-fields-always-returns-array-how-to-return-an-object-or-simple-data-type/146891) and it does make sense to me.

Now when I try to use that new scripted field in a date range agg. I get 500 error with the error message:

```auto
Unsupported script value [Tue Nov 17 00:00:00 UTC 2020], expected a number, date, or boolean

```

This again makes sense as this date object is within array but not sure how I can overcome this, as I understand it, date range agg. expects object not array but seems I cannot achieve this with scripted fields.

Any hints would be much appreciated, thanks!

---

<div class="post-metadata">

**Author:** ![flash1293](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/flash1293/32/41227_2.png) [@flash1293](https://discuss.elastic.co/u/flash1293)\
**Post date:** [December 15, 2020, 2:34pm UTC](https://discuss.elastic.co/t/cannot-use-scripted-field-in-a-date-range-bucket-aggregation/258711/2 "2020-12-15T14:34:28Z")

</div>

Could you try to return the timestamp directly without creating a Date instance? (still keeping thge "date" type of the field)

```auto
return doc["time"].value * 1000;

```

---

<div class="post-metadata">

**Author:** ![kspe](https://avatars.discourse-cdn.com/v4/letter/k/e79b87/32.png) [@kspe](https://discuss.elastic.co/u/kspe)\
**Post date:** [December 15, 2020, 3:13pm UTC](https://discuss.elastic.co/t/cannot-use-scripted-field-in-a-date-range-bucket-aggregation/258711/3 "2020-12-15T15:13:28Z")

</div>

This does work, thanks a lot!

---

<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:** [January 12, 2021, 3:13pm UTC](https://discuss.elastic.co/t/cannot-use-scripted-field-in-a-date-range-bucket-aggregation/258711/4 "2021-01-12T15:13:33Z")

</div>

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