基於Elasticsearch的地理位置簡單搜索

彙總一些簡單用法

請輸入圖片描述

由於公司須要使用一個需求, 經過用戶的當前地理位置消息搜索出周邊的一些數據, 若是使用php進行大數據計算的話,很是消耗性能,因此採用es

相關文檔學習

包的使用: php

https://packagist.org/package...html

https://www.cnblogs.com/codeA...git

地理位置的查詢:
http://cwiki.apachecn.org/pag...github

查詢語法:
https://doc.yonyoucloud.com/d...apache

經緯度查詢實例:
https://cloud.tencent.com/inf...json

ES - PHP
https://www.elastic.co/guide/...app

建立索引

PUT http://localhost:9200/showelasticsearch

建立索引字段

PUT http://localhost:9200/show/store/_mappingide

{ 
    "store": { 
            "_all":{ 
            "enabled":false 
            }, 
            "properties": { 
                "id": { 
                    "type": "integer" 
                }, 
                "name": { 
                    "type": "text", 
                    "analyzer": "ik_max_word" 
                }, 
                "type": { 
                    "type": "integer"
                },
                "position": {
                    "properties": {
                        "location": {
                            "type": "geo_point"
                        }
                    }
            }
            } 
        } 
}

建立索引文檔

PUT http://localhost:9200/show/test/2

{ 
    "id" : 1, 
    "name" :  "建升大廈", 
    "type" :  1,
    "position":{
        "location" : {
            "lat" : 22.6482057076,
            "lon" : 114.1250142233
        }
    }
}


PUT http://localhost:9200/show/test/1

{ 
    "id" : 2, 
    "name" :  "深圳市第三人民醫院", 
    "type" :  1,
    "position":{
        "location" : {
            "lat" : 22.6352587415,
            "lon" : 114.1289020619
        }
    }
}

PUT http://localhost:9200/show/test/3

{ 
    "id" : 3, 
    "name" :  "深圳百合醫院", 
    "type" :  1,
    "position":{
        "location" : {
            "lat" : 22.6164455768,
            "lon" : 114.1395956293
        }
    }
}

開始查詢

查詢全部post

POST http://localhost:9200/show/store/_search //

{
    "query": {
        "bool": {
            "must": {
                "match_all": {
                    
                }    
            },
            "filter": {
                "geo_distance": {
                    "distance" : "10km",
                    "position.location": {
                        "lat": 22.6497899384,
                        "lon": 114.1258725301
                    }
                }
            }
        }
    },
    "sort": [ 
    { 
        "_geo_distance": { 
            "position.location": { 
            "lat": 22.6497899384, 
            "lon": 114.1258725301
        }, 
            "order": "asc", 
            "unit": "km", 
            "mode": "min"
        } 
    } 
    ]
}

查詢+分詞

{
    "from":3,
    "size":3,
    "query": {
        "bool": {
            "must": {
                "match": {
                    "name": "深圳"
                }    
            },
            "filter": {
                "geo_distance": {
                    "distance" : "100km",
                    "position.location": {
                        "lat": 22.649928,
                        "lon": 114.125646
                    }
                }
            }
        }
    },
    "sort": [ 
    { 
        "_geo_distance": { 
        "position.location": { 
        "lat": 22.6497899384, 
        "lon": 114.1258725301
    }, 
        "order": "asc", 
        "unit": "km", 
        "mode": "min"
        } 
    } 
    ]
}

以上文檔的下載地址

點我查看下載地址-

from: 鄧塵鋒

相關文章
相關標籤/搜索