地图搜索实现:
①参数:
左下角经纬度和右上角经纬度
图层数(zoom)
关键字等各种数据库中的字段
排序方式
具体的坐标点+距离
②实现
a.用es作为关系库,首先先mapping所有的字段,然后用laravel命令把数据库中的数据导入到es中
b.收集前台参数
1.在矩形中查找:
$es_geo['geo_bounding_box'] = array( 'location' => array( 'top_right' => array( 'lat' => $top_right_lat, 'lon' => $top_right_lon, ), 'bottom_left' => array( 'lat' => $bottom_left_lat, 'lon' => $bottom_left_lon, ) ), ); $params['body']['query']['bool']['filter'][] = $es_geo;
2.在圆形中查找
$es_geo['geo_distance'] = array( 'distance' => '1000m', 'location' => array( 'lat' => $lat, 'lon' => $lng ), ); $params['body']['query']['bool']['filter'][] = $es_geo;
c.缩放时的聚合
$params['body']['aggs']['area_id'] = array( 'terms' => array( 'field' => 'area_id', 'size' => 100, ) );
d.实例化es和查询
$db_parameters = array( 'DB_DEBUG' => FALSE, 'ES_PARAM' => array('hosts' => array($es_host)), 'ES_INDEX' => $es_index, 'ES_TYPE' => $es_type, ); $client = ClientBuilder::create()->setHosts($db_parameters['ES_PARAM']['hosts'])->build(); $ret = $client->search($params);
e.缓存条件和结果到redis中
$md5_key = ENCODE($params);//加密参数 $redis->set($md5_key, json_encode($result), 1800);
③其他
更新es索引节点: curl -XPUT http://localhost:9200/es_index/_mapping/es_type -d' { "properties": { "xx": { "type": "integer" }, "xx": { "type": "nested", 'properties' : { 'xx' : { "type": "integer" } } } } }'