# Sorting on nested object collections

**URL:** <https://discuss.elastic.co/t/sorting-on-nested-object-collections/21654>\
**Category:** Elasticsearch\
**Created:** [January 15, 2015, 3:58am UTC](https://discuss.elastic.co/t/sorting-on-nested-object-collections/21654 "2015-01-15T03:58:09Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Russ\_Cam](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/russ_cam/32/895_2.png) [@Russ\_Cam](https://discuss.elastic.co/u/Russ_Cam)\
**Post date:** [January 15, 2015, 3:58am UTC](https://discuss.elastic.co/t/sorting-on-nested-object-collections/21654/1 "2015-01-15T03:58:09Z")

</div>

I have a mapping with the following (omitted other properties for brevity):

{  
"index-name" : {  
"mappings" : {  
"index-type" : {  
"properties" : {  
"history" : {  
"properties" : {  
"valuations" : {  
"type" : "nested",  
"include\_in\_root" : true,  
"properties" : {  
"confidence" : {  
"type" : "integer"  
},  
"date" : {  
"type" : "date",  
"format" : "dateOptionalTime"  
},  
"lowerPrice" : {  
"type" : "integer"  
},  
"precision" : {  
"type" : "integer"  
},  
"upperPrice" : {  
"type" : "integer"  
}  
}  
}  
}  
},  
"id" : {  
"type" : "integer"  
}  
}  
}  
}  
}  
}

the valuations nested object mapping is a collection of such objects.

I would like to sort documents according to the lowerPrice ascending for  
the max date valuation in each document i.e. for each document, get the  
latest valuation in the history.valuations collection according to max date,  
then project the lowerPrice value from the valuation and order by this  
value ascending.

What I have at the moment is:

{  
"from": 0,  
"size": 20,  
"sort": [{  
"history.valuations.date": {  
"order": "desc",  
"mode": "max"  
}  
}, {  
"history.valuations.lowerPrice": {  
"order": "asc"  
}  
}]  
}

But I am not 100% certain that this does what I expect it to. Looking at  
the results when I run this query, it \*looks \*like it is working, but any  
feedback with someone better versed with more complex sorting would be  
greatly appreciated.

--  
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/e1ac4169-a66e-44ee-8dc6-f8e2f1669bad%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/e1ac4169-a66e-44ee-8dc6-f8e2f1669bad%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![Russ\_Cam](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/russ_cam/32/895_2.png) [@Russ\_Cam](https://discuss.elastic.co/u/Russ_Cam)\
**Post date:** [January 16, 2015, 2:56am UTC](https://discuss.elastic.co/t/sorting-on-nested-object-collections/21654/2 "2015-01-16T02:56:07Z")

</div>

I've run the query with the smallest possible subset and the query is  
returning the results in the expected order so it appears to be correct.

The biggest question that I have is does the second sort condition know to  
run on the _first_ projected valuation that had the max date from the first  
sort condition?

--  
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/420cb05f-38f0-4ef9-a922-96e26f5ab5e1%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/420cb05f-38f0-4ef9-a922-96e26f5ab5e1%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, 12:38am UTC](https://discuss.elastic.co/t/sorting-on-nested-object-collections/21654/3 "2017-07-06T00:38:31Z")

</div>


