# Sorting date fields

**URL:** <https://discuss.elastic.co/t/sorting-date-fields/16017>\
**Category:** Elasticsearch\
**Created:** [February 25, 2014, 9:23pm UTC](https://discuss.elastic.co/t/sorting-date-fields/16017 "2014-02-25T21:23:01Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![Adrian\_2](https://avatars.discourse-cdn.com/v4/letter/a/c57346/32.png) [@Adrian\_2](https://discuss.elastic.co/u/Adrian_2)\
**Post date:** [February 25, 2014, 9:23pm UTC](https://discuss.elastic.co/t/sorting-date-fields/16017/1 "2014-02-25T21:23:01Z")

</div>

Hi all,

I have a question on how sorting during queries works in elasticsearch.

I have an index with a custom date format field, on which the sort is applied.  
When quering the index for a given keywork, results are provided with the given  
sort.

However, I've observed that some documents are not present in the result set. I  
would have expected these results to be part of the result set as it would be  
in relational systems using the SQL ORDER BY statement. I've verified that  
these missing documents are covered by the query using the explain api.  
According to the documentation, score computation ist not performed when using  
sorts on fields.

Maybe someone can provide more information on how sorting is done?

I am using Elasticsearch 1.0.0RC1 on debian whezzy with openjdk7-jdk.

Thanks, Adrian

--  
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/20140225212301.GA22436%40server1.850nm.net](https://groups.google.com/d/msgid/elasticsearch/20140225212301.GA22436%40server1.850nm.net).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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:** [February 25, 2014, 10:11pm UTC](https://discuss.elastic.co/t/sorting-date-fields/16017/2 "2014-02-25T22:11:13Z")

</div>

ES loads the values of the fields to sort on into memory cache.

You should update to 1.0.0, maybe you hit a bug that has been fixed.

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/CAKdsXoHo\_N3LXo6NiGztL5%3D1GaqVG4QOTCX32OuAjYeOhGfFng%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAKdsXoHo_N3LXo6NiGztL5%3D1GaqVG4QOTCX32OuAjYeOhGfFng%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:** ![Adrian\_2](https://avatars.discourse-cdn.com/v4/letter/a/c57346/32.png) [@Adrian\_2](https://discuss.elastic.co/u/Adrian_2)\
**Post date:** [February 25, 2014, 10:28pm UTC](https://discuss.elastic.co/t/sorting-date-fields/16017/3 "2014-02-25T22:28:29Z")

</div>

On Tue, Feb 25, 2014 at 11:11:13PM +0100, [joergprante@gmail.com](mailto:joergprante@gmail.com) wrote:

Jörg,

> ES loads the values of the fields to sort on into memory cache.

Yes, I've read that - is it known when these caches are flushed?

> You should update to 1.0.0, maybe you hit a bug that has been fixed.

I'll do that. I am just wondering if I am missing something ..

Best regards, Adrian

--  
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/20140225222829.GB22436%40server1.850nm.net](https://groups.google.com/d/msgid/elasticsearch/20140225222829.GB22436%40server1.850nm.net).  
For more options, visit [https://groups.google.com/groups/opt\_out](https://groups.google.com/groups/opt_out).

---

<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:** [February 25, 2014, 11:21pm UTC](https://discuss.elastic.co/t/sorting-date-fields/16017/4 "2014-02-25T23:21:26Z")

</div>

For the cache, see

[http://www.elasticsearch.org/guide/en/elasticsearch/reference/1.x/index-modules-fielddata.html](http://www.elasticsearch.org/guide/en/elasticsearch/reference/1.x/index-modules-fielddata.html)

By default, the field cache size is unbounded, and does not expire. For  
sort, it means that each field to sort is examined, all values of the field  
are loaded, so the in-memory sorting can take place. It's exactly the same  
what Lucene is executing.

With the default settings of the field cache, sort is working alright  
(unless the field values will exceed the available memory)

Maybe you can set up an example of your sort as a demo, so that the error  
can be reproduced?

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/CAKdsXoF4zOnB5TJddf3EZTgGtDqXSBzJAHpZAPo8biUE35wGQg%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAKdsXoF4zOnB5TJddf3EZTgGtDqXSBzJAHpZAPo8biUE35wGQg%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:** ![Adrian\_2](https://avatars.discourse-cdn.com/v4/letter/a/c57346/32.png) [@Adrian\_2](https://discuss.elastic.co/u/Adrian_2)\
**Post date:** [March 4, 2014, 10:18pm UTC](https://discuss.elastic.co/t/sorting-date-fields/16017/5 "2014-03-04T22:18:19Z")

</div>

On Wed, Feb 26, 2014 at 12:21:26AM +0100, [joergprante@gmail.com](mailto:joergprante@gmail.com) wrote:

Jörg,

sorry for the late answer.

> Maybe you can set up an example of your sort as a demo, so that the error  
> can be reproduced?

It turned out that this behaviour was caused by me, since documents contained  
the wrong timestamp on the sorted field. After fixing this, the results were as  
expected.

Thanks for the pointers and your help,

Adrian

--  
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/20140304221819.GA31519%40server1.850nm.net](https://groups.google.com/d/msgid/elasticsearch/20140304221819.GA31519%40server1.850nm.net).  
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:45am UTC](https://discuss.elastic.co/t/sorting-date-fields/16017/6 "2017-07-06T01:45:44Z")

</div>


