# Performance of sorting on nested field

**URL:** <https://discuss.elastic.co/t/performance-of-sorting-on-nested-field/2030>\
**Category:** Elasticsearch\
**Created:** [June 5, 2015, 5:24pm UTC](https://discuss.elastic.co/t/performance-of-sorting-on-nested-field/2030 "2015-06-05T17:24:02Z")\
**Posts on this page:** 1\
**Showing post:** 7

<div class="post-metadata">

**Author:** ![micpalmia](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/micpalmia/32/541_2.png) [@micpalmia](https://discuss.elastic.co/u/micpalmia)\
**Post date:** [February 1, 2016, 9:18am UTC](https://discuss.elastic.co/t/performance-of-sorting-on-nested-field/2030/7 "2016-02-01T09:18:42Z")

</div>

My use case required me to return a set of documents sorted using nested objects, followed by everything else scored normally. I wanted to solve this by adding two sort values: the nested sorting first and `_score` second. Unfortunately this turned out to be extremely slow for the reasons mentioned above (I had searches with millions of results, with only a handful of documents containing nested fields).

My solution was to perform two distinct searches at the application level (still in a way that was completely transparent to the caller):

- first filter for documents containing the nested object you want to search on, and sort them: this is going to be fast since you are ensuring that the nested documents are actually there;
- than (if necessary) perform a search for the rest of the documents and return them if needed.

**IMPORTANT**  
After we deployed this system, [memory consumption skyrocketed](https://discuss.elastic.co/t/heap-filled-with-fixedbitsetfiltercache/34524): the only solution was to move to Elasticsearch 2.1 (where the memory issue is completely solved). I have the impression that the memory issues were caused by nested sorting, but I did not have the resources to investigate this thoroughly. Just a warning.

---

_[View the full topic](https://discuss.elastic.co/t/performance-of-sorting-on-nested-field/2030)._
