Skip to main content
RisingWave supports vector indexes to enable efficient similarity search operations. Vector indexes are specialized data structures that optimize queries involving vector distance calculations.

Creating vector indexes

Use the CREATE INDEX command with vector-specific syntax to create vector indexes. For more details, see CREATE INDEX.
Syntax

Index types

Before creating a vector index, you may create a sample table item to reference the table name and column names. Currently, we only support creating vector indexes on append-only inputs, such as append-only tables or materialized views. Therefore, we have to specify the table as append-only here:
RisingWave supports two methods when creating index:
  • FLAT index: Provides exact results by comparing the query vector against all stored vectors.
  • HNSW index: Hierarchical Navigable Small World (HNSW) index that provides approximate nearest neighbor search with better performance for large datasets.
For HNSW index, we also support specifying a query parameter ef_search by setting the session variable batch_hnsw_ef_search (the default value is 40).

Parameters

Vector distance operators

RisingWave provides specialized operators for calculating vector distances: Use vector distance operators with ORDER BY and LIMIT to perform similarity search:

Vector indexes on function expressions

You can create vector indexes on function expressions instead of raw columns. This allows you to avoid storing a separate vector column, saving storage and reducing maintenance costs.
  1. Create the table to include the input column
The embedding column is used to store the embedding generated from the description column. If you create the vector index directly from description column with function expression, you don’t have to store raw embedding in the table.
  1. Define the user-defined function (UDF)
  1. Create the vector index on the function expression
In this example, get_embedding(description) is used as the index expression. This approach avoids materializing a separate vector column in the table, which reduces storage costs and keeps the table schema simpler.

Streaming vector index lookup

Added in v2.7.0.
After creating a vector index, you can use array subqueries to define streaming jobs that look up the vector index, and expand the input with an additional array column containing the Top-N nearest rows. The query syntax is as follows:
Syntax
This query expands the input with an extra column. The data type of the expanded column is:
You can use this query to create a materialized view, sink, or sink into table, thereby defining a streaming job that performs vector index lookups. When creating such streaming jobs, FOR SYSTEM_TIME AS OF must be set to PROCTIME(). See example. The same syntax is also supported in ad-hoc queries. In that case, FOR SYSTEM_TIME AS OF is optional, and you may specify a different system time, similar to how time travel queries are defined.
  • Currently, vector index lookups are supported only for append-only inputs.
  • All columns returned by the lookup from the indexed table must be included in the vector index.

Examples

Using cosine distance type

The SQL query depends on the type of vector index you created:
  • If the vector index is built on a raw embedding column, use the raw column in your ORDER BY clause.
  • If the vector index is built using a function expression, use the same function expression in your ORDER BY clause.

Vector index lookup in streaming sinks

Suppose you have two append-only tables, items and events:
Create a vector index on the items table:
You can then create a sink to table or external system that uses the embedding from the events table to look up the vector index on items:
For each row in events, this query expands the output with a new column. The row embedding in events is used to query the vector index, retrieve a list of the Top-N nearest text values, and store the result in an array column. With these Top-N nearest texts, the related content can be further processed for downstream scenarios such as real-time RAG.