Skip to content

Commit 5e9087d

Browse files
committed
feat: add methods to ensure index existence and load collection in Milvus service
1 parent 7260995 commit 5e9087d

File tree

1 file changed

+57
-4
lines changed

1 file changed

+57
-4
lines changed

mem4j-core/src/main/java/io/github/mem4j/vectorstores/MilvusVectorStoreService.java

Lines changed: 57 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22
* Copyright 2024-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
5-
* you may not use this file except in compliance with the License.
5+
* you may not use this file except in compliance with the Lic client.createCollection(createCollectionParam);
6+
logger.info("Milvus 集合已创建: {}", collectionName);
7+
8+
// 加载集合到内存中
9+
ensureCollectionLoaded();se.
610
* You may obtain a copy of the License at
711
*
812
* https://www.apache.org/licenses/LICENSE-2.0
@@ -22,12 +26,15 @@
2226
import io.milvus.client.MilvusServiceClient;
2327
import io.milvus.grpc.*;
2428
import io.milvus.param.ConnectParam;
29+
import io.milvus.param.IndexType;
2530
import io.milvus.param.MetricType;
2631
import io.milvus.param.R;
2732
import io.milvus.param.collection.CreateCollectionParam;
2833
import io.milvus.param.collection.DropCollectionParam;
2934
import io.milvus.param.collection.FieldType;
3035
import io.milvus.param.collection.HasCollectionParam;
36+
import io.milvus.param.collection.LoadCollectionParam;
37+
import io.milvus.param.index.CreateIndexParam;
3138
import io.milvus.param.dml.DeleteParam;
3239
import io.milvus.param.dml.InsertParam;
3340
import io.milvus.param.dml.QueryParam;
@@ -124,10 +131,12 @@ private void ensureCollectionExists() {
124131
R<Boolean> hasCollection = client.hasCollection(hasCollectionParam);
125132
if (hasCollection.getData() != null && hasCollection.getData()) {
126133
logger.info("Milvus 集合已存在: {}", collectionName);
134+
// 确保索引存在
135+
ensureIndexExists();
136+
// 确保集合已加载
137+
ensureCollectionLoaded();
127138
return;
128-
}
129-
130-
// 构建字段定义
139+
} // 构建字段定义
131140
List<FieldType> fieldsSchema = new ArrayList<>();
132141
// 主键字段
133142
fieldsSchema.add(FieldType.newBuilder()
@@ -189,13 +198,54 @@ private void ensureCollectionExists() {
189198

190199
client.createCollection(createCollectionParam);
191200
logger.info("Milvus 集合已创建: {}", collectionName);
201+
202+
// 创建向量字段的索引
203+
ensureIndexExists();
204+
205+
// 加载集合到内存中
206+
ensureCollectionLoaded();
192207
}
193208
catch (Exception e) {
194209
logger.error("确保 Milvus 集合存在时出错", e);
195210
throw new RuntimeException("创建或检查 Milvus 集合失败", e);
196211
}
197212
}
198213

214+
private void ensureIndexExists() {
215+
try {
216+
// 为向量字段创建索引
217+
CreateIndexParam indexParam = CreateIndexParam.newBuilder()
218+
.withCollectionName(collectionName)
219+
.withFieldName("vector")
220+
.withIndexType(IndexType.IVF_FLAT)
221+
.withMetricType(MetricType.COSINE)
222+
.withExtraParam("{\"nlist\":128}")
223+
.build();
224+
225+
client.createIndex(indexParam);
226+
logger.info("Milvus 向量索引已创建: {}", collectionName);
227+
}
228+
catch (Exception e) {
229+
// 如果索引已存在,Milvus会抛出异常,但这是正常的
230+
logger.debug("索引可能已存在: {}", e.getMessage());
231+
}
232+
}
233+
234+
private void ensureCollectionLoaded() {
235+
try {
236+
// 加载集合到内存中
237+
LoadCollectionParam loadParam = LoadCollectionParam.newBuilder().withCollectionName(collectionName).build();
238+
239+
client.loadCollection(loadParam);
240+
241+
logger.info("Milvus 集合已加载到内存: {}", collectionName);
242+
}
243+
catch (Exception e) {
244+
logger.error("确保 Milvus 集合加载时出错", e);
245+
throw new RuntimeException("加载 Milvus 集合失败", e);
246+
}
247+
}
248+
199249
@Override
200250
public void add(MemoryItem item) {
201251
try {
@@ -271,6 +321,9 @@ public void add(MemoryItem item) {
271321
public List<MemoryItem> search(Double[] queryEmbedding, Map<String, Object> filters, Integer limit,
272322
Double threshold) {
273323
try {
324+
// 确保集合已加载
325+
ensureCollectionLoaded();
326+
274327
// 构建搜索表达式
275328
String searchExpr = buildSearchExpression(filters);
276329

0 commit comments

Comments
 (0)