|
|
@@ -26,6 +26,8 @@ class MilvusKnowledgeIndex:
|
|
|
self._embedding_model: Any | None = None
|
|
|
self._client_lock = Lock()
|
|
|
self._embedding_model_lock = Lock()
|
|
|
+ self._collection_lock = Lock()
|
|
|
+ self._collection_ready = False
|
|
|
|
|
|
def warmup(self) -> None:
|
|
|
"""预连接 Milvus,并在后台把本地 BGE-M3 权重装入内存。"""
|
|
|
@@ -164,10 +166,22 @@ class MilvusKnowledgeIndex:
|
|
|
return cast(list[list[float]], raw_vectors)
|
|
|
|
|
|
def _ensure_collection(self, client: MilvusClient) -> None:
|
|
|
- """确保知识集合和向量索引存在;已存在时不重复创建。"""
|
|
|
+ """确保知识集合存在,并已加载到 QueryNode。"""
|
|
|
|
|
|
- if client.has_collection(self._collection_name):
|
|
|
+ if self._collection_ready:
|
|
|
return
|
|
|
+ with self._collection_lock:
|
|
|
+ if self._collection_ready:
|
|
|
+ return
|
|
|
+ if not client.has_collection(self._collection_name):
|
|
|
+ self._create_collection(client)
|
|
|
+ client.load_collection(
|
|
|
+ collection_name=self._collection_name,
|
|
|
+ timeout=30.0,
|
|
|
+ )
|
|
|
+ self._collection_ready = True
|
|
|
+
|
|
|
+ def _create_collection(self, client: MilvusClient) -> None:
|
|
|
schema = MilvusClient.create_schema(auto_id=False, enable_dynamic_field=False)
|
|
|
schema.add_field(
|
|
|
field_name="id",
|