# Nested aggregation

**URL:** <https://discuss.elastic.co/t/nested-aggregation/326949>\
**Category:** Elasticsearch\
**Tags:** painless, language-clients\
**Created:** [March 3, 2023, 2:52pm UTC](https://discuss.elastic.co/t/nested-aggregation/326949 "2023-03-03T14:52:44Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Sneha\_Rose](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/sneha_rose/32/118018_2.png) [@Sneha\_Rose](https://discuss.elastic.co/u/Sneha_Rose)\
**Post date:** [March 3, 2023, 2:52pm UTC](https://discuss.elastic.co/t/nested-aggregation/326949/1 "2023-03-03T14:52:44Z")

</div>

I have an elastic index with authors field as array of objects:  
"authors" : [  
{  
"email" : "[100@gmail.com](mailto:100@gmail.com)",  
"authid" : "100",  
},  
{  
"email" : "[200@gmail.com](mailto:200@gmail.com)",  
"authid" : "200",  
}, {  
"email" : "[300@gmail.com](mailto:300@gmail.com)",  
"authid" : "300",  
},  
]

And am calculating sum of relevance score of each author as follows:  
"aggregations": {  
"authid": {  
"terms": {  
"field": "authors.authid",  
"order": [  
{  
"relevance": "desc"  
},  
{  
"\_key": "asc"  
}  
]  
},  
"aggregations": {  
"relevance": {  
"sum": {  
"script": {  
"source":"\_score"  
"lang": "painless"  
}  
}  
}  
}  
}  
}  
Here, I need to boost score of authors whose id comes as the first or last in authors array

I tried as below:   
"aggregations": {  
"relevance": {  
"sum": {  
"script": {  
"source": "params.\_source.authors[0].authid == params.author\_id || params.\_source.authors[-1].authid == params.author\_id ? \_score \* params.boost\_factor : \_score",  
"params": {  
"author\_id": "{{key}}",  
"boost\_factor": 2  
},  
"lang": "painless"  
}  
}  
}  
}

Here I tried to get outer bucket key using params.key.But its not returnig its original value

---

<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:** [March 31, 2023, 2:52pm UTC](https://discuss.elastic.co/t/nested-aggregation/326949/2 "2023-03-31T14:52:53Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
