All Products
Search
Document Center

Tablestore:Perform a 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

    FieldName

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

    CenterPoint

    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".

    DistanceInMeter

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

    Query

    The query statement for the search index. Set the query type to GeoDistanceQuery.

    TableName

    The name of the data table.

    IndexName

    The name of the search index.

    ColumnsToGet

    Specifies whether to return all columns in the rows that meet the query conditions. You can specify the ReturnAll, Columns, and ReturnAllFromIndex parameters.

    The default value of ReturnAll is false, which indicates that not all columns are returned. You can use one of the following methods to specify the columns that you want to return. If you do not use the following methods to specify the columns that you want to return, only the primary key columns are returned.
    • Configure Columns to specify the columns that you want to return.
    • Set ReturnAllFromIndex to true to return all columns from the search index.

    If you set ReturnAll to true, all columns are returned.

  • Example

    The following sample code shows how to query the rows in which the value of the geo_type_col column falls within a circular geographic area in a table.

    /// <summary> 
    /// Query the rows in which the value of the geo_type_col column falls within a circular geographical area in a table. 
    /// </summary>
    /// <param name="client"></param>
    public static void GeoDistanceQuery(OTSClient client)
    {
        SearchQuery searchQuery = new SearchQuery();
        GeoDistanceQuery geoDistanceQuery = new GeoDistanceQuery(); // Set the query type to GeoDistanceQuery. 
        geoDistanceQuery.FieldName = Geo_type_col;
        geoDistanceQuery.CenterPoint = "10,11"; // Specify the coordinate pair for a central point. 
        geoDistanceQuery.DistanceInMeter = 10000; // Set the radius of the circular geographical area to 10,000. Unit: meter. 
        searchQuery.Query = geoDistanceQuery;
    
        SearchRequest searchRequest = new SearchRequest(TableName, IndexName, searchQuery);
    
        ColumnsToGet columnsToGet = new ColumnsToGet();
        columnsToGet.Columns = new List<string>() { Geo_type_col }; // Specify that the Geo_type_col column is returned. 
        searchRequest.ColumnsToGet = columnsToGet;
    
        SearchResponse response = client.Search(searchRequest);
        Console.WriteLine(response.TotalCount);
    }
                

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

    FieldName

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

    TopLeft

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

    BottomRight

    The coordinate pair of the lower-right corner of the rectangular geographical area. The coordinate pairs of the upper-left corner and lower-right corner define a unique rectangular geographical area.

    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".

    Query

    The query statement for the search index. Set the query type to GeoBoundingBoxQuery.

    TableName

    The name of the data table.

    IndexName

    The name of the search index.

    ColumnsToGet

    Specifies whether to return all columns in the rows that meet the query conditions. You can specify the ReturnAll, Columns, and ReturnAllFromIndex parameters.

    The default value of ReturnAll is false, which indicates that not all columns are returned. You can use one of the following methods to specify the columns that you want to return. If you do not use the following methods to specify the columns that you want to return, only the primary key columns are returned.
    • Configure Columns to specify the columns that you want to return.
    • Set ReturnAllFromIndex to true to return all columns from the search index.

    If you set ReturnAll to true, all columns are returned.

  • Example

    The following sample code shows how to query the rows in which the value of the geo_type_col column falls within the rectangular geographic area whose upper-left corner is at "10,0" and lower-right corner is at "0,10".

    /// <summary>
    /// Query the rows in which the value of the geo_type_col column is within a rectangular geographical area in a table. The value of the geo_type_col column is of the GEOPOINT data type. The rectangular geographical area is specified by an upper-left corner whose coordinate pair is "10,0" and a lower-right corner whose coordinate pair is "0,10". 
    /// </summary>
    /// <param name="client"></param>
    public static void GeoBoundingBoxQuery(OTSClient client)
    {
        SearchQuery searchQuery = new SearchQuery();
        GeoBoundingBoxQuery geoBoundingBoxQuery = new GeoBoundingBoxQuery(); // Set the query type to GeoBoundingBoxQuery. 
        geoBoundingBoxQuery.FieldName = Geo_type_col; // Specify the name of the column that you want to query. 
        geoBoundingBoxQuery.TopLeft = "10,0"; // Specify the coordinate pair for the upper-left corner of the rectangular geographical area. 
        geoBoundingBoxQuery.BottomRight = "0,10"; // Specify coordinate pair for the lower-right corner of the rectangular geographical area. 
        searchQuery.Query = geoBoundingBoxQuery;
    
        SearchRequest searchRequest = new SearchRequest(TableName, IndexName, searchQuery);
    
        var columnsToGet = new ColumnsToGet();
        columnsToGet.Columns = new List<string> { Geo_type_col }; // Specify that the Geo_type_col column is returned. 
        searchRequest.ColumnsToGet = columnsToGet;
    
        SearchResponse response = client.Search(searchRequest);
        Console.WriteLine(response.TotalCount);
    }

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

    FieldName

    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 a polygon geographical area.

    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".

    Query

    The query statement for the search index. Set the query type to GeoPolygonQuery.

    TableName

    The name of the data table.

    IndexName

    The name of the search index.

    ColumnsToGet

    Specifies whether to return all columns in the rows that meet the query conditions. You can specify the ReturnAll, Columns, and ReturnAllFromIndex parameters.

    The default value of ReturnAll is false, which indicates that not all columns are returned. You can use one of the following methods to specify the columns that you want to return. If you do not use the following methods to specify the columns that you want to return, only the primary key columns are returned.
    • Configure Columns to specify the columns that you want to return.
    • Set ReturnAllFromIndex to true to return all columns from the search index.

    If you set ReturnAll to true, all columns are returned.

  • Example

    The following sample code shows how to query the rows in which the value of the geo_type_col column falls within a polygon geographic area in a table.

    /// <summary>
    /// Query the rows in which the value of the geo_type_col column is within a polygon geographical area in a table. 
    /// </summary>
    /// <param name="client"></param>
    public static void GeoPolygonQuery(OTSClient client)
    {
        SearchQuery searchQuery = new SearchQuery();
        GeoPolygonQuery geoPolygonQuery = new GeoPolygonQuery(); // Set the query type to GeoPolygonQuery. 
        geoPolygonQuery.FieldName = Geo_type_col;
        geoPolygonQuery.Points = new List<string>() { "0,0", "10,0", "10,10" }; // Specify coordinate pairs for vertices of a polygon geographical area. 
        searchQuery.Query = geoPolygonQuery;
    
        SearchRequest searchRequest = new SearchRequest(TableName, IndexName, searchQuery);
    
        ColumnsToGet columnsToGet = new ColumnsToGet();
        columnsToGet.Columns = new List<string>() { Geo_type_col }; // Specify that the Geo_type_col column is returned. 
        searchRequest.ColumnsToGet = columnsToGet;
    
        SearchResponse response = client.Search(searchRequest);
        Console.WriteLine(response.TotalCount);
    }
                

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.

    You can sort or paginate rows that meet the query conditions by using the sorting and paging features. For more information, see Sorting and paging.

    You can use the collapse (distinct) feature to collapse the result set based on a specific column. 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, you can use the aggregation feature of the Search operation or execute SQL statements. For example, you can obtain the minimum and maximum values, sum, and total number of rows. For more information, see Aggregation and SQL query.

  • If you want to 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.