地理位置查询包括地理距离查询(GeoDistanceQuery)、地理长方形范围查询(GeoBoundingBoxQuery)和地理多边形范围查询(GeoPolygonQuery)三种方式。
前提条件
地理距离查询(GeoDistanceQuery)
GeoDistanceQuery根据一个中心点和距离条件查询表中的数据,当一个地理位置点到指定的中心点的距离不超过指定的值时,满足查询条件。
- 参数
参数 说明 table_name 数据表名称。 index_name 多元索引名称。 query 多元索引的查询语句。设置查询类型为QueryTypeConst::GEO_DISTANCE_QUERY。 field_name 列名,类型为Geopoint。 center_point 中心地理坐标点,是一个经纬度值。 格式为
纬度,经度
,纬度在前,经度在后,且纬度范围为[-90,+90],经度范围[-180,+180]。例如35.8,-45.91
。distance 距离中心点的距离,类型为Double。单位为米。 - 示例
查询表中geo列的值距离中心点
30.001,120.001
不超过1000米的数据。$request = array( 'table_name' => 'php_sdk_test', 'index_name' => 'php_sdk_test_search_index', 'search_query' => array( 'offset' => 0, 'limit' => 2, 'get_total_count' => true, 'query' => array( 'query_type' => QueryTypeConst::GEO_DISTANCE_QUERY, 'query' => array( 'field_name' => 'geo', 'center_point' => '30.001,120.001', 'distance' => 1000 ) ), 'sort' => array( array( 'geo_distance_sort' => array( 'field_name' => 'geo', 'order' => SortOrderConst::SORT_ORDER_ASC, 'distance_type' => GeoDistanceTypeConst::GEO_DISTANCE_PLANE, 'points' => array('30,120') ) ), ) ), 'columns_to_get' => array( 'return_type' => ColumnReturnTypeConst::RETURN_SPECIFIED, 'return_names' => array('geo') ) ); $response = $otsClient->search($request);
地理长方形范围查询(GeoBoundingBoxQuery)
GeoBoundingBoxQuery根据一个长方形范围的地理位置边界条件查询表中的数据,当一个地理位置点落在给出的长方形范围内时,满足查询条件。
- 参数
参数 说明 table_name 数据表名称。 index_name 多元索引名称。 query 多元索引的查询语句。设置查询类型为QueryTypeConst::GEO_BOUNDING_BOX_QUERY。 field_name 列名,类型为Geopoint。 top_left 长方形框的左上角的坐标。 bottom_right 长方形框的右下角的坐标,通过左上角和右下角的坐标可以确定一个唯一的长方形。 格式为
纬度,经度
,纬度在前,经度在后,且纬度范围为[-90,+90],经度范围[-180,+180]。例如35.8,-45.91
。 - 示例
查询表中geo列的值在左上角坐标为
31,119
,右下角坐标为29,121
的长方形范围内的数据。$request = array( 'table_name' => 'php_sdk_test', 'index_name' => 'php_sdk_test_search_index', 'search_query' => array( 'offset' => 0, 'limit' => 2, 'get_total_count' => true, 'query' => array( 'query_type' => QueryTypeConst::GEO_BOUNDING_BOX_QUERY, 'query' => array( 'field_name' => 'geo', 'top_left' => '31,119', 'bottom_right' => '29,121' ) ), 'sort' => array( array( 'geo_distance_sort' => array( 'field_name' => 'geo', 'order' => SortOrderConst::SORT_ORDER_ASC, 'distance_type' => GeoDistanceTypeConst::GEO_DISTANCE_PLANE, 'points' => array('30,120') ) ), ) ), 'columns_to_get' => array( 'return_type' => ColumnReturnTypeConst::RETURN_SPECIFIED, 'return_names' => array('geo') ) ); $response = $otsClient->search($request);
地理多边形范围查询(GeoPolygonQuery)
GeoPolygonQuery根据一个多边形范围的地理位置边界条件查询表中的数据,当一个地理位置点落在指定的多边形范围内时,满足查询条件。
- 参数
参数 说明 table_name 数据表名称。 index_name 多元索引名称。 query 多元索引的查询语句。设置查询类型为QueryTypeConst::GEO_POLYGON_QUERY。 field_name 列名,类型为Geopoint。 points 组成多边形范围的坐标,通过多个坐标可以确定一个唯一的多边形。 格式为
纬度,经度
,纬度在前,经度在后,且纬度范围为[-90,+90],经度范围[-180,+180]。例如35.8,-45.91
。 - 示例
查询表中geo列的值在由
31,120
、29,121
和29,119
坐标组成的多边形范围内的数据。$request = array( 'table_name' => 'php_sdk_test', 'index_name' => 'php_sdk_test_search_index', 'search_query' => array( 'offset' => 0, 'limit' => 2, 'get_total_count' => true, 'query' => array( 'query_type' => QueryTypeConst::GEO_POLYGON_QUERY, 'query' => array( 'field_name' => 'geo', 'points' => array( "31,120", "29,121", "29,119" ) ) ), 'sort' => array( array( 'geo_distance_sort' => array( 'field_name' => 'geo', 'order' => SortOrderConst::SORT_ORDER_ASC, 'distance_type' => GeoDistanceTypeConst::GEO_DISTANCE_PLANE, 'points' => array('30,120') ) ), ) ), 'columns_to_get' => array( 'return_type' => ColumnReturnTypeConst::RETURN_SPECIFIED, 'return_names' => array('geo') ) ); $response = $otsClient->search($request);