# Sorting on timestamps from multiple fields

**URL:** <https://discuss.elastic.co/t/sorting-on-timestamps-from-multiple-fields/18069>\
**Category:** Elasticsearch\
**Created:** [June 12, 2014, 1:29pm UTC](https://discuss.elastic.co/t/sorting-on-timestamps-from-multiple-fields/18069 "2014-06-12T13:29:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jurian\_Sluiman](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jurian_sluiman/32/1128_2.png) [@Jurian\_Sluiman](https://discuss.elastic.co/u/Jurian_Sluiman)\
**Post date:** [June 12, 2014, 1:29pm UTC](https://discuss.elastic.co/t/sorting-on-timestamps-from-multiple-fields/18069/1 "2014-06-12T13:29:04Z")

</div>

In Elasticsearch I'd like to index items of multiple types. Examples are "blog posts" and "deals". All types in the index should be sorted according to their publish date, with a single exception: deals are valid in a certain time span. The sorting of deals within the complete list should NOT be based on publish time but on the "valid until" timestamp. So the question is, how can I sort on the publish date of the blog post and the "valid until" date of the deals, in a single query?  
_Raw example data_

12/06: Blog post 8  
11/06: Blog post 7  
09/06: Blog post 6  
09/06: Deal 2  
06/06: Blog post 5  
03/06: Blog post 4  
29/05: Blog post 3  
27/05: Blog post 2  
26/05: Deal 1  
26/05: Blog post 1

The deal #2 is available from 09/06 - 23/06, deal #1 was available from 26/05 - 09/06 and so on (every x weeks n new deals valid for a T period of time, possible overlap).  
_Expected result_

This should be the result of the search:

09/06: Deal 2 // Valid until 23/06, so this should be on top  
12/06: Blog post 8  
11/06: Blog post 7  
09/06: Blog post 6  
26/05: Deal 1 // It was valid until 09/06, so place this just before the blog post from 09/06  
06/06: Blog post 5  
03/06: Blog post 4  
29/05: Blog post 3  
27/05: Blog post 2  
26/05: Blog post 1

To make things more difficult, I have to consider these things as well:

1. Pagination should work no matter of the paging size (shouln't be a big issue I guess)
2. More types could be added in the future (shouln't be a big issue as well)
3. Other types (say, events) could have a same system, where another time span ("promotional period") will be used
4. All items can be tagged / categorized and it should be able to filter on these tags and use faceted search, all having the same sorting constraints

It's the first time I dive into ES, so I am not sure if it clear to it's just a matter of RTFM, but please give me a nudge in the right direction 🙂

--

Jurian

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/56737978-9303-408d-b7a6-cd515b2e40f2%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/56737978-9303-408d-b7a6-cd515b2e40f2%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![jprante](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jprante/32/44941_2.png) [@jprante](https://discuss.elastic.co/u/jprante)\
**Post date:** [June 12, 2014, 3:28pm UTC](https://discuss.elastic.co/t/sorting-on-timestamps-from-multiple-fields/18069/2 "2014-06-12T15:28:43Z")

</div>

If you have two (or more) date fields to sort on, look at "copy\_to" mapping  
feature to copy them over to a third field e.g. "sort\_date". So you have a  
single field you can happily to sort on, without having to change fields in  
the source.

Same method works for tag/category fields in different indexes that are  
meant for facets that can span more than one index.

Jörg

On Thu, Jun 12, 2014 at 3:29 PM, Jurian Sluiman [jurian@soflomo.com](mailto:jurian@soflomo.com) wrote:

> In Elasticsearch I'd like to index items of multiple types. Examples are "blog posts" and "deals". All types in the index should be sorted according to their publish date, with a single exception: deals are valid in a certain time span. The sorting of deals within the complete list should NOT be based on publish time but on the "valid until" timestamp. So the question is, how can I sort on the publish date of the blog post and the "valid until" date of the deals, in a single query?  
> _Raw example data_
> 
> 12/06: Blog post 8  
> 11/06: Blog post 7  
> 09/06: Blog post 6  
> 09/06: Deal 2  
> 06/06: Blog post 5  
> 03/06: Blog post 4  
> 29/05: Blog post 3  
> 27/05: Blog post 2  
> 26/05: Deal 1  
> 26/05: Blog post 1
> 
> The deal #2 is available from 09/06 - 23/06, deal #1 was available from 26/05 - 09/06 and so on (every x weeks n new deals valid for a T period of time, possible overlap).  
> _Expected result_
> 
> This should be the result of the search:
> 
> 09/06: Deal 2 // Valid until 23/06, so this should be on top  
> 12/06: Blog post 8  
> 11/06: Blog post 7  
> 09/06: Blog post 6  
> 26/05: Deal 1 // It was valid until 09/06, so place this just before the blog post from 09/06  
> 06/06: Blog post 5  
> 03/06: Blog post 4  
> 29/05: Blog post 3  
> 27/05: Blog post 2  
> 26/05: Blog post 1
> 
> To make things more difficult, I have to consider these things as well:
> 
> 1. Pagination should work no matter of the paging size (shouln't be a big issue I guess)
> 2. More types could be added in the future (shouln't be a big issue as well)
> 3. Other types (say, events) could have a same system, where another time span ("promotional period") will be used
> 4. All items can be tagged / categorized and it should be able to filter on these tags and use faceted search, all having the same sorting constraints
> 
> It's the first time I dive into ES, so I am not sure if it clear to it's just a matter of RTFM, but please give me a nudge in the right direction 🙂
> 
> --
> 
> Jurian
> 
> --  
> You received this message because you are subscribed to the Google Groups  
> "elasticsearch" group.  
> To unsubscribe from this group and stop receiving emails from it, send an  
> email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
> To view this discussion on the web visit  
> [https://groups.google.com/d/msgid/elasticsearch/56737978-9303-408d-b7a6-cd515b2e40f2%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/56737978-9303-408d-b7a6-cd515b2e40f2%40googlegroups.com)  
> [https://groups.google.com/d/msgid/elasticsearch/56737978-9303-408d-b7a6-cd515b2e40f2%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/56737978-9303-408d-b7a6-cd515b2e40f2%40googlegroups.com?utm_medium=email&utm_source=footer)  
> .  
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/CAKdsXoEy0LxMsiABW6up8jWsVYBXqUjHs8zTLHOWe68q8tAiXA%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAKdsXoEy0LxMsiABW6up8jWsVYBXqUjHs8zTLHOWe68q8tAiXA%40mail.gmail.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Jurian\_Sluiman](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jurian_sluiman/32/1128_2.png) [@Jurian\_Sluiman](https://discuss.elastic.co/u/Jurian_Sluiman)\
**Post date:** [June 17, 2014, 8:52am UTC](https://discuss.elastic.co/t/sorting-on-timestamps-from-multiple-fields/18069/3 "2014-06-17T08:52:46Z")

</div>

Thanks for the response 🙂

## I was now testing it out with the \_timestamp field which I need to set manually for each item, but copy\_to seems even better. Thanks for the insights!

Jurian

On Thursday, June 12, 2014 5:28:49 PM UTC+2, Jörg Prante wrote:

> If you have two (or more) date fields to sort on, look at "copy\_to"  
> mapping feature to copy them over to a third field e.g. "sort\_date". So you  
> have a single field you can happily to sort on, without having to change  
> fields in the source.
> 
> Same method works for tag/category fields in different indexes that are  
> meant for facets that can span more than one index.
> 
> Jörg

--  
You received this message because you are subscribed to the Google Groups "elasticsearch" group.  
To unsubscribe from this group and stop receiving emails from it, send an email to [elasticsearch+unsubscribe@googlegroups.com](mailto:elasticsearch+unsubscribe@googlegroups.com).  
To view this discussion on the web visit [https://groups.google.com/d/msgid/elasticsearch/755c9765-581d-425c-8bde-86a463d9f5d8%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/755c9765-581d-425c-8bde-86a463d9f5d8%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<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, 1:21am UTC](https://discuss.elastic.co/t/sorting-on-timestamps-from-multiple-fields/18069/4 "2017-07-06T01:21:53Z")

</div>


