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代码:

[php]  view plain  copy
 
  1. $filter[] = array('geo_bounding_box'=>array('poi'=>array(  
  2.                 'top_right'=>array('lat'=>$this->params['rightTop'][1],'lon'=>$this->params['rightTop'][0]),  
  3.                 'bottom_left'=>array('lat'=>$this->params['leftBottom'][1],'lon'=>$this->params['leftBottom'][0]),  
  4.             )));  
  5.         $request['body']['query']['filtered']['filter']['and'] = $filter;  

      

  b、以一个点为中心,查找范围

[php]  view plain  copy
 
  1. $request['body']['query']['filtered']['filter']['and'][] = array(  
  2.                     'geo_distance_range' => array(  
  3.                         'from' => '0km',  
  4.                         'to'   => '80km',  
  5.                         'poi'  => array('lon' => $longitude, 'lat' => $latitude)  
  6.                     ),  
  7.                 );  

c、按距离排序

"sort": {
            "_geo_distance": {
                "poi": {
                    "lon": "113.25909555556",
                    "lat": "23.131899722222"
                },
                "order": "asc",
                "unit": "km"
            }
        }

参考官方资料:https://www.elastic.co/blog/geo-location-and-search

猜你喜欢

转载自www.cnblogs.com/a-du/p/9212891.html
今日推荐