$termsAgg2 = new Elastica\Aggregation\Terms("location_radius");
$termsAgg2->setField("location_name");
$termsAgg2->setSize(10);
$termsAgg2->setScript(new Elastica\Script\Script("ceil(doc['location'].arcDistanceInKm(51.2249429, 6.7756524))"));
$elasticaQuery->addAggregation($termsAgg2);
My earlier implementation with Facet was in this way:
$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"
);
Someone can help me?
If I try this script:
$termsAgg2->setScript(new Elastica\Script\Script("ceil(doc['location'].arcDistanceInKm(51.2249429, 6.7756524))+ '|' + doc['location_name'].value"));
Error in elasticsearch logs is:
Caused by: ScriptException[failed to run inline script [ceil(doc['location'].arcDistanceInKm(51.2249429, 6.7756524)) + '|' + doc['location_name'].value] using lang [groovy]]; nested: IllegalArgumentException[Negative position];
Thanks
Nik
'index' => 'not_analyzed'
This has cost me 1 day testing :D
2. To use this aggregations you need to remove the filed for the aggregation (0,5 days of testing), because with the new key you are generating a new one for the aggregation.
Here my solution:
$termsAgg2 = new Elastica\Aggregation\Terms("location_radius");
//$termsAgg2->setField("location_facet"); // -- removed
$termsAgg2->setSize(20);
$termsAgg2->setScript(new Elastica\Script\Script("doc['location'].empty ? null : ceil(doc['location'].arcDistanceInKm(51.2249429, 6.7756524)) + ' - ' + doc['location_facet'].value+ ' - ' + doc['location_id'].value"));
$elasticaQuery->addAggregation($termsAgg2);
In ES query style this looks so:
"aggs": {
"location_radius": {
"terms": {
"size": 100,
"script": "doc['location'].empty ? null : ceil(doc['location'].arcDistanceInKm(51.2249429, 6.7756524)) + ' - ' + doc['location_facet'].value+ ' - ' + doc['location_id'].value"
}
}
}
$agg = new GeoDistance('geo', 'location', array('lat' => 32.804654, 'lon' => -117.242594)); $agg->addRange(null, 100); $agg->setUnit('mi'); This example is delivering only the amount of data for this Distance, but is it possible to become all the cities around 100 mi radius with the amount of data? |