Use Tablestore SDK for Python to filter data by distance from a center point, a bounding box, or a polygon.
Prerequisites
Install the Tablestore SDK for Python and initialize a client.
Description
Geo queries filter data based on the geographical locations in a GeoPoint field. You can query by distance, bounding box, or polygon. When you call the search method, set the query type to GeoDistanceQuery, GeoBoundingBoxQuery, or GeoPolygonQuery based on the required geographical range.
GeoDistanceQuery(field_name, center_point, distance)
GeoBoundingBoxQuery(field_name, top_left, bottom_right)
GeoPolygonQuery(field_name, points)
The following example queries rows in which the location field is no more than 200,000 meters from 30.25,120.16 and returns up to 10 rows and the total number of matching rows.
query = GeoDistanceQuery("location", "30.25,120.16", 200000)
search_query = SearchQuery(
query,
limit=10,
get_total_count=True,
)
response = client.search(
"example_table",
"example_index",
search_query,
ColumnsToGet(return_type=ColumnReturnType.ALL),
)
print(response.total_count)
for row in response.rows:
print(row)
Parameters
Search request
The search method contains the following parameters.
|
Name |
Type |
Description |
|
table_name (required) |
|
The name of the data table. |
|
index_name (required) |
|
The name of the search index. |
|
search_query (required) |
|
The query condition and common query configurations. |
|
columns_to_get (optional) |
|
The return column configuration. If this parameter is not specified, only primary key columns are returned. |
|
routing_keys (optional) |
|
The list of primary key values for custom routing fields. You do not need to specify this parameter if custom routing is not configured. |
|
timeout_s (optional) |
|
The request timeout in seconds. If this parameter is not specified, the client-level timeout is used. |
Query configuration
search_query is of the SearchQuery type and contains the following parameters.
|
Name |
Type |
Description |
|
query (required) |
|
The query condition. Set this parameter to |
|
sort (optional) |
|
The sort order of query results. For more information, see Sort and paginate results. |
|
get_total_count (optional) |
|
Specifies whether to return the total number of matching rows. Default value: |
|
next_token (optional) |
|
The pagination token. Set this parameter to |
|
offset (optional) |
|
The offset from which the current query starts. Use this parameter for shallow pagination. |
|
limit (optional) |
|
The maximum number of rows to return. If this parameter is set to |
|
aggs (optional) |
|
The aggregation configurations. For more information, see Aggregation. |
|
group_bys (optional) |
|
The grouping configurations. For more information, see Aggregation. |
|
collapse_field (optional) |
|
The result collapsing configuration, which removes duplicate results based on a specified field. For more information, see Collapse query results. |
Coordinates in all three query types use the latitude,longitude format. Latitude precedes longitude. The latitude range is [-90,+90], and the longitude range is [-180,+180]. Example: 35.8,-45.91.
Geo distance condition
search_query.query is of the GeoDistanceQuery type and contains the following parameters.
|
Name |
Type |
Description |
|
field_name (required) |
|
The name of the |
|
center_point (required) |
|
The coordinates of the center point. |
|
distance (required) |
|
The maximum distance from the center point. Unit: meters. |
Geo bounding box condition
search_query.query is of the GeoBoundingBoxQuery type and contains the following parameters.
|
Name |
Type |
Description |
|
field_name (required) |
|
The name of the |
|
top_left (required) |
|
The coordinates of the upper-left corner. |
|
bottom_right (required) |
|
The coordinates of the lower-right corner. |
Geo polygon condition
search_query.query is of the GeoPolygonQuery type and contains the following parameters.
|
Name |
Type |
Description |
|
field_name (required) |
|
The name of the |
|
points (required) |
|
The list of coordinates that form the polygon. Specify the coordinates in boundary order. |
Return columns
columns_to_get is of the ColumnsToGet type and contains the following parameters.
|
Name |
Type |
Description |
|
column_names (optional) |
|
The names of attribute columns to return. Specify this parameter only when |
|
return_type (optional) |
|
The return column mode. |
Response
The search method returns SearchResponse. The following table describes the core fields.
|
Field |
Type |
Description |
|
rows |
|
The rows returned by the current query. The number of rows does not exceed |
|
next_token |
|
The token for the next page. If this field is not empty, pass it to the next request to continue reading. |
|
total_count |
|
The number of matching rows. The value depends on the |
|
is_all_succeed |
|
Indicates whether all index partitions were queried. If the value is |
|
agg_results |
|
The aggregation results. This field is empty if |
|
group_by_results |
|
The grouping results. This field is empty if |
|
search_hits |
|
The search hits, including extended information such as relevance scores, highlights, and matching child rows. |
Tuple-compatible response
Starting from Tablestore SDK for Python 5.2.0, search APIs return response objects instead of tuples. Version 5.1.0 and earlier return tuples directly. In version 5.2.1 and later, you can call SearchResponse.v1_response() to obtain a tuple compatible with earlier versions. For new code, access SearchResponse attributes directly to avoid unpacking errors if response fields are extended.
(
rows,
next_token,
total_count,
is_all_succeed,
agg_results,
group_by_results,
search_hits,
) = response.v1_response()
Examples
Query data in a bounding box
The following example queries data in the bounding box defined by the upper-left corner 32.0,119.0 and lower-right corner 29.0,122.0.
query = GeoBoundingBoxQuery(
"location",
"32.0,119.0",
"29.0,122.0",
)
response = client.search(
"example_table",
"example_index",
SearchQuery(query, limit=10),
ColumnsToGet(return_type=ColumnReturnType.ALL),
)
print(response.rows)
Query data in a polygon
The following example queries data in the polygon formed by four coordinates.
query = GeoPolygonQuery(
"location",
[
"29.0,119.0",
"32.0,119.0",
"32.0,122.0",
"29.0,122.0",
],
)
response = client.search(
"example_table",
"example_index",
SearchQuery(query, limit=10),
ColumnsToGet(return_type=ColumnReturnType.ALL),
)
print(response.rows)