All Products
Search
Document Center

Tablestore:Geo query

Last Updated:Aug 19, 2024

Geo queries are classified into the following types: geo-distance query, geo-bounding box query, and geo-polygon query.

Prerequisites

Geo-distance query

To perform a geo-distance query, specify a circular geographic area by using a central point and a radius. Tablestore returns the rows in which the value of a specific column falls within the circular geographic area.

  • Parameters

    Parameter

    Description

    table_name

    The name of the data table.

    index_name

    The name of the search index.

    query

    The query statement for the search index. To use geo-distance query, set the query type to QueryTypeConst::GEO_DISTANCE_QUERY.

    field_name

    The name of the column that you want to query. The value of this parameter is of the GEOPOINT data type.

    center_point

    The coordinate pair of the central point. The coordinate pair consists of latitude and longitude values.

    The coordinate pair is in the latitude,longitude format. Valid values of latitude: [-90,+90]. Valid values of longitude: [-180,+180]. Example: 35.8,-45.91.

    distance

    The radius of the circular geographical area. The value of this parameter is of the DOUBLE data type. Unit: meters.

  • Examples

    The following sample code shows how to query all rows whose data in the geo column is no more than 1,000 meters away from the central point 30.001,120.001 in a data table.

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

Geo-bounding box query

To perform a geo-bounding box query, specify a rectangular geographic area by using an upper-left corner and a lower-right corner. Tablestore returns the rows in which the value of a specific column falls within the rectangular geographic area.

  • Parameters

    Parameter

    Description

    table_name

    The name of the data table.

    index_name

    The name of the search index.

    query

    The query statement for the search index. To use geo-bounding query, set the query type to QueryTypeConst::GEO_BOUNDING_BOX_QUERY.

    field_name

    The name of the column that you want to query. The value of this parameter is of the GEOPOINT data type.

    top_left

    The coordinate pair of the upper-left corner of the rectangular geographic area.

    bottom_right

    The coordinate pair of the lower-right corner of the rectangle box. A rectangular geographical area can be specified by a upper-left corner and a lower-right corner.

    The coordinate pair is in the latitude,longitude format. Valid values of latitude: [-90,+90]. Valid values of longitude: [-180,+180]. Example: 35.8,-45.91.

  • Examples

    The following sample code shows how to query all rows whose data in the geo column falls within the rectangular area defined by an upper-left point (coordinates: 31,119) and a lower-right point (coordinates: 29,121) in a data table.

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

Geo-polygon query

To perform a geo-polygon query, specify a polygon geographic area by using the coordinate pairs of multiple points. Tablestore returns the rows in which the value of a specific column falls within the polygon geographic area.

  • Parameters

    Parameter

    Description

    table_name

    The name of the data table.

    index_name

    The name of the search index.

    query

    The query statement for the search index. To use geo-polygon query, set the query type to QueryTypeConst::GEO_POLYGON_QUERY.

    field_name

    The name of the column that you want to query. The value of this parameter is of the GEOPOINT data type.

    points

    The coordinate pairs of the points that define the polygon geographic area. You can specify a polygon by using multiple coordinate pairs.

    The coordinate pair is in the latitude,longitude format. Valid values of latitude: [-90,+90]. Valid values of longitude: [-180,+180]. Example: 35.8,-45.91.

  • Examples

    The following sample code shows how to query all rows whose data in the geo column falls within the polygonal area defined by points whose respective coordinates are 31,120, 29,121, and 29,119 in a data table.

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

FAQ

References

  • When you use a search index to query data, you can use the following query methods: term query, terms query, match all query, match query, match phrase query, prefix query, range query, wildcard query, Boolean query, geo query, nested query, and exists query. You can use different query methods to query data from multiple dimensions based on your business requirements.

    If you want to sort or paginate the rows that meet the query conditions, you can use the sorting and paging feature. For more information, see Sorting and paging.

    If you want to collapse the result set based on a specific column, you can use the collapse (distinct) feature. This way, data of the specified type appears only once in the query results. For more information, see Collapse (distinct).

  • If you want to analyze data in a data table, such as obtaining the extreme values, sum, and total number of rows, you can perform aggregation operations or execute SQL statements. For more information, see Aggregation and SQL query.

  • If you want to quickly obtain all rows that meet the query conditions without the need to sort the rows, you can call the ParallelScan and ComputeSplits operations to use the parallel scan feature. For more information, see Parallel scan.