# Function\_score iterate through nested objects

**URL:** <https://discuss.elastic.co/t/function-score-iterate-through-nested-objects/19418>\
**Category:** Elasticsearch\
**Created:** [August 23, 2014, 10:57am UTC](https://discuss.elastic.co/t/function-score-iterate-through-nested-objects/19418 "2014-08-23T10:57:05Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![kisi](https://avatars.discourse-cdn.com/v4/letter/k/ecd19e/32.png) [@kisi](https://discuss.elastic.co/u/kisi)\
**Post date:** [August 23, 2014, 10:57am UTC](https://discuss.elastic.co/t/function-score-iterate-through-nested-objects/19418/1 "2014-08-23T10:57:05Z")

</div>

Hi,

i have products in my index and i would like to sort them by the lowest  
price of a client. I think i have to use a function\_score for that?

//Product Mapping  
{  
ArticleNumber: 1000,  
Title: "sample product",  
Clients: [  
{  
Type: "K",  
Price: 10,  
ClientIds: [  
1,  
2  
]  
},  
{  
Type: "K",  
Price: 9,  
ClientIds: [  
3  
]  
}  
],  
}

Currently my query looks like this. But i don't know how i can iterate over  
the nested "Clients" Objects and get the lowest price for the Client with  
the Id 1.

query: {  
function\_score: {  
boost\_mode: "replace",  
query: {  
match\_all: { }  
},  
functions: [  
{  
script\_score: {  
script: "miss script to iterate over the Clients",  
lang: "groovy"  
}  
}  
]  
}  
}

Any help would be 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/8f42c4e6-7706-4d8d-9105-ac1fda1ed43c%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/8f42c4e6-7706-4d8d-9105-ac1fda1ed43c%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:** [August 26, 2014, 2:47pm UTC](https://discuss.elastic.co/t/function-score-iterate-through-nested-objects/19418/2 "2014-08-26T14:47:44Z")

</div>

Hello Kisi ,

You can use score\_mode as min on that field -

> **[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.

But i guess for that to work , you need to enable  
"include\_in\_parent": true,

In nested type -

> **[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.

Thanks  
Vineeth

On Sat, Aug 23, 2014 at 4:27 PM, kisi [koglchri@gmail.com](mailto:koglchri@gmail.com) wrote:

> Hi,
> 
> i have products in my index and i would like to sort them by the lowest  
> price of a client. I think i have to use a function\_score for that?
> 
> //Product Mapping  
> {  
> ArticleNumber: 1000,  
> Title: "sample product",  
> Clients: [  
> {  
> Type: "K",  
> Price: 10,  
> ClientIds: [  
> 1,  
> 2  
> ]  
> },  
> {  
> Type: "K",  
> Price: 9,  
> ClientIds: [  
> 3  
> ]  
> }  
> ],  
> }
> 
> Currently my query looks like this. But i don't know how i can iterate  
> over the nested "Clients" Objects and get the lowest price for the Client  
> with the Id 1.
> 
> query: {  
> function\_score: {  
> boost\_mode: "replace",  
> query: {  
> match\_all: { }  
> },  
> functions: [  
> {  
> script\_score: {  
> script: "miss script to iterate over the Clients",  
> lang: "groovy"  
> }  
> }  
> ]  
> }  
> }
> 
> Any help would be 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/8f42c4e6-7706-4d8d-9105-ac1fda1ed43c%40googlegroups.com](https://groups.google.com/d/msgid/elasticsearch/8f42c4e6-7706-4d8d-9105-ac1fda1ed43c%40googlegroups.com)  
> [https://groups.google.com/d/msgid/elasticsearch/8f42c4e6-7706-4d8d-9105-ac1fda1ed43c%40googlegroups.com?utm\_medium=email&utm\_source=footer](https://groups.google.com/d/msgid/elasticsearch/8f42c4e6-7706-4d8d-9105-ac1fda1ed43c%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/CAGdPd5mZgEt3tJBxQLkJK\_5tp\_tp6vHZhQWY9WbbgY0FZFWvvA%40mail.gmail.com](https://groups.google.com/d/msgid/elasticsearch/CAGdPd5mZgEt3tJBxQLkJK_5tp_tp6vHZhQWY9WbbgY0FZFWvvA%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:06am UTC](https://discuss.elastic.co/t/function-score-iterate-through-nested-objects/19418/3 "2017-07-06T01:06:19Z")

</div>


