基于 elsticsearch 的地理位置简单搜索使用

汇总一些简单用法

请输入图片描述

因为公司需要使用一个需求, 通过用户的当前地理位置消息搜索出周边的一些数据, 如果使用php进行大数据计算的话,非常消耗性能,所以采用es

相关文档学习

包的使用:

https://packagist.org/packages/elasticsear...

https://www.cnblogs.com/codeAB/p/10283304....

地理位置的查询:
http://cwiki.apachecn.org/pages/viewpage.a...

查询语法:
https://doc.yonyoucloud.com/doc/mastering-...

经纬度查询实例:
https://cloud.tencent.com/info/4640585893c...

ES - PHP
https://www.elastic.co/guide/cn/elasticsea...

创建索引

PUT http://localhost:9200/show

创建索引字段

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

{ 
    "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 http://localhost:9200/show/store/_search //

{
    "query": {
        "bool": {
            "must": {
                "match_all": {} // 搜索所有的时候采用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"
        } 
    } 
    ]
}

以上文档的下载地址

点我查看postman json地址-

from: surest.cn

本作品采用《CC 协议》,转载必须注明作者和本文链接
每天3小时...加油
《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
《G01 Go 实战入门》
从零开始带你一步步开发一个 Go 博客项目,让你在最短的时间内学会使用 Go 进行编码。项目结构很大程度上参考了 Laravel。
讨论数量: 1
Epona

我觉得用高德等第三方地图API会更方便和易于维护 :smile:

4年前 评论

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!