From ae7a850d4e537bb97e9d1c74a93ba1fafdf87d5f Mon Sep 17 00:00:00 2001 From: ArnoChen Date: Wed, 19 Feb 2025 04:37:43 +0800 Subject: [PATCH] create mongodb vector index only if not exists --- lightrag/kg/mongo_impl.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/lightrag/kg/mongo_impl.py b/lightrag/kg/mongo_impl.py index b02c8a8c..92a0d9db 100644 --- a/lightrag/kg/mongo_impl.py +++ b/lightrag/kg/mongo_impl.py @@ -799,7 +799,7 @@ class MongoVectorDBStorage(BaseVectorStorage): self._data = await get_or_create_collection(self.db, self._collection_name) # Ensure vector index exists - await self.create_vector_index() + await self.create_vector_index_if_not_exists() logger.debug(f"Use MongoDB as VDB {self._collection_name}") @@ -808,9 +808,17 @@ class MongoVectorDBStorage(BaseVectorStorage): await ClientManager.release_client(self.db) self.db = None - async def create_vector_index(self): + async def create_vector_index_if_not_exists(self): """Creates an Atlas Vector Search index.""" try: + index_name = "vector_knn_index" + + indexes = await self._data.list_search_indexes().to_list(length=None) + for index in indexes: + if index["name"] == index_name: + logger.debug("vector index already exist") + return + search_index_model = SearchIndexModel( definition={ "fields": [ @@ -822,7 +830,7 @@ class MongoVectorDBStorage(BaseVectorStorage): } ] }, - name="vector_knn_index", + name=index_name, type="vectorSearch", )