You can use the k-nearest neighbor (KNN) vector query feature to perform an approximate nearest neighbor search based on vectors. This way, you can find data items that have the highest similarity as the vector that you want to query in a large-scale dataset.
The KNN vector query feature is in invitational preview and disabled by default. To use the KNN vector query feature, submit a ticket.
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 and a vector field is specified. For more information, see Create a search index.
Usage notes
Make sure that the latest version of Tablestore SDK for Go is installed. For more information, see Install Tablestore SDK for Go.
Limits are imposed on the number of vector fields and the number of dimensions for a vector field. For more information, see Search index limits.
The search index server has multiple partitions. Each partition of the search index server returns the top k nearest neighbors to the vector that you want to query. The top k nearest neighbors returned by the partitions are aggregated on the client node. If you want to use tokens to query all data by page, the total number of rows in the response is related to the number of partitions of the search index server.
Parameters
Parameter | Required | Description |
FieldName | Yes | The name of the vector field. |
TopK | Yes | The top K query results that have the highest similarity as the vector that you want to query. For information about the maximum value of the TopK parameter, see Search index limits. Important
|
Float32QueryVector | Yes | The vector for which you want to query the similarity. |
Filter | No | The filter. You can use a combination of query conditions that are not KNN vector query conditions. |
Examples
The following sample code provides an example on how to query the top 10 vectors in a table that have the highest similarity as the specified vector. In this example, the top 10 vectors must meet the following query condition: the value of the col_keyword column is hangzhou.
func query(client *tablestore.TableStoreClient) {
searchQuery := search.NewSearchQuery()
searchQuery.Query = &search.KnnVectorQuery{
FieldName: "col_vector",
TopK: proto.Int32(10),
Float32QueryVector: []float32{-1.4, 1, 1, 1.2},
Filter: &search.BoolQuery{
ShouldQueries: []search.Query{
&search.TermQuery{
FieldName: "col_keyword",
Term: "hangzhou",
},
},
},
}
searchQuery.Sort = &search.Sort{
Sorters: []search.Sorter{
search.NewScoreSort(), // Sort the query results based on scores.
},
}
searchRequest := &tablestore.SearchRequest{
SearchQuery: searchQuery,
TableName: "<TABLE_NAME>",
IndexName: "<SEARCH_INDEX_NAME>",
ColumnsToGet: &tablestore.ColumnsToGet{Columns: []string{
"col_keyword",
"col_long",
}},
}
if resp, err := client.Search(searchRequest); err != nil {
fmt.Println("float32 vector query failed: ", err)
} else {
for _, hit := range resp.SearchHits {
fmt.Println("score:", *hit.Score)
jsonBody, err := json.Marshal(hit.Row)
if err != nil {
panic(err)
}
fmt.Println("row: ", string(jsonBody))
}
}
}
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 Perform 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.