Redis vector sets

Introduction to Redis vector sets

Vector sets are a data type similar to sorted sets, but instead of a score, vector set elements have a string representation of a vector. Vector sets allow you to add items to a set, and then either:

  • retrieve a subset of items that are the most similar to a specified vector, or
  • retrieve a subset of items that are the most similar to the vector of an element that is already part of the vector set.

Vector sets also provide for optional filtered search. You can associate attributes with all or some elements in a vector set, and then use the FILTER option of the VSIM command to retrieve items similar to a given vector while applying simple mathematical filters to those attributes. Here's a sample filter: ".year > 1950".

The following commands are available for vector sets:

  • VADD - add an element to a vector set, creating a new set if it didn't already exist.
  • VCARD - retrieve the number of elements in a vector set.
  • VDIM - retrieve the dimension of the vectors in a vector set.
  • VEMB - retrieve the approximate vector associated with a vector set element.
  • VGETATTR - retrieve the attributes of a vector set element.
  • VINFO - retrieve metadata and internal details about a vector set, including size, dimensions, quantization type, and graph structure.
  • VLINKS - retrieve the neighbors of a specified element in a vector set; the connections for each layer of the HNSW graph.
  • VRANDMEMBER - retrieve random elements of a vector set.
  • VREM - remove an element from a vector set.
  • VSETATTR - set or replace attributes on a vector set element.
  • VSIM - retrieve elements similar to a given vector or element with optional filtering.

Examples

The following examples give an overview of how to use vector sets. For clarity, we will use a set of two-dimensional vectors that represent points in the Cartesian coordinate plane. However, in real use cases, the vectors will typically represent text embeddings and have hundreds of dimensions. See Redis for AI for more information about using text embeddings.

The points we will use are A: (1.0, 1.0), B: (-1.0, -1.0), C: (-1.0, 1.0), D: (1.0. -1.0), and E: (1.0, 0), shown in the diagram below.

Example points on the coordinate plane.

Basic operations

Start by adding the point vectors to a set called points using VADD. This also creates the vector set object. The TYPE command returns a type of vectorset for this object.

Get the number of elements in the set (also known as the cardinality of the set) using VCARD and the number of dimensions of the vectors using VDIM:

Get the coordinate values from the elements using VEMB. Note that the values will not typically be the exact values you supplied when you added the vector because quantization is applied to improve performance.

Set and retrieve an element's JSON attribute data using VSETATTR and VGETATTR. You can also pass an empty string to VSETATTR to delete the attribute data:

Remove an unwanted element with VREM

Use VSIM to rank the points in order of their vector distance from a sample point:

Find the four elements that are closest to point A and show their distance "scores":

Add some JSON attributes and use filter expressions to include them in the search:

More information

See the other pages in this section to learn more about the features and performance parameters of vector sets.

RATE THIS PAGE
Back to top ↑