多元索引支持三种地理位置查询方式,分别是GeoBoundingBoxQuery、GeoDistanceQuery和GeoPolygonQuery。

GeoBoundingBoxQuery

使用GeoBoundingBoxQuery进行地理边界框查询。根据一个矩形范围的地理位置边界条件查询表中的数据,当一个地理位置点落在给出的矩形范围内时,满足查询条件。

示例
$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);

GeoDistanceQuery

使用GeoDistanceQuery进行地理距离查询。根据一个中心点和距离条件查询表中的数据,当一个地理位置点到指定的中心点的距离不超过指定的值时,满足查询条件。

示例
$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);

GeoPolygonQuery

使用GeoPolygonQuery进行地理多边形查询。根据一个多边形范围条件查询表中的数据,当一个地理位置点落在指定的多边形内时,满足查询条件。

示例
$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);