Upstash Documentation

Querying Vectors

2 min read

Now that our database contains data, we want to query it.

There are several ways to query the database, depending on the type of index you have configured.

Querying#

You can query your vector index for similar vectors.

Below, we will learn how to use the SDK to query Dense, Sparse, and Hybrid indexes.

You can read more about Sparse Indexes and Hybrid Indexes on our docs.

Dense Indexes#

We’ll use the query() method to instruct the SDK to query vectors from Upstash Vector.

Note

The dimension of the query vector must match the dimension of your index.

Note

The score returned from query requests is a normalized value between 0 and 1, where 1 indicates the highest similarity and 0 the lowest regardless of the similarity function used.

Sparse Indexes#

We can also use the query() method to instruct the SDK to query our Sparse index, as shown below:

Note
Sparse indexes can only be queried using sparse vectors. If you attempt to pass a regular vector in the query, the SDK will throw an exception.

Hybrid Indexes#

Hybrid indexes work the same way; they also use the query() method to query our index, as shown below:

Embedding Models#

Query Data#

If your index is configured with one of our embedding models, you can query the index using a simple string, which will be automatically converted into vector embeddings. See the example below:

Note
If your index is not configured with an embedding model, this call will throw an exception.

You can read more about Embedding Models on our docs.

Metadata Filtering#

Data stored in indexes on Upstash Vector can be populated with metadata, which can then be used for filtering vectors.

Our SDK makes it easy to filter vectors based on their metadata values. Check out the example below:

You can read more about Metadata Filtering on our docs.