# Sorting a nested array

**URL:** <https://discuss.elastic.co/t/sorting-a-nested-array/18779>\
**Category:** Elasticsearch\
**Created:** [July 22, 2014, 2:45am UTC](https://discuss.elastic.co/t/sorting-a-nested-array/18779 "2014-07-22T02:45:39Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Darin\_Amos](https://avatars.discourse-cdn.com/v4/letter/d/db5fbb/32.png) [@Darin\_Amos](https://discuss.elastic.co/u/Darin_Amos)\
**Post date:** [July 22, 2014, 2:45am UTC](https://discuss.elastic.co/t/sorting-a-nested-array/18779/1 "2014-07-22T02:45:39Z")

</div>

Hi Everyone,

I am looking for a little guidance on how to setup my indexes to support  
sorting a nested array. To give a simple example, we have an index of  
products in our index, below is a dummy example document:

/products/product/

{  
"name":"Toaster",  
"desc":"For making toast",  
"price":29.99,  
"reviews":[  
{  
"rating" : 5,  
"comment" : "works great!"  
},  
{  
"rating" : 1,  
"comment" : "Ugh!!"  
},  
{  
"rating" : 3,  
"comment" : "Meh"  
}  
]  
}

I would like to be able to query for product documents, but be able to sort  
the reviews array based on the rating. A customer could ask to see the  
first 10 best reviews, or the worst 5 reviews for example. Is this  
possible? Is this possible if the reviews are in a separate index and I  
execute a join query?

Any information or guidance would be extremely helpful!

Thanks!

Darin

--  
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/500b16ff-b00f-44a9-a238-9c7a09432bfa%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/500b16ff-b00f-44a9-a238-9c7a09432bfa%40googlegroups.com).  
For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

---

<div class="post-metadata">

**Author:** ![vineeth\_mohan\_2](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/vineeth_mohan_2/32/747_2.png) [@vineeth\_mohan\_2](https://discuss.elastic.co/u/vineeth_mohan_2)\
**Post date:** [July 22, 2014, 3:11am UTC](https://discuss.elastic.co/t/sorting-a-nested-array/18779/2 "2014-07-22T03:11:07Z")

</div>

Hello Darin ,

This is the wrong approach to do this.  
You can only sort per document and not per nested document and recieve  
output as documents and not as nested documents.  
So i would rather go for parent child approach.

That being said , what you asked is actually possible with little tweaking  
on client side. -

> **[Elasticsearch Platform — Find real-time answers at scale](https://www.elastic.co)**
>
> Power insights and outcomes with the Elasticsearch Platform and AI. See into your data and find answers that matter with enterprise solutions designed to help you build, observe, and protect. Try Elasticsearch free today.

So a function score query can has a score\_mode of min or max.  
This means that if we give the field reward as the score value and  
score\_mode , it will take the highest value per doc as the score.  
With this , you can get the top 10 documents bearing top 10 rating.  
Once you receive this you need to take out all ratings and do a sort again  
and then find the top 10 ratings.  
Possibility is that a single document might contain top 2 good reviews.

With this i believe you might need to turn include\_in\_parent on.

Thanks  
Vineeth

On Tue, Jul 22, 2014 at 8:15 AM, Darin Amos [darinamos.it.inc@gmail.com](mailto:darinamos.it.inc@gmail.com)  
wrote:

> Hi Everyone,
> 
> I am looking for a little guidance on how to setup my indexes to support  
> sorting a nested array. To give a simple example, we have an index of  
> products in our index, below is a dummy example document:
> 
> /products/product/
> 
> {  
> "name":"Toaster",  
> "desc":"For making toast",  
> "price":29.99,  
> "reviews":[  
> {  
> "rating" : 5,  
> "comment" : "works great!"  
> },  
> {  
> "rating" : 1,  
> "comment" : "Ugh!!"  
> },  
> {  
> "rating" : 3,  
> "comment" : "Meh"  
> }  
> ]  
> }
> 
> I would like to be able to query for product documents, but be able to  
> sort the reviews array based on the rating. A customer could ask to see the  
> first 10 best reviews, or the worst 5 reviews for example. Is this  
> possible? Is this possible if the reviews are in a separate index and I  
> execute a join query?
> 
> Any information or guidance would be extremely helpful!
> 
> Thanks!
> 
> Darin
> 
> --  
> 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/500b16ff-b00f-44a9-a238-9c7a09432bfa%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/500b16ff-b00f-44a9-a238-9c7a09432bfa%40googlegroups.com)  
> [https://groups.google.com/d/msgid/elasticsearch/500b16ff-b00f-44a9-a238-9c7a09432bfa%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/500b16ff-b00f-44a9-a238-9c7a09432bfa%40googlegroups.com?utm_medium=email&utm_source=footer)  
> .  
> For more options, visit [https://groups.google.com/d/optout](https://groups.google.com/d/optout).

--  
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/CAGdPd5%3DxMYAQfaZAzx7JyuHvAuBb4%3DZ9XBPyMkp\_1G9uWEOdvA%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAGdPd5%3DxMYAQfaZAzx7JyuHvAuBb4%3DZ9XBPyMkp_1G9uWEOdvA%40mail.gmail.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, 1:14am UTC](https://discuss.elastic.co/t/sorting-a-nested-array/18779/3 "2017-07-06T01:14:22Z")

</div>


