查询弹性搜索地理距离邮政编码搜索

时间:2015-02-15 23:50:03

标签: php elasticsearch

我正在尝试使用elasticsearch实现邮政编码地理搜索 通常它有点工作,但是如果我搜索一个有列表的邮政编码,在某些情况下列表根本不会显示。但是,邮政编码之外的其他列表(在范围内)确实会显示出来。我希望使用相同邮政编码的商家信息显示在其他邮箱的顶部 - 但它根本没有显示。

这是我的PHP代码。

对于elasticsearch而言,我是一个完整的新手,我只是使用由先前的开发人员设置的现有弹性搜索代码。

由于

$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);

1 个答案:

答案 0 :(得分:1)

我也遇到过这个库的同样问题,并尝试在互联网上找到帮助。 但现在我使用Query DSL而不是使用数组格式解决了这个问题,我只是创建有效的JSON查询并传递给你的body参数。例如

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"}}]}}}
)

这可能会对你有所帮助。你也可以这样看:http://www.elasticsearch.org/guide/en/elasticsearch/guide/current/decay-functions.html