基于Elasticsearch的地理位置简单搜索

栏目: 后端 · 发布时间: 7年前

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

汇总一些简单用法

基于Elasticsearch的地理位置简单搜索

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

相关文档学习

包的使用:

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

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

地理位置的查询:

http://cwiki.apachecn.org/pag...

经纬度查询实例:

https://cloud.tencent.com/inf...

创建索引

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": {
                    
                }    
            },
            "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: 邓尘锋


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Java Servlet & JSP Cookbook

Java Servlet & JSP Cookbook

Bruce W. Perry / O'Reilly Media / 2003-12-1 / USD 49.99

With literally hundreds of examples and thousands of lines of code, the Java Servlet and JSP Cookbook yields tips and techniques that any Java web developer who uses JavaServer Pages or servlets will ......一起来看看 《Java Servlet & JSP Cookbook》 这本书的介绍吧!

RGB转16进制工具
RGB转16进制工具

RGB HEX 互转工具

随机密码生成器
随机密码生成器

多种字符组合密码

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具