ArnoChen commited on
Commit
65fd96e
·
1 Parent(s): 3d2d236

create mongodb vector index only if not exists

Browse files
Files changed (1) hide show
  1. lightrag/kg/mongo_impl.py +11 -3
lightrag/kg/mongo_impl.py CHANGED
@@ -799,7 +799,7 @@ class MongoVectorDBStorage(BaseVectorStorage):
799
  self._data = await get_or_create_collection(self.db, self._collection_name)
800
 
801
  # Ensure vector index exists
802
- await self.create_vector_index()
803
 
804
  logger.debug(f"Use MongoDB as VDB {self._collection_name}")
805
 
@@ -808,9 +808,17 @@ class MongoVectorDBStorage(BaseVectorStorage):
808
  await ClientManager.release_client(self.db)
809
  self.db = None
810
 
811
- async def create_vector_index(self):
812
  """Creates an Atlas Vector Search index."""
813
  try:
 
 
 
 
 
 
 
 
814
  search_index_model = SearchIndexModel(
815
  definition={
816
  "fields": [
@@ -822,7 +830,7 @@ class MongoVectorDBStorage(BaseVectorStorage):
822
  }
823
  ]
824
  },
825
- name="vector_knn_index",
826
  type="vectorSearch",
827
  )
828
 
 
799
  self._data = await get_or_create_collection(self.db, self._collection_name)
800
 
801
  # Ensure vector index exists
802
+ await self.create_vector_index_if_not_exists()
803
 
804
  logger.debug(f"Use MongoDB as VDB {self._collection_name}")
805
 
 
808
  await ClientManager.release_client(self.db)
809
  self.db = None
810
 
811
+ async def create_vector_index_if_not_exists(self):
812
  """Creates an Atlas Vector Search index."""
813
  try:
814
+ index_name = "vector_knn_index"
815
+
816
+ indexes = await self._data.list_search_indexes().to_list(length=None)
817
+ for index in indexes:
818
+ if index["name"] == index_name:
819
+ logger.debug("vector index already exist")
820
+ return
821
+
822
  search_index_model = SearchIndexModel(
823
  definition={
824
  "fields": [
 
830
  }
831
  ]
832
  },
833
+ name=index_name,
834
  type="vectorSearch",
835
  )
836