elasticsearch文档 Query DSL

Geo queries

Elasticsearch支持两种类型的 geo 数据: geo_point 字段支持 lat / lon 对,geo_shape 字段支持点、线、圆、多边形等。

查询语句如下:

  • geo_bounding_box query:
    • 查询出geoshapes或geopoints与指定的多边形相交的文档。
  • geo_distance query:
    • 查询出geoshapes或geopoints与中心点在指定距离内的文档。
  • geo_grid query:
    • ......
  • geo_polygon query:
    • 查询出geoshapes或geopoints与指定多边形相交的文档。
  • geo_shape query:
    • 查询出geoshapes或geopoints与指定的geoshape相关的文档。关联关系可以是:相交、包含、毗邻。

Geo-distance query

对于文档中的 geo_point 和 geo_shape, 判断是否在给定的geopoint距离之内。

Example

创建一个索引,叫做 my_locations

PUT /my_locations
{
  "mappings": {
    "properties": {
      "pin": {
        "properties": {
          "location": {
            "type": "geo_point"
          }
        }
      }
    }
  }
}

插入一条id是1的记录

PUT /my_locations/_doc/1
{
  "pin": {
    "location": {
      "lat": 40.12,
      "lon": -71.34
    }
  }
}

使用 geo_distance 过滤器来匹配与某个geo_point在特定距离内的其他geo_point

GET /my_locations/_search
{
  "query": {
    "bool": {
      "must": {
        "match_all": {}
      },
      "filter": {
        "geo_distance": {
          "distance": "200km",
          "pin.location": {
            "lat": 40,
            "lon": -70
          }
        }
      }
    }
  }
}

Accepted formats

geo_point可以有不同的表现方式,过滤器都能很好的识别

Lat lon as properties

lat 和 lon都是作为属性,例子如下


GET /my_locations/_search
{
  "query": {
    "bool": {
      "must": {
        "match_all": {}
      },
      "filter": {
        "geo_distance": {
          "distance": "12km",
          "pin.location": {
            "lat": 40,
            "lon": -70
          }
        }
      }
    }
  }
}

Lat lon as array

格式 [lon, lat], 


GET /my_locations/_search
{
  "query": {
    "bool": {
      "must": {
        "match_all": {}
      },
      "filter": {
        "geo_distance": {
          "distance": "12km",
          "pin.location": [ -70, 40 ]
        }
      }
    }
  }
}

Lat lon as WKT string

GET /my_locations/_search
{
  "query": {
    "bool": {
      "must": {
        "match_all": {}
      },
      "filter": {
        "geo_distance": {
          "distance": "12km",
          "pin.location": "POINT (-70 40)"
        }
      }
    }
  }
}

Geohash

GET /my_locations/_search
{
  "query": {
    "bool": {
      "must": {
        "match_all": {}
      },
      "filter": {
        "geo_distance": {
          "distance": "12km",
          "pin.location": "drm3btev3e86"
        }
      }
    }
  }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值