# Using scripted fields in filter range sub-aggregation

**URL:** <https://discuss.elastic.co/t/using-scripted-fields-in-filter-range-sub-aggregation/104546>\
**Category:** Kibana\
**Created:** [October 19, 2017, 11:17am UTC](https://discuss.elastic.co/t/using-scripted-fields-in-filter-range-sub-aggregation/104546 "2017-10-19T11:17:26Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![A.McBride](https://avatars.discourse-cdn.com/v4/letter/a/f17d59/32.png) [@A.McBride](https://discuss.elastic.co/u/A.McBride)\
**Post date:** [October 19, 2017, 11:17am UTC](https://discuss.elastic.co/t/using-scripted-fields-in-filter-range-sub-aggregation/104546/1 "2017-10-19T11:17:26Z")

</div>

I'm making a table in Kibana and one of the things I want to be able to show is the count of documents that have the field "outcome" with a value "OVERRIDE" and a date range between some date and now.  
The outcome filter is fine; I just use outcome:OVERRIDE, but the date range filter is tripping me up. I've tried using the following format:

date\_time:[successful\_date\_time TO now]

However this doesn't work even though I've specified successful\_date\_time (which is a scripted field) to have the format "YYYY-MM-DDTHH:mm:ss.SSS" as the moment.js format pattern. An example output from this field would be 2017-10-17T15:52:12.647 , and when I use this value manually for the starting point of the range, i.e. date\_time:[2017-10-17T15:52:12.647 TO now], the query works fine, even though successful\_date\_time outputs this value. Is there a way to use this scripted field as a range marker for my query?

---

<div class="post-metadata">

**Author:** ![timroes](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/timroes/32/19712_2.png) [@timroes](https://discuss.elastic.co/u/timroes)\
**Post date:** [October 24, 2017, 8:17am UTC](https://discuss.elastic.co/t/using-scripted-fields-in-filter-range-sub-aggregation/104546/2 "2017-10-24T08:17:43Z")

</div>

Hi Andrew,

sorry for the late reply.

Unfortunately it is not possible to use scripted fields within a query, due to the way Elasticsearch handles those.  
With Kibana 6 we are introducing a new query language called `Kuery`, in which we hope, that we will be able to address that issue in a future version, so it's not as confusing anymore as it is today.

Cheers,  
Tim

---

<div class="post-metadata">

**Author:** ![A.McBride](https://avatars.discourse-cdn.com/v4/letter/a/f17d59/32.png) [@A.McBride](https://discuss.elastic.co/u/A.McBride)\
**Post date:** [October 24, 2017, 8:54am UTC](https://discuss.elastic.co/t/using-scripted-fields-in-filter-range-sub-aggregation/104546/3 "2017-10-24T08:54:51Z")

</div>

Hi Tim,

Thanks for getting back to me. Just for future reference, does this also mean that regular stored fields also can't be used withing queries? e.g. If I wanted a query like date\_time:[@timestamp TO now], would that simply not work or is there a specific syntax that would make it work?

Thanks for the help,

Andrew.

---

<div class="post-metadata">

**Author:** ![timroes](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/timroes/32/19712_2.png) [@timroes](https://discuss.elastic.co/u/timroes)\
**Post date:** [October 24, 2017, 9:00am UTC](https://discuss.elastic.co/t/using-scripted-fields-in-filter-range-sub-aggregation/104546/4 "2017-10-24T09:00:53Z")

</div>

Hi Andrew,

sorry my answer was a bit confusing in that! No you cannot reference other fields in queries in general. I meant, that you are also not able to properly query scripted fields at all.

You cannot reference fields, due to the way Elasticsearch stores and query the data internally. It builds up inverse indexes for each field, so at query time, when you query for field `x` it has no link to any other field value in the same document.

Cheers,  
Tim

---

<div class="post-metadata">

**Author:** ![A.McBride](https://avatars.discourse-cdn.com/v4/letter/a/f17d59/32.png) [@A.McBride](https://discuss.elastic.co/u/A.McBride)\
**Post date:** [October 24, 2017, 9:25am UTC](https://discuss.elastic.co/t/using-scripted-fields-in-filter-range-sub-aggregation/104546/5 "2017-10-24T09:25:59Z")

</div>

Hi Tim,

Thanks for the quick response. I'll be sure to keep up with Kuery and all of its features!

Kind regards,  
Andrew.

---

<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:** [November 21, 2017, 9:26am UTC](https://discuss.elastic.co/t/using-scripted-fields-in-filter-range-sub-aggregation/104546/6 "2017-11-21T09:26:06Z")

</div>

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