Vector search
Use the Tablestore SDK for Go to query the TopK rows whose vectors are most similar to a query vector.
Prerequisites
Before you begin, complete the following preparations:
-
Install the Tablestore Go SDK and initialize a client.
-
A search index that contains a Vector field is created. For more information, see Create a search index.
Description
Vector search uses KnnVectorQuery to calculate distances between a query vector and vectors in a Vector field and returns the TopK most similar rows. You can use Filter to restrict candidates and MinScore to exclude rows below a score threshold.
Limits apply to the number and dimensions of Vector fields and to parameters such as TopK. For more information, see Search index limits.
A search index contains multiple server-side partitions. Each partition returns its own TopK nearest rows, and a coordinator merges the results. Therefore, when token-based pagination is used, the total number of retrievable rows depends on the number of index partitions.
func (client *tablestore.TableStoreClient) Search(request *tablestore.SearchRequest) (*tablestore.SearchResponse, error)
query := &search.KnnVectorQuery{
FieldName: "embedding",
TopK: proto.Int32(10),
NumCandidates: proto.Int32(100),
Float32QueryVector: []float32{1, 0, 0, 0},
MinScore: proto.Float32(0.1),
Filter: &search.TermQuery{
FieldName: "category",
Term: "books",
},
}
searchQuery := search.NewSearchQuery().
SetQuery(query).
SetSort(&search.Sort{Sorters: []search.Sorter{search.NewScoreSort()}})
response, err := client.Search(&tablestore.SearchRequest{
TableName: "example_table",
IndexName: "example_vector_index",
SearchQuery: searchQuery,
ColumnsToGet: &tablestore.ColumnsToGet{
ReturnAllFromIndex: true,
},
})
if err != nil {
log.Fatal(err)
}
fmt.Println(response.SearchHits)
Parameters
Query request
|
Name |
Type |
Description |
|
TableName (required) |
string |
The name of the data table. |
|
IndexName (required) |
string |
The name of the search index. |
|
SearchQuery (required) |
search.SearchQuery |
The query condition and common query configurations. |
|
ColumnsToGet (optional) |
*tablestore.ColumnsToGet |
The return-column configuration. If this parameter is omitted, only primary key columns are returned. |
|
RoutingValues (optional) |
[]*tablestore.PrimaryKey |
Primary key values for custom routing fields. Omit this parameter if custom routing is not configured. |
|
TimeoutMs (optional) |
*int32 |
The request timeout period in milliseconds. |
Query configuration
|
Name |
Type |
Description |
|
SetQuery (required) |
search.Query |
Specifies the query condition. |
|
SetOffset (optional) |
int32 |
Specifies the start position. Default value: 0. For offset-based pagination, Offset + Limit cannot exceed 100,000. |
|
SetLimit (optional) |
int32 |
Specifies the maximum number of rows to return. Default value: 10. Maximum value: 100. A value of 0 returns no rows. |
|
SetCollapse (optional) |
*search.Collapse |
Collapses query results. For more information, see Collapse query results. |
|
SetSort (optional) |
*search.Sort |
Specifies the result sort order. For more information, see Sort and paginate results. |
|
SetGetTotalCount (optional) |
bool |
Specifies whether to count all matched rows. Default value: false. |
|
SetToken (optional) |
[]byte |
Specifies the NextToken value returned by the previous response. This method clears Sort because the token contains the previous-page sort conditions. Do not specify Offset when you use token-based pagination. |
|
SetSearchFilter (optional) |
*search.SearchFilter |
Applies a post-query filter. For more information, see Use post-query filters. |
|
Aggregation (optional) |
...search.Aggregation |
Configures aggregations. For more information, see Aggregation. |
|
GroupBy (optional) |
...search.GroupBy |
Configures grouping. For more information, see Aggregation. |
Vector query condition
|
Name |
Type |
Description |
|
FieldName (required) |
string |
The name of the Vector field. |
|
TopK (required) |
*int32 |
The number of nearest rows to return. A larger value generally improves recall but increases latency and cost. |
|
Float32QueryVector (required) |
[]float32 |
The query vector. Its dimension must match the index field configuration. |
|
Filter (optional) |
search.Query |
A filter for candidate rows. All query types except vector search are supported. |
|
MinScore (optional) |
*float32 |
The minimum score threshold. The value must be greater than or equal to 0. Default value: 0. Only rows whose scores are greater than this value are returned. |
|
NumCandidates (optional) |
*int32 |
The number of candidate vectors for reranking. The value must be no less than TopK. A larger value generally improves recall but increases latency. |
|
Weight (optional) |
*float32 |
The relevance weight of the query condition. |
Columns to return
|
Name |
Type |
Description |
|
Columns (optional) |
[]string |
The attribute columns to return. This parameter takes effect only when ReturnAll and ReturnAllFromIndex are both false. |
|
ReturnAll (optional) |
bool |
Specifies whether to return all attribute columns in the data table. Default value: false. |
|
ReturnAllFromIndex (optional) |
bool |
Specifies whether to return all indexed attribute columns. Default value: false. Do not set this parameter and ReturnAll to true at the same time. |
Response
|
Name |
Type |
Description |
|
TotalCount |
int64 |
The total number of matched rows. The value depends on SetGetTotalCount. |
|
Rows |
[]*tablestore.Row |
The rows returned by the current query. The number does not exceed the value specified by SetLimit. |
|
SearchHits |
[]*tablestore.SearchHit |
The search hits. Read this field when you use highlighting, nested inner hits, or relevance scores. |
|
NextToken |
[]byte |
The token for the next page. If the value is not empty, pass it to the next query. |
|
IsAllSuccess |
bool |
Indicates whether all index partitions were queried. If the value is false, partial results are returned and TotalCount may be less than the actual number of matched rows. |
|
AggregationResults |
search.AggregationResults |
The aggregation results. |
|
GroupByResults |
search.GroupByResults |
The grouping results. |