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



我正在尝试使用弹性搜索实现邮政编码地理搜索
一般来说,这是可行的,但如果我搜索一个有列表的邮政编码,在某些情况下,列表根本不会显示。但邮政编码以外(在该范围内)的其他列表确实会出现。我本以为带有相同邮政编码的列表会出现在其他列表的顶部,但它根本没有显示。

这是我的PHP代码。

当涉及到弹性搜索时,我是一个完全的新手,我只是使用由以前的开发人员设置的现有弹性搜索代码。

感谢

$es = new ElasticsearchClient();
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);

我也遇到过这个库的同样问题,并试图在互联网上找到帮助。但现在我已经使用QueryDSL而不是使用数组格式解决了这个问题,我只创建了有效的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

最新更新