zoukankan      html  css  js  c++  java
  • elasticsearch 中geo point地理位置数据类型

    第一个例子

    建立映射

    PUT /my_index
    {
      "mappings": {
        "my_type": {
          "properties": {
            "location" : {
              "type": "geo_point"   //地理位置的分词器是geo_point
            }
          }
        }
      }
    }
    

    填充数据

    PUT my_index/my_type/1
    {
      "text": "Geo-point as an object",
      "location": { 
        "lat": 41.12,    //latitude :纬度的缩写
        "lon": -71.34    //longitude : 经度的缩写
      }
    }
    

    根据地理位置进行查询

    GET /my_index/my_type/_search
    {
      "query": {
        "geo_bounding_box": {
          "location": {
            "top_left" : {
              "lat" : 42,
              "lon" : -72
            },
            "bottom_right" : {
              "lat" : 40,
              "lon" : -74
            }
          }
        }
      }
    }
    //有一点很奇怪,经纬度和坐标系有点不一样
    

    第二个例子

    酒店O2O

    //建立索引
    PUT /hotel_app
    {
      "mappings": {
        "hotels" : {
          "properties": {
            "bin" : {
              "properties": {
                "location" : {
                  "type" : "geo_point"
                }
              }
            }
          }
        }
      }
    }
    //填充数据
    PUT /hotel_app/hotels/1
    {
        "name": "喜来登大酒店",
        "pin" : {
            "location" : {
                "lat" : 40.12,
                "lon" : -71.34
            }
        }
    }
    
    //搜索(两点矩形)
    GET /hotel_app/hotels/_search
    {
      "query": {
        "bool": {
          "must": [
            {"match_all": {}}
          ],
          "filter": {
            "geo_bounding_box": {
              "pin.location": {
                "top_left" : {
                    "lat" : 40.73,
                    "lon" : -74.1
                },
                "bottom_right" : {
                    "lat" : 40.01,
                    "lon" : -71.12
                }
              }
            }
          }
        }
      }
    }
    //搜索(多点,多边形)
    GET /hotel_app/hotels/_search
    {
      "query": {
        "bool": {
          "must": [
            {"match_all": {}}
          ],
          "filter": {
            "geo_polygon": {
              "pin.location": {
                "points": [
                  {"lat" : 40.73, "lon" : -74.1},
                  {"lat" : 40.01, "lon" : -71.12},
                  {"lat" : 50.56, "lon" : -90.58}
                ]
              }
            }
          }
        }
      }
    }
    //搜索(根据距当前位置的距离)
    GET /hotel_app/hotels/_search
    {
      "query": {
        "bool": {
          "must": [
            {"match_all": {}}
          ],
          "filter": {
            "geo_distance": {
              "distance": "1000km",
              "pin.location": {
                "lat": 40,
                "lon": -74
              }
            }
          }
        }
      }
    }
    //聚合分析,(距离当前位置一定范围内有多少个酒店)
    GET /hotel_app/hotels/_search
    {
      "size": 0,
      "aggs": {
        "count_by_distinct": {
          "geo_distance": {
            "field": "pin.location",    //要分析的点
            "origin": {                 //当前位置经纬度
              "lat": 40,
              "lon": 70
            },
            "ranges": [                  //范围控制
              {"to" : 100},
              {
                "from": 100,
                "to": 300
              },
              {"from": 300}
            ],
            "unit": "mi",                //单位
            "distance_type": "arc"       //排序,这个比较消耗性能,请谨慎使用
          }
        }
      }
    }
    
  • 相关阅读:
    Netty学习笔记(三) 自定义编码器
    JavaWeb 消息总线框架 Saka V0.0.1 发布
    BMP 图像信息隐藏及检测
    MATLAB之图像与音频信号处理
    MATLAB之基本语法与基础函数
    CVE-2018-14418 擦出新火花
    浅谈电子数字取证技术
    Linux 反弹 Shell
    Windows 反弹 Shell
    AWD攻防赛之各类漏洞FIX方案
  • 原文地址:https://www.cnblogs.com/cchilei/p/12777870.html
Copyright © 2011-2022 走看看