Sub topic 7
Building and querying a Vector Search index
This chapter shows how to build a Mosaic AI Vector Search index over a Delta table and query it from Python. It assumes you can use Databricks notebooks and Delta tables. It also assumes you know what embeddings are and that you have already split your documents into chunks.
How the pieces fit
Vector Search has two parts:
- A Vector Search endpoint is the compute that hosts and serves your indexes.
- A Vector Search index stores embeddings for your chunks and answers similarity queries. It is registered in Unity Catalog, so it has a three-level name such as
main.genai.docs_index.
A Delta Sync index keeps itself in step with a source Delta table. You point it at a text column and an embedding model endpoint. Databricks computes the embeddings and updates them when the table changes.
Prepare the source table
The source table needs two things:
- A primary key column, here
id, that is unique for each chunk. - Change Data Feed enabled, so the index can see which rows changed.
Enable Change Data Feed on an existing table with SQL:
ALTER TABLE main.genai.docs_chunks
SET TBLPROPERTIES (delta.enableChangeDataFeed = true);Create the index
The code below creates a Delta Sync index. It assumes the Vector Search endpoint already exists.
from databricks.vector_search.client import VectorSearchClient
vsc = VectorSearchClient()
index = vsc.create_delta_sync_index(
endpoint_name="<vector-search-endpoint>",
index_name="main.genai.docs_index",
source_table_name="main.genai.docs_chunks",
pipeline_type="TRIGGERED",
primary_key="id",
embedding_source_column="text",
embedding_model_endpoint_name="<embedding-endpoint-name>",
)The pipeline_type setting controls when the index refreshes:
| Value | Behavior | Use when |
|---|---|---|
| `TRIGGERED` | Updates only when you start a sync | Data changes in batches and cost matters |
| `CONTINUOUS` | Updates as the table changes | Results must stay fresh |
The first build takes a few minutes. Wait for the index to report that it is ready before you query it.
Query the index
Get a handle to the index, then call similarity_search.
from databricks.vector_search.client import VectorSearchClient
index = VectorSearchClient().get_index(
endpoint_name="<vector-search-endpoint>",
index_name="main.genai.docs_index",
)
results = index.similarity_search(
query_text="How do I rotate an access token?",
columns=["id", "text"],
num_results=3,
)
for row in results["result"]["data_array"]:
print(row)Each row in data_array holds the columns you requested, in the order you listed them. The last value in each row is the similarity score. This is why the RAG code in a later chapter reads the text with row[1]. Because the index computes embeddings for you, you pass query_text and do not need to embed the query yourself.
Common errors
- Permission denied. You need privileges on the catalog and schema, plus access to the source table and the index. Chapter-level governance rules apply here as they do for any Unity Catalog asset.
- Index creation fails on the source table. Check that Change Data Feed is on and that the primary key column exists.
- Empty or stale results. On a
TRIGGEREDindex, run a sync. Also check that the index finished its initial build. - Wrong column order when reading rows. Rows follow the order of the
columnsargument, so changing that list changes the position of each value.
Exercise
You add 500 new chunks to the source table of a Delta Sync index created with pipeline_type="TRIGGERED". Queries do not return the new chunks. What is the likely cause, and how do you fix it?
Show answer
A triggered index only updates when a sync is started, so the new rows are not yet embedded or indexed. Run index.sync() (or start the sync from the UI) and wait for it to finish. Alternatively, recreate the index with pipeline_type="CONTINUOUS" if you need automatic updates.
Exercise
In similarity_search, you call columns=["text", "id"]. Which index in each result row holds the chunk text?
Show answer
Index 0. Rows follow the order of the columns list, so text comes first, followed by id, and then the similarity score.