Update vector search index¶
You can use the db.collection.updateSearchIndex() method to update a vector search index.
Warning
updateSearchIndex() replaces the index definition; it does not merge your changes into the existing one. Always submit the complete definition, including the fields you are not changing. Any field you omit is removed from the index.
You cannot rename an index with this method. To rename an index, drop it and create a new one with the desired name.
After you update an index, mongot rebuilds it in the background. The index continues to serve queries with the old definition until the rebuild completes. Use getSearchIndexes() to check the rebuild status.
Update a vector search index¶
db.<collection>.updateSearchIndex(
<name>,
{
<definition>
}
)
updateSearchIndex() accepts these fields:
| Field | Type | Required | Description |
|---|---|---|---|
name |
string |
Required | Name of the index to update. |
definition |
document |
Required | The complete replacement definition for the index. This replaces the existing definition; it is not merged with it. |
Add category as a filter field to products_vector_idx
Suppose products_vector_idx currently indexes the embedding field. The following operation adds category as a filter field.
db.products.updateSearchIndex(
"products_vector_idx",
{
fields: [
{
type: "vector",
path: "embedding",
numDimensions: 768,
similarity: "cosine"
},
{
type: "filter",
path: "category"
}
]
}
);
What happens under the hood
When you update the index
mongodforwards the updated definition tomongot.mongotbuilds a new version of the index in the background. The existing index remains available for queries while the rebuild is in progress.- The new index contains the
embeddingvectors and thecategoryvalues. During a filtered vector query,categorynarrows the documents considered for similarity search. It does not affect the vector similarity score. - When the rebuild finishes, the new index replaces the previous version.