All Products
Search
Document Center

Tablestore:KNN vector query

Last Updated:Mar 13, 2025

You can use the k-nearest neighbor (KNN) vector query feature of Tablestore to perform an approximate nearest neighbor search based on vectors. This way, you can find data items that have the highest similarity to the vector that you want to query in a large-scale dataset. This feature is suitable for various scenarios such as retrieval-augmented generation (RAG), recommendation systems, similarity detection, and natural language processing (NLP) and semantic search.

Scenarios

The KNN vector query feature is suitable for the following scenarios:

  • RAG

    RAG is an AI framework that combines retrieval capabilities with the capabilities of large language models (LLMs) to improve the accuracy of outputs generated by LLMs, especially in the field of private data or professional data. RAG is widely used in knowledge bases.

  • Recommendation system

    KNN vector query can be used by platforms such as e-commerce, social media, and video streaming platforms. For example, a platform can encode content, such as user behaviors, preferences, and content features, as vectors and store the vectors. Then, KNN vector query can be used to quickly find products, articles, or videos that match user interests. This way, the platform can provide custom recommendations to improve user experience and loyalty.

  • Similarity detection

    In content recognition scenarios, such as image recognition, video recognition, speech recognition, voiceprint recognition, and facial recognition, unstructured data is converted to vectors and stored. Then, the system uses KNN vector query to quickly find the most similar content. For example, after a user uploads an image of a commodity to an e-commerce platform, the system can quickly find the images of commodities that are similar to the uploaded image in style, color, or pattern.

  • NLP and semantic search

    In the NLP field, text is converted to vectors by using techniques such as Word2Vec and Bidirectional Encoder Representations from Transformers (BERT). Then, KNN vector query is used to understand the semantics of query statements and find the most semantically relevant content such as documents, news, and Q&A pairs. This helps improve the relevance of query results and user experience.

  • Knowledge graph and intelligent Q&A

    The nodes and relationship of nodes in knowledge graphs can be represented by vectors. Then, KNN vector query can be used to accelerate entity linking, relational inference, and the response of the intelligent Q&A system. This helps the system accurately understand the questions and provide answers to complex questions.

Benefits

Cost-effectiveness

The core engine of the KNN vector query feature uses the optimized DiskANN technology and does not need to load all index data to the memory. Compared with systems that use the hierarchical navigable small world (HNSW) algorithm, the KNN vector query feature consumes less than 10% of the memory to achieve the high recall rate and high performance of the HNSW algorithm and significantly reduces the overall costs.

Ease of use

  • As a feature of search indexes, KNN vector query is also serverless. You can use the KNN vector query feature after you create an instance in the Tablestore console, without the need to build or deploy a system.

  • The feature supports the pay-as-you-go billing method. You do not need to worry about the usage or scaling. The system supports the scale-out of storage and computing resources. KNN vector query supports up to hundreds of billions of rows, whereas other query features support up to 10 trillion rows.

  • The internal engine of the KNN vector query feature uses the query optimizer to automatically select the optimal algorithm and execution path. You can achieve a high recall rate and high performance without the need to tune numerous parameters. This significantly simplifies the use of KNN vector query and effectively shortens the business development cycle.

  • You can use the KNN vector query feature by using SQL statements, Tablestore SDKs for various programming languages, such as Java, Go, Python, and Node.js, and open source frameworks, such as LangChain, LangChain4J, and LlamaIndex.

Features

You can use the 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 to the vector that you want to query in a large-scale dataset.

KNN vector query is an out-of-the-box feature that inherits all features of search indexes. You can use the KNN vector query feature in pay-as-you-go mode, without the need to build or deploy a system. KNN vector query supports the streaming mode of search indexes. Data can be queried in near real time after the data is written to tables. KNN vector query also supports high-throughput insert, update, and delete operations. The query performance is comparable to that of systems that use the HNSW algorithm.

When you use the KNN vector query feature, you must specify the vector for which you want to query the similarity, the Vector field that you want to query, and the top K query results that have the highest similarity to the vector that you want to query. You can also use the features of other query methods in combination with KNN vector query to filter the query results.

Vector fields

Before you use the KNN vector query feature, you must specify the following information when you create a search index: the Vector field, the number of dimensions of the Vector field, the data type of the Vector field, and the algorithm that you want to use to measure the distance between vectors.

The data type of the field in the data table that is mapped to the Vector field must be String. The value of the Vector field in the search index must be an array that consists of FLOAT32 strings. The following table describes the parameters of the Vector field.

Parameter

Description

dimension

The number of dimensions of the Vector field. The maximum value is 2048. The number of dimensions of the Vector field must be the same as that of vectors generated by the vector generation system.

The array length of the Vector field is equal to the value of the dimension parameter. For example, if the value of a Vector field is [1, 5.1, 4.7, 0.08], the value of the dimension parameter for the field is 4.

Note

Only dense vectors are supported. The number of dimensions of a Vector field in a search index must be the same as that specified in the schema of the search index when the search index is created. A greater or smaller number of dimensions lead to a failure of index building.

dataType

The data type of the Vector field. Only FLOAT32 is supported. FLOAT32 does not support extreme values such as NaN and Infinite.

The data type of the Vector field must be the same as that of vectors generated by the vector generation system.

Note

If you want to use vectors of other data types, submit a ticket.

metricType

The algorithm that you want to use to measure the distance between vectors. Valid values: euclidean, cosine, and dot_product.

The distance measurement algorithm of the Vector field must be the same as that of vectors generated by the vector generation system. For more information, see the Distance measurement algorithms for vectors.

Note

The attributes of vectors vary based on the model or version used in the vector generation system. The attributes include the number of dimensions, data type, and algorithm used to measure the distance between vectors. The attributes of a Vector field in a vector retrieval system must be the same as those of vectors generated by the vector generation system. For information about the methods that you can use to generate vectors, see Generate vectors.

Distance measurement algorithms for vectors

The KNN vector query feature supports the following distance measurement algorithms for vectors: euclidean, dot_product, and cosine. The following table describes the algorithms. A greater value that is obtained by using an algorithm indicates a higher similarity between two vectors.

Metric type

Formula

Performance

Description

euclidean

image

Relatively high

Measures the shortest path between two vectors in a multi-dimensional space. The euclidean distance algorithm in Tablestore does not perform the final square root calculation to achieve better performance. A greater value that is obtained by using the euclidean distance algorithm indicates a higher similarity between two vectors.

dot_product

image

Highest

Multiplies the corresponding coordinates of two vectors of the same dimension and adds the products. A greater value that is obtained by using the dot product algorithm indicates a higher similarity between two vectors.

Float32 vectors must be normalized before they are written to tables. For example, you can use the L2 norm to normalize Float32 vectors. If you do not normalize Float32 vectors before they are written to tables, you may encounter issues such as inaccurate query results, slow construction of vector indexes, and poor query performance. For more information, see Appendix 2: Sample vector normalization.

cosine

image

Relatively low

Calculates the cosine of the angle between two vectors in a vector space. A greater value that is obtained by using the cosine similarity algorithm indicates a higher similarity between two vectors. In most cases, the algorithm is used to calculate the similarity between text data.

If 0 is used as a divisor, the cosine similarity cannot be calculated because it does not allow 0 to be used as a divisor. Therefore, the sum of squares of Float32 vectors cannot be 0.

The process to calculate cosine similarity is complex. We recommend that you normalize vectors before you write data to tables and use the dot product algorithm to measure the distance between vectors. For more information, see Appendix 2: Sample vector normalization.

Usage notes

When you use the KNN vector query feature, take note of the following items:

  • 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 neighbors nearest 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.

  • The KNN vector query feature is supported in the following regions: China (Hangzhou), China (Shanghai), China (Qingdao), China (Beijing), China (Zhangjiakou), China (Ulanqab), China (Shenzhen), China (Guangzhou), China (Chengdu), China (Hong Kong), Japan (Tokyo), Singapore, Malaysia (Kuala Lumpur), Indonesia (Jakarta)), Philippines (Manila), Thailand (Bangkok), Germany (Frankfurt), UK (London), US (Virginia), US (Silicon Valley), and SAU (Riyadh - Partner Region).

Procedure

  1. Generate vectors and write data to Tablestore. For more information, see Generate vectors.

    Use open source models to convert Tablestore data into vectors and store the vectors.

  2. Create a search index and specify a Vector field. For more information, see Create a search index.

    When you create a search index, specify the data type of the Vector field, the number of dimensions of the Vector field, and the algorithm that you want to use to measure the distance between vectors.

  3. Use the KNN vector query feature to query data. For more information, see Use KNN vector query.

Billing rules

During the public preview, you are not charged for the billable items specific to the KNN vector query feature when you use the feature. You are charged for other billable items based on the existing billing rules.

When you use a search index to query data, you are charged for the read throughput that is consumed. For more information, see Billable items of search indexes.

Appendix 1: Combination of KNN vector query and Boolean query

You can use KNN vector query and Boolean query in various combinations. The query performance varies based on the combination that you use. In the example in this section, a small amount of data meets the filter conditions.

In this example, 0.1 billion images are stored in a table and 50,000 images belong to User A. Among the 50,000 images, 50 images are stored in the previous seven days. User A wants to search for 10 images that have the highest similarity to the specified image among the 50 images. The following table describes two common combinations of KNN vector query and Boolean query that User A can use to meet the query requirements.

Combination

Query condition

Description

Boolean query in the filter of KNN vector query

image

The rows that meet the query conditions of KNN vector query are the top K rows that meet the query conditions of Boolean query. The top K rows have the highest similarity to the vector that User A wants to query. The number of rows in the response to SearchRequest is determined based on the value of the Size parameter. The Size parameter specifies the number of rows that User A wants to return in order of similarity from high to low among the top K rows.

When this combination is used, a filter of KNN vector query is used to obtain all images of User A that are stored in the previous seven days, which are 50 images. Then, the top 10 images that have the highest similarity to the image that User A wants to query among the 50 images are found and returned to User A.

KNN vector query in Boolean query

image

Each subquery condition of Boolean query is matched first, and then the intersection of the results of all subqueries is calculated.

When this combination is used, KNN vector query returns the top 500 images that have the highest similarity to the image that User A wants to query among 0.1 billion images. Then, a term query and a range query are performed to query 10 images of User A that are stored in the previous seven days among the 500 images. The top 500 images may not include all 50 images of User A that are stored in the previous seven days. When User A uses this combination, User A may fail to obtain 10 images that have the highest similarity to the specified image among the 50 images. In extreme cases, User A may obtain no images.

Appendix 2: Sample vector normalization

The following sample code provides an example on how to normalize vectors:

  public static float[] l2normalize(float[] v, boolean throwOnZero) {
    double squareSum = 0.0f;
    int dim = v.length;
    for (float x : v) {
      squareSum += x * x;
    }
    if (squareSum == 0) {
      if (throwOnZero) {
        throw new IllegalArgumentException("can't normalize a zero-length vector");
      } else {
        return v;
      }
    }
    double length = Math.sqrt(squareSum);
    for (int i = 0; i < dim; i++) {
      v[i] /= length;
    }
    return v;
  }