# Sorting by rounded date

**URL:** <https://discuss.elastic.co/t/sorting-by-rounded-date/15675>\
**Category:** Elasticsearch\
**Created:** [February 7, 2014, 6:40pm UTC](https://discuss.elastic.co/t/sorting-by-rounded-date/15675 "2014-02-07T18:40:34Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Karol\_Gwaj](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/karol_gwaj/32/1324_2.png) [@Karol\_Gwaj](https://discuss.elastic.co/u/Karol_Gwaj)\
**Post date:** [February 7, 2014, 6:40pm UTC](https://discuss.elastic.co/t/sorting-by-rounded-date/15675/1 "2014-02-07T18:40:34Z")

</div>

hi,

can elasticsearch sort by rounded date ?  
i want to sort by document creation date but rounded to 1 day, something  
like that:

"sort": [

```
      "CreationDate*/1d*"
  
]

```

solr supports this type of sorting, so i expect elasticsearch is supporting  
it too, but i cant find any documentation for that

i tried scripted sorting but it is much slower, for example sorting 30m  
documents:

"sort": [  
{  
"\_script" :  
{  
"script" : "doc['CreationDate'].value",  
"type" : "number",  
"order": "desc"  
}  
}  
]

vs

"sort": [  
{  
"CreationDate": {  
"order": "desc"  
}  
}  
]

first one (script) is like 50 times slower (i guess because it is loading  
CreationDate from \_source)

--  
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/cbb2fb50-3ccb-4ed3-8de2-95280666241f%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/cbb2fb50-3ccb-4ed3-8de2-95280666241f%40googlegroups.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<div class="post-metadata">

**Author:** ![spinscale](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/spinscale/32/25011_2.png) [@spinscale](https://discuss.elastic.co/u/spinscale)\
**Post date:** [February 10, 2014, 12:53pm UTC](https://discuss.elastic.co/t/sorting-by-rounded-date/15675/2 "2014-02-10T12:53:40Z")

</div>

Hey,

the date field does not get loaded from the source, but from a memory  
structure called fielddata. On certain occasions this fielddata structure  
needs to be removed and then recreated. If you fire a query after a removal  
and the structure has not yet been recreated, you will get into performance  
issues. You can use the warmer API to prevent exactly this behaviour and  
ensure that the fielddata structure is loaded for your date field.

In addition you still do have 30m calculations if you sort by the above  
script. You would gain far more performance, if you could involve filters  
before or maybe take a look at rescoring (not sure if it fits your  
use-case, but you should take a look)

Hope this helps..

--Alex

On Fri, Feb 7, 2014 at 7:40 PM, Karol Gwaj [karol@gwaj.me](mailto:karol@gwaj.me) wrote:

> hi,
> 
> can elasticsearch sort by rounded date ?  
> i want to sort by document creation date but rounded to 1 day, something  
> like that:
> 
> "sort": [
> 
> ```
> "CreationDate*/1d*"
> 
> ]
> 
> ```
> 
> solr supports this type of sorting, so i expect elasticsearch is  
> supporting it too, but i cant find any documentation for that
> 
> i tried scripted sorting but it is much slower, for example sorting 30m  
> documents:
> 
> "sort": [  
> {  
> "\_script" :  
> {  
> "script" : "doc['CreationDate'].value",  
> "type" : "number",  
> "order": "desc"  
> }  
> }  
> ]
> 
> vs
> 
> "sort": [  
> {  
> "CreationDate": {  
> "order": "desc"  
> }  
> }  
> ]
> 
> first one (script) is like 50 times slower (i guess because it is loading  
> CreationDate from \_source)
> 
> --  
> 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/cbb2fb50-3ccb-4ed3-8de2-95280666241f%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/cbb2fb50-3ccb-4ed3-8de2-95280666241f%40googlegroups.com)  
> .  
> For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

--  
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/CAGCwEM-\_FDzDM5JP62LTHEv%3D7M%3DZRFEzmOxCY0CZcRWjQev3gA%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAGCwEM-_FDzDM5JP62LTHEv%3D7M%3DZRFEzmOxCY0CZcRWjQev3gA%40mail.gmail.com).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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:51am UTC](https://discuss.elastic.co/t/sorting-by-rounded-date/15675/3 "2017-07-06T01:51:28Z")

</div>


