Reputation: 795
I am trying to implement a postcode geo search with elasticsearch.
Generally it is kind of working, but if I do a search for a postcode that does have a listing, in SOME situations that listing does not display at all. But other listings outside of the postcode (within the range) do show up. I'd be expecting the listing with the same postcode to appear at the top of the others - but it ain't showing at all.
This is the PHP code that I have.
I am a complete novice when it comes to elasticsearch, and am just working with existing elasticsearch code that was setup by a prior developer.
Thanks
$es = new Elasticsearch\Client();
include('helpers/geoHelper.php');
$geo = new geoHelper();
$geoData = $geo->getLatLngFromAddress($postcode.',+AU');
$params['body']['sort']['_geo_distance']['location']['lat']= $geoData->lat;
$params['body']['sort']['_geo_distance']['location']['lon']= $geoData->lng;
$params['body']['sort']['_geo_distance']['order'] = 'asc';
$params['body']['sort']['_geo_distance']['unit'] = 'km';
$params['body']['sort']['_geo_distance']['distance_type'] = 'plane';
$params['body']['fields'] = '_source';
$params['body']['script_fields']['distance']['lang'] = 'groovy';
$params['body']['script_fields']['distance']['params'] = array(
'lat' => $geoData->lat,
'lon' => $geoData->lng
);
$params['body']['script_fields']['distance']['script'] = "doc['location'].distanceInKm(lat,lon)";
$filter_distance = array(
'distance' => '200km',
'location' => array(
'lat' => $geoData->lat,
'lon' => $geoData->lng,
)
);
$params['body']['query']['filtered']['filter']['geo_distance'] = $filter_distance;
$params['body']['size']=25
$params['body']['from']=1
$params['index'] = 'shops';
$params['type'] = 'shop';
$data = new stdClass();
$data->results = $es->search($params);
Upvotes: 0
Views: 587
Reputation: 254
I have also faced same issue with this library and try to find out help on internet. but now i have solved this problem using Query DSL instead of using array format, I just create valid JSON query and pass into you body parameter. For example
Array
(
[index] => spnew
[type] => sprovider_new
[body] => {"from":0,"size":15,"sort":[{"_geo_distance":{"geo_location":{"lat":18.9295932,"lon":72.8327296},"order":"asc","unit":"km"}},{"likes_count":{"order":"desc"}},{"reviews.created_time":{"order":"desc"}}],"query":{"bool":{"must":[{"term":{"status":"1"}},{"term":{"category.category_id":"103"}},{"term":{"parent_service_id":"0"}},{"term":{"city_id":"1"}}]}}}
)
This may help you. you can also look this: http://www.elasticsearch.org/guide/en/elasticsearch/guide/current/decay-functions.html
Upvotes: 1