All Products
Search
Document Center

Tablestore:Use KNN vector query

Last Updated:Mar 03, 2025

This topic describes how to use the k-nearest neighbor (KNN) vector query feature to query data in the Tablestore console or by using Tablestore SDKs.

API operation

You can call the Search operation and set the query type to KnnVectorQuery to use the KNN vector query feature.

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
  • A greater value of K indicates higher recall rate, query latency, and costs.

  • If the value of the topK parameter is less than the value of the limit parameter in SearchQuery, the server automatically uses the value of the limit parameter as the value of the topK parameter.

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.

Methods

Note

If an exception occurs when you use the KNN vector query feature, submit a ticket.

You can use the Tablestore console or Tablestore SDKs to use the KNN vector query feature. Before you use the KNN vector query feature to query data, make sure that the following requirements are met:

Use the Tablestore console

  1. Go to the Indexes tab.

    1. Log on to the Tablestore console.

    2. In the top navigation bar, select a resource group and a region.

    3. On the Overview page, click the name of the instance that you want to manage or click Manage Instance in the Actions column of the instance.

    4. On the Tables tab of the Instance Details tab, click the name of the data table or click Indexes in the Actions column of the data table.

  2. On the Indexes tab, find the search index that you want to use to query data and click Manage Data in the Actions column.

  3. In the Search dialog box, specify query conditions.

    1. By default, the system returns all attribute columns. To return specific attribute columns, turn off All Columns and specify the attribute columns that you want to return. Separate multiple attribute columns with commas (,).

      Note

      By default, the system returns all primary key columns of the data table.

    2. Select the And, Or, or Not logical operator based on your business requirements.

      If you select the And logical operator, data that meets the query conditions is returned. If you select the Or operator and specify a single query condition, data that meets the query condition is returned. If you select the Or logical operator and specify multiple query conditions, data that meets one of the query conditions is returned. If you select the Not logical operator, data that does not meet the query conditions is returned.

    3. Select a vector field and click Add.

    4. Set the Query Type parameter to KNN Vector Query(KnnVectorQuery) and enter the vector that you want to query and the value of the topK parameter.

      Enter a vector in the valid format as prompted.

    5. By default, the sorting feature is disabled. If you want to sort the query results based on specific fields, turn on Sort and specify the fields based on which you want to sort the query results and the sorting order.

    6. By default, the aggregation feature is disabled. If you want to collect statistics on a specific field, turn on Collect Statistics, specify the field based on which you want to collect statistics, and then configure the information that is required to collect statistics.

  4. Click OK.

    Data that meets the query conditions is displayed in the specified order on the Indexes tab.

Use Tablestore SDKs

Important

The KNN vector query feature is supported by Tablestore SDK for Java V5.17.0 and later, Tablestore SDK for Go of the latest version, Tablestore SDK for Python V5.4.4 and later, and Tablestore SDK for Node.js V5.5.0 and later.

You can use Tablestore SDK for Java, Tablestore SDK for Go, Tablestore SDK for Python, or Tablestore SDK for Node.js to use the KNN vector query feature. In this example, Tablestore SDK for Java is used.

Important

Before you use the KNN vector query feature by using Tablestore SDK for Java, you must initialize a client. For more information, see Initialize a Tablestore client.

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 conditions: the value of the Col_Keyword column is hangzhou and the value of the Col_Long column is less than 4.

private static void knnVectorQuery(SyncClient client) {
    SearchQuery searchQuery = new SearchQuery();
    KnnVectorQuery query = new KnnVectorQuery();
    query.setFieldName("Col_Vector");
    query.setTopK(10); // Return the top 10 vectors in the table that have the highest similarity as the specified vector. 
    query.setFloat32QueryVector(new float[]{0.1f, 0.2f, 0.3f, 0.4f});
    // Specify the query conditions for the top 10 vectors: the value of the Col_Keyword column is hangzhou and the value of the Col_Long column is less than 4. 
    query.setFilter(QueryBuilders.bool()
            .must(QueryBuilders.term("Col_Keyword", "hangzhou"))
            .must(QueryBuilders.range("Col_Long").lessThan(4))
    );
    searchQuery.setQuery(query);
    searchQuery.setLimit(10);
    // Sort the query results based on scores. 
    searchQuery.setSort(new Sort(Collections.singletonList(new ScoreSort())));
    SearchRequest searchRequest = new SearchRequest("<TABLE_NAME>", "<SEARCH_INDEX_NAME>", searchQuery);
    SearchRequest.ColumnsToGet columnsToGet = new SearchRequest.ColumnsToGet();
    columnsToGet.setColumns(Arrays.asList("Col_Keyword", "Col_Long"));
    searchRequest.setColumnsToGet(columnsToGet);
    // Call the Search operation. 
    SearchResponse resp = client.search(searchRequest);
    for (SearchHit hit : resp.getSearchHits()) {
        // Display the scores. 
        System.out.println(hit.getScore());
        // Display the data. 
        System.out.println(hit.getRow());
    }
}

FAQ

How do I optimize the performance of Tablestore KNN vector query?

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, fuzzy query, Boolean query, geo query, nested query, KNN vector query, and exists query. You can select query methods based on your business requirements to query data from multiple dimensions.

    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.