发布于 2016-02-29 14:38:07 | 328 次阅读 | 评论: 0 | 来源: 网络整理
这是目前为止最有效的 地理坐标过滤器了,因为它计算起来非常简单。
你指定一个矩形的 顶部(top), 底部(bottom), 左边界(left), 和 右边界(right),
然后它只需判断坐标的经度是否在左右边界之间,纬度是否在上下边界之间。(译注:原文似乎写反了)
GET /attractions/restaurant/_search
{
"query": {
"filtered": {
"filter": {
"geo_bounding_box": {
"location": { <1>
"top_left": {
"lat": 40.8,
"lon": -74.0
},
"bottom_right": {
"lat": 40.7,
"lon": -73.0
}
}
}
}
}
}
}
地理坐标盒模型过滤器不需要把所有坐标点都加载到内存里。
因为它要做的只是简单判断 纬度 和 经度 坐标数值是否在给定的范围内,所以它可以用倒排索引来做一个范围(range)过滤。
要使用这种优化方式,需要把 geo_point 字段用 纬度(lat)和经度(lon)方式表示并分别索引。
PUT /attractions
{
"mappings": {
"restaurant": {
"properties": {
"name": {
"type": "string"
},
"location": {
"type": "geo_point",
"lat_lon": true <1>
}
}
}
}
}
然后,查询时你需要告诉 Elasticesearch 使用已索引的 lat和lon。
GET /attractions/restaurant/_search
{
"query": {
"filtered": {
"filter": {
"geo_bounding_box": {
"type": "indexed", <1>
"location": {
"top_left": {
"lat": 40.8,
"lon": -74.0
},
"bottom_right": {
"lat": 40.7,
"lon": -73.0
}
}
}
}
}
}
}
注意:
geo_point类型可以包含多个地理坐标点,但是针对经度纬度分别索引的这种优化方式(lat_lon)只对单个坐标点的方式有效。