elasticsearch+php 代码实现地理位置的搜索

浏览:1656 发布日期:2019/01/15 分类:功能实现
在app里面可以调用SDK进行相关地理位置的处理,比如搜索距离某个地方500KM范围的数据,或者进行一些地理位置从近到远的排序。
那么web网站呢,可以调用百度地图API进行相关处理达到类似的功能。
这里我采用elasticsearch来实现一个demo
1、搜索指定地点范围内的数据,
2、按某个指定点查找数据,并且进行数据的排序。

1、创建索引

$client = new Client();
$url = 'http://localhost:9200/cn_large_cities/';
$param = [
'json' => [
'mappings' => [
'city'=>[
'properties'=>[
'city'=>['type'=>'text'],
'state'=>['type'=>'text'],
'location'=>['type'=>'geo_point']
]
]
]

]
];

$res = $client->put($url,$param);
exit;

2、创建5条测试数据

$client = new Client();
$url = 'http://localhost:9200/cn_large_cities/city/1';

$param = [
'json' => [
'city' => 'Beijing',
'state'=>'BJ',
'location' => [
'lat'=>'39.91667',
'lon'=>'116.41667'
]
]
];

$res = $client->post($url,$param);
exit;

$client = new Client();
$url = 'http://localhost:9200/cn_large_cities/city/2';

$param = [
'json' => [
'city' => 'Shanghai',
'state'=>'SH',
'location' => [
'lat'=>'34.50000',
'lon'=>'121.43333'
]
]
];

$res = $client->post($url,$param);
exit;

$client = new Client();
$url = 'http://localhost:9200/cn_large_cities/city/3';

$param = [
'json' => [
'city' => 'Xiamen',
'state'=>'FJ',
'location' => [
'lat'=>'24.46667',
'lon'=>'118.10000'
]
]
];
其余代码可以访问我的网站博客,欢迎大家交流
http://www.huangxiaoshan.xyz/article.php?id=76
评论( 相关
后面还有条评论,点击查看>>