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 !