Geo queries are classified into the following types: geo-distance query, geo-bounding box query, and geo-polygon query.
Prerequisites
An OTSClient 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 search indexes.
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 used to meet the query conditions. 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.
This parameter value must be in the format of "latitude,longitude". Valid values of the 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: meters.
Query
The type of the query. Set the query type to GeoDistanceQuery.
TableName
The name of the data table.
IndexName
The name of the search index.
Sample code
The following sample code provides an example on how to query the rows in which the value of Col_GeoPoint falls within a circular geographical area:
func GeoDistanceQuery(client *tablestore.TableStoreClient, tableName string, indexName string) { searchRequest := &tablestore.SearchRequest{} searchRequest.SetTableName(tableName) searchRequest.SetIndexName(indexName) query := &search.GeoDistanceQuery{} // Set the query type to GeoDistanceQuery. query.FieldName = "Col_GeoPoint" query.CenterPoint = "5,5" // Specify the coordinate pair of the central point. query.DistanceInMeter = 10000.0 // Set the radius of the circular geographical area to 10,000. Unit: meter. searchQuery := search.NewSearchQuery() searchQuery.SetQuery(query) searchRequest.SetSearchQuery(searchQuery) // Return all columns. searchRequest.SetColumnsToGet(&tablestore.ColumnsToGet{ ReturnAll:true, }) searchResponse, err := client.Search(searchRequest) if err != nil { fmt.Printf("%#v", err) return } fmt.Println("IsAllSuccess: ", searchResponse.IsAllSuccess) // Check whether all rows that meet the query conditions are returned. fmt.Println("TotalCount: ", searchResponse.TotalCount) // Display the total number of rows that meet the query conditions instead of the number of returned rows. fmt.Println("RowCount: ", len(searchResponse.Rows)) for _, row := range searchResponse.Rows { jsonBody, err := json.Marshal(row) if err != nil { panic(err) } fmt.Println("Row: ", string(jsonBody)) } }
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 used to meet the query conditions. 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.
This parameter value must be in the format of "latitude,longitude". Valid values of the latitude: [-90,90]. Valid values of longitude: [-180,180]. Example: “35.8,-45.91".
Query
The type of the query. Set the query type to GeoBoundingBoxQuery.
TableName
The name of the data table.
IndexName
The name of the search index.
Sample code
The following sample code provides an example on how to query the rows in which the value of Col_GeoPoint is within the rectangular geographical area whose upper-left corner is at '10,0' and lower-right corner is at '0,10':
func GeoBoundingBoxQuery(client *tablestore.TableStoreClient, tableName string, indexName string) { searchRequest := &tablestore.SearchRequest{} searchRequest.SetTableName(tableName) searchRequest.SetIndexName(indexName) query := &search.GeoBoundingBoxQuery{} // Set the query type to GeoBoundingBoxQuery. query.FieldName = "Col_GeoPoint" // Specify the name of the column that you want to query. query.TopLeft = "10,0" // Specify the coordinate pair of the upper-left corner of the rectangular geographic area. query.BottomRight = "0,10" // Specify the coordinate pair of the lower-right corner of the rectangular geographic area. searchQuery := search.NewSearchQuery() searchQuery.SetQuery(query) searchRequest.SetSearchQuery(searchQuery) // Return all columns in the rows that meet the query conditions. searchRequest.SetColumnsToGet(&tablestore.ColumnsToGet{ ReturnAll:true, }) searchResponse, err := client.Search(searchRequest) if err != nil { fmt.Printf("%#v", err) return } fmt.Println("IsAllSuccess: ", searchResponse.IsAllSuccess) // Check whether all rows that meet the query conditions are returned. fmt.Println("TotalCount: ", searchResponse.TotalCount) // Display the total number of rows that meet the query conditions instead of the number of returned rows. fmt.Println("RowCount: ", len(searchResponse.Rows)) for _, row := range searchResponse.Rows { jsonBody, err := json.Marshal(row) if err != nil { panic(err) } fmt.Println("Row: ", string(jsonBody)) } }
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 used to meet the query conditions. 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 of each point must be in the format of "latitude,longitude". Valid values of latitude: [-90,90]. Valid values of longitude: [-180,180]. Example: “35.8,-45.91".
Query
The type of the query. Set the query type to GeoPolygonQuery.
TableName
The name of the data table.
IndexName
The name of the search index.
Sample code
The following sample code provides an example on how to query the rows in which the value of Col_GeoPoint is within a polygonal geographical area:
func GeoPolygonQuery(client *tablestore.TableStoreClient, tableName string, indexName string) { searchRequest := &tablestore.SearchRequest{} searchRequest.SetTableName(tableName) searchRequest.SetIndexName(indexName) query := &search.GeoPolygonQuery{} // Set the query type to GeoDistanceQuery. query.FieldName = "Col_GeoPoint" query.Points = []string{"0,0","5,5","5,0"} // Specify the coordinate pairs of the vertices of the polygon geographic area. searchQuery := search.NewSearchQuery() searchQuery.SetQuery(query) searchRequest.SetSearchQuery(searchQuery) // Return all columns in the rows that meet the query conditions. searchRequest.SetColumnsToGet(&tablestore.ColumnsToGet{ ReturnAll:true, }) searchResponse, err := client.Search(searchRequest) if err != nil { fmt.Printf("%#v", err) return } fmt.Println("IsAllSuccess: ", searchResponse.IsAllSuccess) // Check whether all rows that meet the query conditions are returned. fmt.Println("TotalCount: ", searchResponse.TotalCount) // Display the total number of rows that meet the query conditions instead of the number of returned rows. fmt.Println("RowCount: ", len(searchResponse.Rows)) for _, row := range searchResponse.Rows { jsonBody, err := json.Marshal(row) if err != nil { panic(err) } fmt.Println("Row: ", string(jsonBody)) } }
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.