# \[HELP\] Access other value doc into aggregates scripts

**URL:** <https://discuss.elastic.co/t/help-access-other-value-doc-into-aggregates-scripts/211082>\
**Category:** Elasticsearch\
**Tags:** painless\
**Created:** [December 9, 2019, 9:38am UTC](https://discuss.elastic.co/t/help-access-other-value-doc-into-aggregates-scripts/211082 "2019-12-09T09:38:48Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![natinho68](https://avatars.discourse-cdn.com/v4/letter/n/edb3f5/32.png) [@natinho68](https://discuss.elastic.co/u/natinho68)\
**Post date:** [December 9, 2019, 9:38am UTC](https://discuss.elastic.co/t/help-access-other-value-doc-into-aggregates-scripts/211082/1 "2019-12-09T09:38:49Z")

</div>

Hi !  
I try to do a script into an aggregates with els 7.

I have an index like this :  
{  
"doc\_address": {  
"aliases": {},  
"mappings": {  
"properties": {  
"address": {  
"properties": {  
"checked": {  
"type": "boolean"  
},  
"city": {  
"type": "keyword"  
},  
"country": {  
"type": "keyword"  
}  
"line1": {  
"type": "keyword"  
},  
"line2": {  
"type": "keyword"  
},  
"zip": {  
"type": "keyword"  
}  
}  
}  
}  
}  
}

I want to split zip codes to get French départements number. There are specificities with Corsica and French overseas départements.

I do it like this with :  
{  
"aggs":{  
"country":{  
"terms":{  
"field":"address.country",  
"size":10  
},  
"aggs":{  
"departement-by-country":{  
"terms":{  
"field":"address.zip",  
"script":{  
"source":"if(\_value != 'null' && \_value.length() \> 2 && \_value.length() \< 5 && \_value =~ /^[0-9]+$/) { if (\_value.startsWith(params.dom\_tom)) { \_value.substring(0, 3);} else if ( Integer.parseInt(\_value) \>= params.min\_zip\_range\_south\_corsica && Integer.parseInt(\_value) \<= params.max\_zip\_range\_south\_corsica) { params.south\_corsica; } else if (Integer.parseInt(\_value) \>= params.min\_zip\_range\_north\_corsica && Integer.parseInt(\_value) \<= params.max\_zip\_range\_north\_corsica) { params.north\_corsica; } else { \_value.substring(0, 2); }} else { \_value; }",  
"lang":"painless",  
"params":{  
"dom\_tom":"97",  
"south\_corsica":"2A",  
"north\_corsica":"2B",  
"min\_zip\_range\_south\_corsica":20000,  
"max\_zip\_range\_south\_corsica":20190,  
"min\_zip\_range\_north\_corsica":20200,  
"max\_zip\_range\_north\_corsica":20620  
}  
},  
"size":100  
}  
}  
}  
}  
}  
}

It works well, return :  
{  
"aggregations": {  
"country": {  
"doc\_count\_error\_upper\_bound": 0,  
"sum\_other\_doc\_count": 0,  
"buckets": [  
{  
"key": "FR",  
"doc\_count": 100,  
"departement-by-country": {  
"doc\_count\_error\_upper\_bound": 0,  
"sum\_other\_doc\_count": 0,  
"buckets": [  
{  
"key": "59",  
"doc\_count": 54  
},  
{  
"key": "75",  
"doc\_count": 46  
}  
]  
}  
}  
]  
}  
}  
}

But I don't know how to apply this script only for French country. In my documents I have multiple countries and with that script nested into 2 terms, it apply it for all my countries (IT, USA, UK...).  
When I want to access to `doc['address']['country']` or `doc['country']`  
I have an error ` Variable [doc] is not defined.`

I know that my script is not necessarily the most optimized and I am listening to all possible advice (I am not an expert with elasticsearch).

Thanks !

---

<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:** [January 6, 2020, 9:40am UTC](https://discuss.elastic.co/t/help-access-other-value-doc-into-aggregates-scripts/211082/2 "2020-01-06T09:40:11Z")

</div>

Closed on January 6, 2020, 9:40am UTC

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