# Sorting on subset of values

**URL:** <https://discuss.elastic.co/t/sorting-on-subset-of-values/10327>\
**Category:** Elasticsearch\
**Created:** [January 14, 2013, 8:33am UTC](https://discuss.elastic.co/t/sorting-on-subset-of-values/10327 "2013-01-14T08:33:51Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![jasisz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jasisz/32/2538_2.png) [@jasisz](https://discuss.elastic.co/u/jasisz)\
**Post date:** [January 14, 2013, 8:33am UTC](https://discuss.elastic.co/t/sorting-on-subset-of-values/10327/1 "2013-01-14T08:33:51Z")

</div>

There is some document I want to search on which contains multiple values  
in one field - e.g. multiple dates.  
Now I want to filter documents by those dates - for example later than  
today - and then sort them by dates. But I would like to sort NOT on all  
dates (which would be not relevant), but only on those later than today.

I cannot figure out how to achieve this simple scenario with Elasticsearch.  
I have tried to use Script Based Sorting, make use of nested documents and  
so on, but maybe I'm missing something really clear and simple.

--

---

<div class="post-metadata">

**Author:** ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)\
**Post date:** [January 15, 2013, 1:47am UTC](https://discuss.elastic.co/t/sorting-on-subset-of-values/10327/2 "2013-01-15T01:47:12Z")

</div>

What you want to do is not simple. I am also not quite sure what you are  
trying to achieve. So, if you have several dates later than today, which  
date are you going to use to sort on?

On Monday, January 14, 2013 3:33:51 AM UTC-5, jasisz wrote:

> There is some document I want to search on which contains multiple values  
> in one field - e.g. multiple dates.  
> Now I want to filter documents by those dates - for example later than  
> today - and then sort them by dates. But I would like to sort NOT on all  
> dates (which would be not relevant), but only on those later than today.
> 
> I cannot figure out how to achieve this simple scenario with  
> Elasticsearch. I have tried to use Script Based Sorting, make use of nested  
> documents and so on, but maybe I'm missing something really clear and  
> simple.

--

---

<div class="post-metadata">

**Author:** ![jasisz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jasisz/32/2538_2.png) [@jasisz](https://discuss.elastic.co/u/jasisz)\
**Post date:** [January 15, 2013, 7:08am UTC](https://discuss.elastic.co/t/sorting-on-subset-of-values/10327/3 "2013-01-15T07:08:54Z")

</div>

I know - it is simple in relational world, but not here...

If I have several dates later than today I want to sort by the earliest one.

W dniu wtorek, 15 stycznia 2013 02:47:12 UTC+1 użytkownik Igor Motov  
napisał:

> What you want to do is not simple. I am also not quite sure what you are  
> trying to achieve. So, if you have several dates later than today, which  
> date are you going to use to sort on?
> 
> On Monday, January 14, 2013 3:33:51 AM UTC-5, jasisz wrote:
> 
> > There is some document I want to search on which contains multiple values  
> > in one field - e.g. multiple dates.  
> > Now I want to filter documents by those dates - for example later than  
> > today - and then sort them by dates. But I would like to sort NOT on all  
> > dates (which would be not relevant), but only on those later than today.
> > 
> > I cannot figure out how to achieve this simple scenario with  
> > Elasticsearch. I have tried to use Script Based Sorting, make use of nested  
> > documents and so on, but maybe I'm missing something really clear and  
> > simple.

--

---

<div class="post-metadata">

**Author:** ![jasisz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jasisz/32/2538_2.png) [@jasisz](https://discuss.elastic.co/u/jasisz)\
**Post date:** [January 15, 2013, 7:10am UTC](https://discuss.elastic.co/t/sorting-on-subset-of-values/10327/4 "2013-01-15T07:10:21Z")

</div>

I know - it is simple in relational world, but not here...

If I have several dates later than today I want to sort by the earliest one.

On Tuesday, January 15, 2013 2:47:12 AM UTC+1, Igor Motov wrote:

> What you want to do is not simple. I am also not quite sure what you are  
> trying to achieve. So, if you have several dates later than today, which  
> date are you going to use to sort on?
> 
> On Monday, January 14, 2013 3:33:51 AM UTC-5, jasisz wrote:
> 
> > There is some document I want to search on which contains multiple values  
> > in one field - e.g. multiple dates.  
> > Now I want to filter documents by those dates - for example later than  
> > today - and then sort them by dates. But I would like to sort NOT on all  
> > dates (which would be not relevant), but only on those later than today.
> > 
> > I cannot figure out how to achieve this simple scenario with  
> > Elasticsearch. I have tried to use Script Based Sorting, make use of nested  
> > documents and so on, but maybe I'm missing something really clear and  
> > simple.

--

---

<div class="post-metadata">

**Author:** ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)\
**Post date:** [January 15, 2013, 3:48pm UTC](https://discuss.elastic.co/t/sorting-on-subset-of-values/10327/5 "2013-01-15T15:48:04Z")

</div>

You can write a sort script[http://www.elasticsearch.org/guide/reference/api/search/sort.html](http://www.elasticsearch.org/guide/reference/api/search/sort.html)that would take a list of dates for the current document (using document  
fields or stored fields[http://www.elasticsearch.org/guide/reference/modules/scripting.html](http://www.elasticsearch.org/guide/reference/modules/scripting.html))  
throw out everything older than today and then return the smallest date.

On Tuesday, January 15, 2013 2:10:21 AM UTC-5, jasisz wrote:

> I know - it is simple in relational world, but not here...
> 
> If I have several dates later than today I want to sort by the earliest  
> one.
> 
> On Tuesday, January 15, 2013 2:47:12 AM UTC+1, Igor Motov wrote:
> 
> > What you want to do is not simple. I am also not quite sure what you are  
> > trying to achieve. So, if you have several dates later than today, which  
> > date are you going to use to sort on?
> > 
> > On Monday, January 14, 2013 3:33:51 AM UTC-5, jasisz wrote:
> > 
> > > There is some document I want to search on which contains multiple  
> > > values in one field - e.g. multiple dates.  
> > > Now I want to filter documents by those dates - for example later than  
> > > today - and then sort them by dates. But I would like to sort NOT on all  
> > > dates (which would be not relevant), but only on those later than today.
> > > 
> > > I cannot figure out how to achieve this simple scenario with  
> > > Elasticsearch. I have tried to use Script Based Sorting, make use of nested  
> > > documents and so on, but maybe I'm missing something really clear and  
> > > simple.

--

---

<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:** [July 6, 2017, 2:56am UTC](https://discuss.elastic.co/t/sorting-on-subset-of-values/10327/6 "2017-07-06T02:56:22Z")

</div>


