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:
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!
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. -
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.
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:
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!
Apache, Apache Lucene, Apache Hadoop, Hadoop, HDFS and the yellow elephant
logo are trademarks of the
Apache Software Foundation
in the United States and/or other countries.