elasticsearch geo_point 地理位置過濾 按經度排序

elasticsearch 支持強大的經緯度座標過濾。php

一、首先要創建座標類型的字段'type' ='geo_point'git

     es存儲的值是這樣的:elasticsearch

"poi": [
          113.40780444444,
          22.920241666667
      ],this

二、構建各類經緯度過濾條件blog

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
                        }
                    }
                  }
                }
            }
        }
    }it

php代碼:io

$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、以一個點爲中心,查找範圍ast


$request['body']['query']['filtered']['filter']['and'][] = array(
                    'geo_distance_range' => array(
                        'from' => '0km',
                        'to'   => '80km',
                        'poi'  => array('lon' => $longitude, 'lat' => $latitude)
                    ),
                );request

c、按距離排序

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

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

相關文章
相關標籤/搜索