# Nested Query score\_mode min?

**URL:** <https://discuss.elastic.co/t/nested-query-score-mode-min/8613>\
**Category:** Elasticsearch\
**Created:** [August 2, 2012, 6:15pm UTC](https://discuss.elastic.co/t/nested-query-score-mode-min/8613 "2012-08-02T18:15:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![polyfractal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/polyfractal/32/48162_2.png) [@polyfractal](https://discuss.elastic.co/u/polyfractal)\
**Post date:** [August 2, 2012, 6:15pm UTC](https://discuss.elastic.co/t/nested-query-score-mode-min/8613/1 "2012-08-02T18:15:11Z")

</div>

It appears "min" is not a viable option for the score\_mode on nested  
queries. Would it be possible to add this? Or another way to "fake" the  
same result?

I have a nested document structure, where a single "Product" can contain  
multiple "Vendors" who sell the product. Each vendor has a "productCost"  
property which is the price of the product.

I'm attempting to sort these by price in Ascending order. To accomplish  
this with the nested documents, I'm applying a custom\_score to each  
"Vendor" nested document, where the score == price. I then sort the entire  
set by the \_score ascending.

This works mostly ok. Search results are sorted by price ascending.  
However, since the score\_mode on nested cannot do "min", products with  
multiple vendors can be mis-sorted ($10 vendor and $100 vendor will cause  
the parent doc to sort incorrectly, since both Avg and Max return too high  
of a value)

I realize I can denormalize my fields and make the parent doc contain a  
"lowestPrice" field which is kept current. If there is no other way to  
perform this query I'll use this approach.

Thanks,  
-Zach

---

<div class="post-metadata">

**Author:** ![Igor\_Motov](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/igor_motov/32/45193_2.png) [@Igor\_Motov](https://discuss.elastic.co/u/Igor_Motov)\
**Post date:** [August 2, 2012, 9:17pm UTC](https://discuss.elastic.co/t/nested-query-score-mode-min/8613/2 "2012-08-02T21:17:54Z")

</div>

The simplest solution that comes to mind is to use custom\_score with score  
== -price.

On Thursday, August 2, 2012 2:15:11 PM UTC-4, Zachary Tong wrote:

> It appears "min" is not a viable option for the score\_mode on nested  
> queries. Would it be possible to add this? Or another way to "fake" the  
> same result?
> 
> I have a nested document structure, where a single "Product" can contain  
> multiple "Vendors" who sell the product. Each vendor has a "productCost"  
> property which is the price of the product.
> 
> I'm attempting to sort these by price in Ascending order. To accomplish  
> this with the nested documents, I'm applying a custom\_score to each  
> "Vendor" nested document, where the score == price. I then sort the entire  
> set by the \_score ascending.
> 
> This works mostly ok. Search results are sorted by price ascending.  
> However, since the score\_mode on nested cannot do "min", products with  
> multiple vendors can be mis-sorted ($10 vendor and $100 vendor will cause  
> the parent doc to sort incorrectly, since both Avg and Max return too high  
> of a value)
> 
> I realize I can denormalize my fields and make the parent doc contain a  
> "lowestPrice" field which is kept current. If there is no other way to  
> perform this query I'll use this approach.
> 
> Thanks,  
> -Zach

---

<div class="post-metadata">

**Author:** ![polyfractal](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/polyfractal/32/48162_2.png) [@polyfractal](https://discuss.elastic.co/u/polyfractal)\
**Post date:** [August 3, 2012, 1:50am UTC](https://discuss.elastic.co/t/nested-query-score-mode-min/8613/3 "2012-08-03T01:50:14Z")

</div>

Doh, what a simple solution. Thanks, worked great!

-Zach

On Thursday, August 2, 2012 5:17:54 PM UTC-4, Igor Motov wrote:

> The simplest solution that comes to mind is to use custom\_score with score  
> == -price.
> 
> On Thursday, August 2, 2012 2:15:11 PM UTC-4, Zachary Tong wrote:
> 
> > It appears "min" is not a viable option for the score\_mode on nested  
> > queries. Would it be possible to add this? Or another way to "fake" the  
> > same result?
> > 
> > I have a nested document structure, where a single "Product" can contain  
> > multiple "Vendors" who sell the product. Each vendor has a "productCost"  
> > property which is the price of the product.
> > 
> > I'm attempting to sort these by price in Ascending order. To accomplish  
> > this with the nested documents, I'm applying a custom\_score to each  
> > "Vendor" nested document, where the score == price. I then sort the entire  
> > set by the \_score ascending.
> > 
> > This works mostly ok. Search results are sorted by price ascending.  
> > However, since the score\_mode on nested cannot do "min", products with  
> > multiple vendors can be mis-sorted ($10 vendor and $100 vendor will cause  
> > the parent doc to sort incorrectly, since both Avg and Max return too high  
> > of a value)
> > 
> > I realize I can denormalize my fields and make the parent doc contain a  
> > "lowestPrice" field which is kept current. If there is no other way to  
> > perform this query I'll use this approach.
> > 
> > Thanks,  
> > -Zach

---

<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, 3:17am UTC](https://discuss.elastic.co/t/nested-query-score-mode-min/8613/4 "2017-07-06T03:17:55Z")

</div>


