geo_bounding_box查询

匹配与边界框相交的geo_point和geo_shape值。

示例

假设以下文档被索引:

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

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

PUT /my_geoshapes
{
  "mappings": {
    "properties": {
      "pin": {
        "properties": {
          "location": {
            "type": "geo_shape"
          }
        }
      }
    }
  }
}

PUT /my_geoshapes/_doc/1
{
  "pin": {
    "location": {
      "type" : "polygon",
      "coordinates" : [[[13.0 ,51.5], [15.0, 51.5], [15.0, 54.0], [13.0, 54.0], [13.0 ,51.5]]]
    }
  }
}

使用geo_bounding_box筛选器匹配与边界框相交的geo_point值。要定义方框,请提供两个对角的地理点值。

GET my_locations/_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
            }
          }
        }
      }
    }
  }
}

使用相同的过滤器匹配与边界框相交的geo_shape值:

GET my_geoshapes/_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
            }
          }
        }
      }
    }
  }
}

要匹配geo_point和geo_shape值,搜索这两个索引:

GET my_locations,my_geoshapes/_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
            }
          }
        }
      }
    }
  }
}

查询选项

选项说明
_name可选名称字段来标识过滤器
validation_method设置为IGNORE_MALFORMED以接受无效纬度或经度的地理点,设置为COERCE也试图推断正确的纬度或经度。(默认是STRICT)。

支持的格式

就像geo_point类型可以接受geo点的不同表示方式一样,过滤器也可以接受它:

lat lon作为属性

GET my_locations/_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
            }
          }
        }
      }
    }
  }
}

lat lon作为数组

格式为[lon, lat],注意,这里的lon/lat的顺序是为了符合GeoJSON。

GET my_locations/_search
{
  "query": {
    "bool": {
      "must": {
        "match_all": {}
      },
      "filter": {
        "geo_bounding_box": {
          "pin.location": {
            "top_left": [ -74.1, 40.73 ],
            "bottom_right": [ -71.12, 40.01 ]
          }
        }
      }
    }
  }
}

lat lon作为字符串

格式为lat,lon。

GET my_locations/_search
{
  "query": {
    "bool": {
      "must": {
        "match_all": {}
      },
      "filter": {
        "geo_bounding_box": {
          "pin.location": {
            "top_left": "40.73, -74.1",
            "bottom_right": "40.01, -71.12"
          }
        }
      }
    }
  }
}

wkt

GET my_locations/_search
{
  "query": {
    "bool": {
      "must": {
        "match_all": {}
      },
      "filter": {
        "geo_bounding_box": {
          "pin.location": {
            "wkt": "BBOX (-74.1, -71.12, 40.73, 40.01)"
          }
        }
      }
    }
  }
}

geohash

GET my_locations/_search
{
  "query": {
    "bool": {
      "must": {
        "match_all": {}
      },
      "filter": {
        "geo_bounding_box": {
          "pin.location": {
            "top_left": "dr5r9ydj2y73",
            "bottom_right": "drj7teegpus6"
          }
        }
      }
    }
  }
}

当geohash用于指定边界框的边界时,geohash被视为矩形。边界框的定义方式是,它的左上角对应于top_left参数中指定的geohash的左上角,它的右下角定义为bottom_right参数中指定的geohash的右下角。

为了指定与geohash的整个区域匹配的边界框,可以在top_left和bottom_right参数中指定geohash:

GET my_locations/_search
{
  "query": {
    "geo_bounding_box": {
      "pin.location": {
        "top_left": "dr",
        "bottom_right": "dr"
      }
    }
  }
}

在本例中,geohash dr将生成边界框查询,其左上角为45.0,-78.75,右下角为39.375,-67.5。

顶点

边界框的顶点可以通过top_left和bottom_right或top_right和bottom_left参数设置。更多的名字,支持topLeft, bottomRight, topRight和bottomLeft。可以使用简单的名称top、left、bottom和right分别设置值,而不是成对设置值。

GET my_locations/_search
{
  "query": {
    "bool": {
      "must": {
        "match_all": {}
      },
      "filter": {
        "geo_bounding_box": {
          "pin.location": {
            "top": 40.73,
            "left": -74.1,
            "bottom": 40.01,
            "right": -71.12
          }
        }
      }
    }
  }
}

多位置文档

过滤器可以处理每个文档的多个位置/点。一旦一个位置/点匹配过滤器,该文档将被包含在过滤器中

忽略未映射的

当ignore_unmapped选项设置为true时,将忽略未映射字段,并且不会匹配此查询的任何文档。这在查询可能具有不同映射的多个索引时很有用。当设置为false(默认值)时,如果该字段没有映射,查询将抛出异常。

精度说明

地理点的精度有限,在索引时间内总是四舍五入。在查询期间,边界框的上边界向下舍入,而下边界向上舍入。因此,由于舍入误差,沿下界(边界框的底部和左侧边缘)的点可能不会进入边界框。与此同时,查询可能会选择上界(顶部和右侧边缘)旁边的点,即使它们位于边缘之外。四舍五入误差应在纬度上小于4.20 -8度,在经度上小于8.39 -8度,即使在赤道上也小于1cm误差。

地理形状也有有限的精度,由于四舍五入。沿着边界框底部和左边边缘的Geoshape边缘可能不匹配一个geo_bounding_box查询。Geoshape边缘略偏在盒子顶部和右边边缘可能仍然匹配查询。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值