Vector search with the Tablestore SDK for Node.js returns the nearest data in a search index by vector similarity and supports score thresholds, candidate counts, and non-vector filters.
Prerequisites
-
An OTSClient instance is initialized. For more information, see Initialize a Tablestore client.
-
A search index is created and the vector field is configured on the data table. For more information, see Create a search index.
Usage notes
-
The KNN vector query feature is supported by Tablestore SDK for Node.js of version 5.5.0 or later. Make sure that a supported version of Tablestore SDK for Node.js is installed.
NoteFor information about the version history of Tablestore SDK for Node.js, see Version history of Tablestore SDK for Java.
-
Limits apply to the number of vector field types, dimensions, and other properties. For more information, see Search index limits.
-
The search index is partitioned on the server side. Each partition returns its own TopK nearest neighbors, and the results are then aggregated at the client node. Therefore, if a token is used to paginate through all data, the total number of rows returned depends on the number of server-side partitions.
Parameters
|
Parameter |
Required |
Description |
|
fieldName |
Yes |
The name of the vector field. |
|
topK |
Yes |
The number of nearest neighbors to return. For the maximum value, see Search index limits. Important
A larger K value increases recall but also increases query latency and cost. |
|
float32QueryVector |
Yes |
The query vector used for the similarity search. |
|
minScore |
No |
The minimum score threshold. Only rows with a score greater than this value are returned. The value must be greater than or equal to 0. Default: 0. |
|
filter |
No |
A filter that supports any combination of non-vector query conditions. |
Example
The following sample code provides an example on how to query the ten nearest neighbors of a specific vector in a data table. In this example, the value of the col_keyword column of the nearest-neighbor vectors must be equal to "0" and the value of the col_long column of the nearest-neighbor vectors must be between 0 and 50.
const tableName = "<TABLE_ANME>"
const indexName = "<SEARCH_INDEX_NAME>"
async function knnVectorQuery() {
return new Promise(function (resolve, reject) {
let params = {
tableName: tableName,
indexName: indexName,
searchQuery: {
offset: 0,
limit: 10,
query: {
queryType: TableStore.QueryType.KNN_VECTOR_QUERY,
query: {
fieldName: "col_vector",
topK: TableStore.Long.fromNumber(10),
float32QueryVector: [1.0, 1.1, 1.2, -1.3],
filter: {
queryType: TableStore.QueryType.BOOL_QUERY,
query: {
mustQueries: [
{
queryType: TableStore.QueryType.RANGE_QUERY,
query: {
fieldName: "col_long",
rangeFrom: TableStore.Long.fromNumber(0),
includeLower: true,
rangeTo: TableStore.Long.fromNumber(50),
includeUpper: true,
}
},
{
queryType: TableStore.QueryType.TERM_QUERY,
query: {
fieldName: "col_keyword",
term: "0",
}
},
],
}
},
},
},
sort: {
sorters: [
{
scoreSort: {
order: TableStore.SortOrder.SORT_ORDER_DESC // Sort the query results based on the scores in descending order.
}
}
],
},
getTotalCount: false,
},
columnToGet: {
returnType: TableStore.ColumnReturnType.RETURN_SPECIFIED,
returnNames: ["col_long", "col_keyword"]
},
timeoutMs: 10000,
}
client.search(params, function (err, data) {
if (err) {
console.log('search error:', err.toString());
reject(err);
} else {
console.log('RequestId:', data.RequestId);
for (let i = 0; i < data.searchHits.length; i++) {
let hit = data.searchHits[i]
console.log('Score:', hit.score, 'Row:', hit.row);
}
resolve(data)
}
});
})
}
knnVectorQuery();
FAQ
References
The following query types are supported by search indexes: term query, terms query, match all query, match query, match phrase query, prefix query, range query, wildcard query, Boolean query, geo query, nested query, vector query, and exists query. You can select a query type to query data based on your business requirements.
If you want to sort or paginate the rows that meet the query conditions, you can use the sorting and paging feature. For more information, see Sorting and paging.
If you want to collapse the result set based on a specific column, you can use the collapse (distinct) feature. 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, such as obtaining the extreme values, sum, and total number of rows, you can perform aggregation operations or execute SQL statements. For more information, see Aggregation and SQL query.
If you want to quickly 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.