# Nested objects, hits, inner hits and sorting

**URL:** <https://discuss.elastic.co/t/nested-objects-hits-inner-hits-and-sorting/32565>\
**Category:** Elasticsearch\
**Created:** [October 20, 2015, 11:32am UTC](https://discuss.elastic.co/t/nested-objects-hits-inner-hits-and-sorting/32565 "2015-10-20T11:32:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![seagullmouse](https://avatars.discourse-cdn.com/v4/letter/s/58956e/32.png) [@seagullmouse](https://discuss.elastic.co/u/seagullmouse)\
**Post date:** [October 20, 2015, 11:32am UTC](https://discuss.elastic.co/t/nested-objects-hits-inner-hits-and-sorting/32565/1 "2015-10-20T11:32:24Z")

</div>

As an example imagine

**Mapping**  
OuterObject contains nested NestedObject.

**Data**  
OuterObject1 contains NestedObject1, NestedObject2  
OuterObject2 contains NestedObject3, NestedObject4

Desired Results (using a nested filter)  
4 hits

1. OuterObject1 and NestedObject1
2. OuterObject1 and NestedObject2
3. OuterObject2 and NestedObject3
4. OuterObject2 and NestedObject4

If I use inner hits then the results look like this

1. OuterObject1, inner\_hits = NestedObject1, NestedObject2
2. OuterObject2, inner\_hits = NestedObject3, NestedObject4

I can then programmatically rewrite these results into the format I want and pretend the 2 hits are actually 4 hits.

**Sorting**  
The problem comes when I then try and sort my results by a field in NestedObject.

I want a sort that compares  
NestedObject1 vs NestedObject2 vs NestedObject3 vs NestedObject4

The sort within inner\_hits separately compares  
NestedObject1 vs NestedObject2  
NestedObject3 vs NestedObject4

Sorting using nested sort compares  
NestedObject1 vs NestedObject3 (one NestedObject from OuterObject1 and one from OuterObject2)

Questions

1. Am I misunderstanding Elasticsearch sorting?
2. Is there a way to convert inner\_hits to normal hits? I.e. so if I match 2 NestedObjects then I get 2 separate hits. This would avoid me programmatically 'pulling out' hits from inner\_hits.
3. How can I sort NestedObject1 vs NestedObject2 vs NestedObject3 vs NestedObject4

Thanks all in advance!

---

<div class="post-metadata">

**Author:** ![jpountz](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/jpountz/32/45836_2.png) [@jpountz](https://discuss.elastic.co/u/jpountz)\
**Post date:** [October 20, 2015, 10:43pm UTC](https://discuss.elastic.co/t/nested-objects-hits-inner-hits-and-sorting/32565/2 "2015-10-20T22:43:53Z")

</div>

Elasticsearch doesn't support sorting nested documents. All that you can do is sorting root documents based on the values of nested documents.

Given the description of your issue, it looks like you should rather use denormalization than nested documents?

---

<div class="post-metadata">

**Author:** ![seagullmouse](https://avatars.discourse-cdn.com/v4/letter/s/58956e/32.png) [@seagullmouse](https://discuss.elastic.co/u/seagullmouse)\
**Post date:** [October 21, 2015, 9:05am UTC](https://discuss.elastic.co/t/nested-objects-hits-inner-hits-and-sorting/32565/3 "2015-10-21T09:05:34Z")

</div>

Thanks. Ok if there isn't a workaround in Elasticsearch itself then we will have a look at denormalising. Frustrating as it feels like the functionality is almost there.

Thank you

---

<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 5, 2017, 11:43pm UTC](https://discuss.elastic.co/t/nested-objects-hits-inner-hits-and-sorting/32565/4 "2017-07-05T23:43:38Z")

</div>


