# How to sort by a derived value that includes a moving date?

**URL:** <https://discuss.elastic.co/t/how-to-sort-by-a-derived-value-that-includes-a-moving-date/160101>\
**Category:** Elasticsearch\
**Created:** [December 10, 2018, 5:56am UTC](https://discuss.elastic.co/t/how-to-sort-by-a-derived-value-that-includes-a-moving-date/160101 "2018-12-10T05:56:47Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![rpm1984](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rpm1984/32/36595_2.png) [@rpm1984](https://discuss.elastic.co/u/rpm1984)\
**Post date:** [December 10, 2018, 5:56am UTC](https://discuss.elastic.co/t/how-to-sort-by-a-derived-value-that-includes-a-moving-date/160101/1 "2018-12-10T05:56:47Z")

</div>

Hi there, (sorry for all the questions.. for those regular peeps that keep seeing me pop up 🙂 )

I have a requirement to sort the results returned by ElasticSearch by a special value i define, let's call it 'X'.

Now - the problem is, 'X' is a value derived based on:

1. field A in the document (which is a 'term')
2. field B (which is a 'date')
3. the current date (UTC)

So, the problem is obviously 3. The date always changes, therefore i'm not sure how to include this in the sort, since it's not part of the document.

Do i need to use a 'script' here? Will that perform well?

The only other idea that came to mind is to calculate the value nightly, and store that in each document. But that has a few drawbacks:

- i need to have something running in the background to update this value
- could be a lot of documents to update (60%+ every night).
- i lose precision for the value depending on how long between script runs. (if i run nightly, value is 23 hours 'stale')

Any advice?

Thanks

---

<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 7, 2019, 5:56am UTC](https://discuss.elastic.co/t/how-to-sort-by-a-derived-value-that-includes-a-moving-date/160101/2 "2019-01-07T05:56:59Z")

</div>

Closed on January 7, 2019, 5:56am UTC

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