|
2 | 2 | * Copyright 2024-2025 the original author or authors.
|
3 | 3 | *
|
4 | 4 | * 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. |
6 | 10 | * You may obtain a copy of the License at
|
7 | 11 | *
|
8 | 12 | * https://www.apache.org/licenses/LICENSE-2.0
|
|
22 | 26 | import io.milvus.client.MilvusServiceClient;
|
23 | 27 | import io.milvus.grpc.*;
|
24 | 28 | import io.milvus.param.ConnectParam;
|
| 29 | +import io.milvus.param.IndexType; |
25 | 30 | import io.milvus.param.MetricType;
|
26 | 31 | import io.milvus.param.R;
|
27 | 32 | import io.milvus.param.collection.CreateCollectionParam;
|
28 | 33 | import io.milvus.param.collection.DropCollectionParam;
|
29 | 34 | import io.milvus.param.collection.FieldType;
|
30 | 35 | import io.milvus.param.collection.HasCollectionParam;
|
| 36 | +import io.milvus.param.collection.LoadCollectionParam; |
| 37 | +import io.milvus.param.index.CreateIndexParam; |
31 | 38 | import io.milvus.param.dml.DeleteParam;
|
32 | 39 | import io.milvus.param.dml.InsertParam;
|
33 | 40 | import io.milvus.param.dml.QueryParam;
|
@@ -124,10 +131,12 @@ private void ensureCollectionExists() {
|
124 | 131 | R<Boolean> hasCollection = client.hasCollection(hasCollectionParam);
|
125 | 132 | if (hasCollection.getData() != null && hasCollection.getData()) {
|
126 | 133 | logger.info("Milvus 集合已存在: {}", collectionName);
|
| 134 | + // 确保索引存在 |
| 135 | + ensureIndexExists(); |
| 136 | + // 确保集合已加载 |
| 137 | + ensureCollectionLoaded(); |
127 | 138 | return;
|
128 |
| - } |
129 |
| - |
130 |
| - // 构建字段定义 |
| 139 | + } // 构建字段定义 |
131 | 140 | List<FieldType> fieldsSchema = new ArrayList<>();
|
132 | 141 | // 主键字段
|
133 | 142 | fieldsSchema.add(FieldType.newBuilder()
|
@@ -189,13 +198,54 @@ private void ensureCollectionExists() {
|
189 | 198 |
|
190 | 199 | client.createCollection(createCollectionParam);
|
191 | 200 | logger.info("Milvus 集合已创建: {}", collectionName);
|
| 201 | + |
| 202 | + // 创建向量字段的索引 |
| 203 | + ensureIndexExists(); |
| 204 | + |
| 205 | + // 加载集合到内存中 |
| 206 | + ensureCollectionLoaded(); |
192 | 207 | }
|
193 | 208 | catch (Exception e) {
|
194 | 209 | logger.error("确保 Milvus 集合存在时出错", e);
|
195 | 210 | throw new RuntimeException("创建或检查 Milvus 集合失败", e);
|
196 | 211 | }
|
197 | 212 | }
|
198 | 213 |
|
| 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 | + |
199 | 249 | @Override
|
200 | 250 | public void add(MemoryItem item) {
|
201 | 251 | try {
|
@@ -271,6 +321,9 @@ public void add(MemoryItem item) {
|
271 | 321 | public List<MemoryItem> search(Double[] queryEmbedding, Map<String, Object> filters, Integer limit,
|
272 | 322 | Double threshold) {
|
273 | 323 | try {
|
| 324 | + // 确保集合已加载 |
| 325 | + ensureCollectionLoaded(); |
| 326 | + |
274 | 327 | // 构建搜索表达式
|
275 | 328 | String searchExpr = buildSearchExpression(filters);
|
276 | 329 |
|
|
0 commit comments