Geo queries are classified into the following types: geo-distance query, geo-bounding box query, and geo-polygon query.
Prerequisites
A TableStoreClient instance is initialized. For more information, see Initialize an OTSClient instance.
A data table is created and data is written to the data table. For more information, see Create a data table and Write data.
A search index is created for the data table. For more information, see Create a search index.
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
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.
query
The query statement for the search index. Set this parameter to GeoDistanceQuery.
table_name
The name of the data table.
index_name
The name of the search index.
Example
The following sample code provides an example on how to query the rows in which the values of the Col_GeoPoint column are within the circular geographical area whose central point is '32.5,116.5' and radius is up to 300,000 meters:
query = GeoDistanceQuery('Col_GeoPoint', '32.5,116.5', 300000) rows, next_token, total_count, is_all_succeed, agg_results, group_by_results = client.search( '<TABLE_NAME>', '<SEARCH_INDEX_NAME>', SearchQuery(query, limit=100, get_total_count=True), ColumnsToGet(return_type=ColumnReturnType.ALL) ).v1_response()
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
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 rectangular geographic area. A rectangular geographic area can be specified by using an 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
.query
The query statement for the search index. Set this parameter to GeoBoundingBoxQuery.
table_name
The name of the data table.
index_name
The name of the search index.
Example
The following sample code provides an example on how to query the rows in which the values of the Col_GeoPoint column are within the rectangular geographical area whose upper-left corner is at '30.9,112.0' and lower-right corner is at '30.2,119.0':
query = GeoBoundingBoxQuery('Col_GeoPoint', '30.9,112.0', '30.2,119.0') rows, next_token, total_count, is_all_succeed, agg_results, group_by_results = client.search( '<TABLE_NAME>', '<SEARCH_INDEX_NAME>', SearchQuery(query, limit=100, get_total_count=True), ColumnsToGet(return_type=ColumnReturnType.ALL) ).v1_response()
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
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
.query
The query statement for the search index. Set this parameter to GeoPolygonQuery.
table_name
The name of the data table.
index_name
The name of the search index.
Example
The following sample code provides an example on how to query the rows in which the values of the Col_Geopoint column are within the polygonal geographical area that consists of the coordinates of '30.9,112.0', '30.5,115.0', '30.3, 117.0', and '30.2,119.0':
query = GeoPolygonQuery('Col_GeoPoint', ['30.9,112.0', '30.5,115.0', '30.3, 117.0', '30.2,119.0']) rows, next_token, total_count, is_all_succeed, agg_results, group_by_results = client.search( '<TABLE_NAME>', '<SEARCH_INDEX_NAME>', SearchQuery(query, limit=100, get_total_count=True), ColumnsToGet(return_type=ColumnReturnType.ALL) ).v1_response()
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, geo query, Boolean query, KNN vector query, nested query, and exists query. You can use the query methods provided by the search index 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.