I have a field "location_facet", which is a strig with mapping
"location_facet": {
"type": "string",
"index": "not_analyzed",
"include_in_all": true
},
In this field I have location_ids and I would like to aggregate on them by the query. Also I want to become some information by the aggregation, that's why I would like to execute a script, for example I want to become the name of the city, but there is always an error coming:
"aggs": {
"location_radius": {
"terms": {
"field": "location_facet",
"size": 10,
"script": "doc[\"location_name\"].value"
}
}
},
"type": "script_exception",
"reason": "failed to run inline script [doc[\"location_name\"].value] using lang [groovy]"
I have actually this implementation with Facets:
$tagFacet = new \Elastica\Facet\Terms("location_radius");
$tagFacet->setField('location_name');
$tagFacet->setAllTerms(true);
$tagFacet->setSize(100);
$tagFacet->setScript(
"doc['location'].empty ? null : ceil(doc['location'].arcDistanceIn".$unit."(".
$entries->getLocationLatitude().", ".
$entries->getLocationLongitude().")) + '|' + doc['location_name'].value
+ '|' + doc['location_id'].value"
);
What I am doing wrong?
If I test this one:
"aggs": {
"location_radius": {
"terms": {
"field": "location_facet",
"size": 10,
"script": "doc['location'].empty ? null : ceil(doc['location'].arcDistanceInKm(51.2249429, 6.7756524))"
}
}
},
I get the error:
"type": "script_exception",
"reason": "failed to run inline script [doc['location'].empty ? null : ceil(doc['location'].arcDistanceInKm(51.2249429, 6.7756524))] using lang [groovy]",
"caused_by": {
"type": "illegal_argument_exception",
"reason": "Negative position"
}
Solution: All the doc fields I can use need to be also "index": "not_analyzed".