Sorting a nested array


(Darin Amos) #1

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.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/500b16ff-b00f-44a9-a238-9c7a09432bfa%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


(vineeth mohan-2) #2

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. -
http://www.elasticsearch.org/guide/en/elasticsearch/reference/current/query-dsl-function-score-query.html#_using_function_score

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
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.
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?utm_medium=email&utm_source=footer
.
For more options, visit 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.
To view this discussion on the web visit https://groups.google.com/d/msgid/elasticsearch/CAGdPd5%3DxMYAQfaZAzx7JyuHvAuBb4%3DZ9XBPyMkp_1G9uWEOdvA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


(system) #3