elasticsearch geo_point 地理位置过滤 按经度排序
elasticsearch 支持强大的经纬度坐标过滤。
1、首先要建立坐标类型的字段'type' ='geo_point'
es存储的值是这样的:
"poi": [
113.40780444444,
22.920241666667
],
2、构建各种经纬度过滤条件
a、获取屏幕范围内,只需屏幕的两个对角的坐标。
{
"from": 0,
"size": 20,
"query": {
"filtered": {
"query": {
"match_all": []
},
"filter": {
"geo_bounding_box" : {
"poi" : {
"top_right" : {
"lat" : 23.172558,
"lon" : 113.370667
},
"bottom_left" : {
"lat" : 23.14997,
"lon" : 113.313425
}
}
}
}
}
}
}
php代码:
- $filter[] = array('geo_bounding_box'=>array('poi'=>array(
- 'top_right'=>array('lat'=>$this->params['rightTop'][1],'lon'=>$this->params['rightTop'][0]),
- 'bottom_left'=>array('lat'=>$this->params['leftBottom'][1],'lon'=>$this->params['leftBottom'][0]),
- )));
- $request['body']['query']['filtered']['filter']['and'] = $filter;
b、以一个点为中心,查找范围
- $request['body']['query']['filtered']['filter']['and'][] = array(
- 'geo_distance_range' => array(
- 'from' => '0km',
- 'to' => '80km',
- 'poi' => array('lon' => $longitude, 'lat' => $latitude)
- ),
- );
c、按距离排序
"sort": {
"_geo_distance": {
"poi": {
"lon": "113.25909555556",
"lat": "23.131899722222"
},
"order": "asc",
"unit": "km"
}
}
参考官方资料:https://www.elastic.co/blog/geo-location-and-search