Use KNN vector query

更新时间:
复制 MD 格式

This topic explains how to use k-nearest neighbor (KNN) vector queries in the Tablestore console or with Tablestore SDKs.

API

To perform a KNN vector query, call the Search operation and set the query type to KnnVectorQuery.

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.

Procedure

Note

If you encounter issues with KNN vector queries, submit a ticket or join DingTalk group 36165029092 (Tablestore Technical Discussion Group - 3) for assistance.

You can perform a KNN vector query in the Tablestore console or by using an SDK. Before you begin, complete the following tasks:

Use the console

  1. Go to the Index Management tab.

    1. Log on to the Table Store console.

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

    3. On the Overview page, click the instance name or click Instance Management in the Actions column.

    4. On the Instance Details tab, in the Data Table List tab, click the data table name or click Index Management in the Actions column.

  2. On the Index Management tab, find the target Search Index and click Search in the Actions column.

  3. In the Search dialog box, configure the query parameters.

    1. By default, all columns are returned. To return specific columns, turn off Retrieve All Columns and enter the column names, separated by commas.

      Note

      By default, Table Store returns the primary key columns of the data table.

    2. Select a logical operator: And, Or, or Not.

      If you select And, the query returns data that meets all specified conditions. If you select Or, the query returns data that meets at least one of the specified conditions. If you select Not, the query returns data that does not meet the specified conditions.

    3. Select a vector field and click Add.

    4. Set the query type of the vector field to KNN vector query (KnnVectorQuery), and then enter the query vector and the topK value.

      Enter the vector in the format prompted on the page.

    5. By default, sorting is disabled. To sort the results by a specific field, turn on Enable Sorting, add the sort field, and configure the sort order.

    6. By default, aggregation is disabled. To perform statistical aggregation on a specific field, turn on Enable Aggregation, add the field for aggregation, and configure the aggregation settings.

  4. Click OK.

    The query results are displayed on the Index Management tab.

Use an SDK

Important

The KNN vector query feature is available in Tablestore SDK for Java 5.17.0 or later, the latest version of Tablestore SDK for Go, Tablestore SDK for Python 5.4.4 or later, and Tablestore SDK for Node.js 5.5.0 or later.

You can perform a KNN vector query by using the Java SDK, Go SDK, Python SDK, or Node.js SDK. The following example uses the Java SDK.

Important

Before you use the Java SDK to perform a KNN vector query, you must initialize the client. For more information, see Initialize a Tablestore client.

The following example retrieves the 10 nearest neighbors to a given vector, with a minimum similarity score of 0.1. The results are further filtered to rows where Col_Keyword is 'hangzhou' and Col_Long 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 K nearest neighbors.
    query.setMinScore(0.1f); // Return only rows with a score greater than 0.1.
    query.setFloat32QueryVector(new float[]{0.1f, 0.2f, 0.3f, 0.4f});
    // The nearest neighbors must also satisfy the conditions: Col_Keyword = 'hangzhou' AND Col_Long < 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 results by score.
    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()) {
        // Print the score.
        System.out.println(hit.getScore());
        // Print the row data.
        System.out.println(hit.getRow());
    }
}

FAQ

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

Related documents