diff --git a/easy-agents-store/easy-agents-store-milvus/src/main/java/com/easyagents/store/milvus/MilvusPrimaryKeySupport.java b/easy-agents-store/easy-agents-store-milvus/src/main/java/com/easyagents/store/milvus/MilvusPrimaryKeySupport.java new file mode 100644 index 0000000..6d1ff1c --- /dev/null +++ b/easy-agents-store/easy-agents-store-milvus/src/main/java/com/easyagents/store/milvus/MilvusPrimaryKeySupport.java @@ -0,0 +1,39 @@ +package com.easyagents.store.milvus; + +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.Objects; + +/** + * Milvus VarChar 主键转换工具。 + */ +final class MilvusPrimaryKeySupport { + + /** + * 禁止实例化无状态工具类。 + */ + private MilvusPrimaryKeySupport() { + } + + /** + * 将删除主键归一化为 Milvus VarChar 主键值。 + * + *

Milvus 向量存储创建的集合固定使用 VarChar 类型的 {@code id} 主键。 + * Java SDK 会根据传入值类型生成删除表达式,因此数值对象必须先转为字符串。

+ * + * @param ids 调用方提供的主键集合 + * @return 可直接传给 Milvus Java SDK 的字符串主键列表 + * @throws NullPointerException 主键集合中包含空值时抛出 + */ + static List normalize(Collection ids) { + List normalizedIds = new ArrayList(ids.size()); + for (Object id : ids) { + normalizedIds.add(Objects.requireNonNull( + id, + "Milvus primary key must not be null" + ).toString()); + } + return normalizedIds; + } +} diff --git a/easy-agents-store/easy-agents-store-milvus/src/main/java/com/easyagents/store/milvus/MilvusVectorStore.java b/easy-agents-store/easy-agents-store-milvus/src/main/java/com/easyagents/store/milvus/MilvusVectorStore.java index aed63f1..efee8fc 100644 --- a/easy-agents-store/easy-agents-store-milvus/src/main/java/com/easyagents/store/milvus/MilvusVectorStore.java +++ b/easy-agents-store/easy-agents-store-milvus/src/main/java/com/easyagents/store/milvus/MilvusVectorStore.java @@ -157,12 +157,14 @@ public class MilvusVectorStore extends DocumentStore implements AutoCloseable { } DeleteReq deleteReq = builder .collectionName(collectionName) - .ids(new ArrayList(ids)) + .ids(MilvusPrimaryKeySupport.normalize(ids)) .build(); client.delete(deleteReq); return StoreResult.success(); } catch (Exception e) { - return StoreResult.fail(); + LOG.error("Milvus delete failed. collection={}, message={}", + collectionName, e.getMessage(), e); + return StoreResult.fail(e.getMessage()); } } diff --git a/easy-agents-store/easy-agents-store-milvus/src/test/java/com/easyagents/store/milvus/MilvusPrimaryKeySupportTest.java b/easy-agents-store/easy-agents-store-milvus/src/test/java/com/easyagents/store/milvus/MilvusPrimaryKeySupportTest.java new file mode 100644 index 0000000..8efa250 --- /dev/null +++ b/easy-agents-store/easy-agents-store-milvus/src/test/java/com/easyagents/store/milvus/MilvusPrimaryKeySupportTest.java @@ -0,0 +1,42 @@ +package com.easyagents.store.milvus; + +import org.junit.Assert; +import org.junit.Test; + +import java.math.BigInteger; +import java.util.Arrays; +import java.util.List; + +/** + * {@link MilvusPrimaryKeySupport} 主键归一化测试。 + */ +public class MilvusPrimaryKeySupportTest { + + /** + * 验证数值和字符串主键都按 VarChar 类型传给 Milvus。 + */ + @Test + public void shouldNormalizeDeleteIdsAsStrings() { + List ids = MilvusPrimaryKeySupport.normalize(Arrays.asList( + new BigInteger("105257799143000107"), + 42L, + "faq-1" + )); + + Assert.assertEquals( + Arrays.asList("105257799143000107", "42", "faq-1"), + ids + ); + for (Object id : ids) { + Assert.assertTrue(id instanceof String); + } + } + + /** + * 验证空主键会被明确拒绝,避免生成无效删除表达式。 + */ + @Test(expected = NullPointerException.class) + public void shouldRejectNullDeleteId() { + MilvusPrimaryKeySupport.normalize(Arrays.asList("1", null)); + } +}