From 1fc2a59e34ab26a4d9cd1f4606b411e822b63d47 Mon Sep 17 00:00:00 2001 From: 0007 <0007@qq.com> Date: Wed, 27 Aug 2025 19:57:53 +0800 Subject: [PATCH] Add File --- .../agentsflex/store/milvus/MilvusDbTest.java | 125 ++++++++++++++++++ 1 file changed, 125 insertions(+) create mode 100644 agents-flex-store/agents-flex-store-milvus/src/test/java/com/agentsflex/store/milvus/MilvusDbTest.java diff --git a/agents-flex-store/agents-flex-store-milvus/src/test/java/com/agentsflex/store/milvus/MilvusDbTest.java b/agents-flex-store/agents-flex-store-milvus/src/test/java/com/agentsflex/store/milvus/MilvusDbTest.java new file mode 100644 index 0000000..7adcc40 --- /dev/null +++ b/agents-flex-store/agents-flex-store-milvus/src/test/java/com/agentsflex/store/milvus/MilvusDbTest.java @@ -0,0 +1,125 @@ +package com.agentsflex.store.milvus; + +import com.agentsflex.core.document.Document; +import com.agentsflex.core.llm.embedding.EmbeddingModel; +import com.agentsflex.core.llm.embedding.EmbeddingOptions; +import com.agentsflex.core.store.SearchWrapper; +import com.agentsflex.core.store.VectorData; +import io.grpc.StatusRuntimeException; +import org.junit.Test; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +public class MilvusDbTest { + + /** + * Create a MilvusVectorStore with standard test configuration + */ + private MilvusVectorStore createTestStore() { + // Create configuration + MilvusVectorStoreConfig config = new MilvusVectorStoreConfig(); + config.setUri("http://localhost:19530"); + config.setUsername("root"); + config.setPassword("Milvus"); + config.setAutoCreateCollection(true); + config.setDefaultCollectionName("milve_test"); + + // Create store + MilvusVectorStore store = new MilvusVectorStore(config); + + // Add embedding model + store.setEmbeddingModel(new EmbeddingModel() { + @Override + public VectorData embed(Document document, EmbeddingOptions options) { + // Generate vector for document + VectorData vectorData = new VectorData(); + vectorData.setVector(new double[]{Math.random(), Math.random()}); + return vectorData; + } + + @Override + public int dimensions() { + return 2; + } + }); + + return store; + } + + /** + * Create test documents + */ + private List createTestDocuments() { + List list = new ArrayList<>(); + + Document doc1 = new Document(); + doc1.setId(1); + doc1.setContent("Technical document content"); + Map metadata = new HashMap<>(); + metadata.put("test1-6666", "test1-666"); + doc1.setMetadataMap(metadata); + list.add(doc1); + + Document doc2 = new Document(); + doc2.setId(2); + doc2.setContent("Lifestyle document content"); + Map metadata2 = new HashMap<>(); + metadata2.put("test2-7777", "test2-777"); + doc2.setMetadataMap(metadata2); + list.add(doc2); + + return list; + } + + + + @Test(expected = StatusRuntimeException.class) + public void testUseMivlusDb() { + MilvusVectorStore store = createTestStore(); + List documents = createTestDocuments(); + + // Store data + store.store(documents); + System.out.println("Data storage completed"); + + // Delete data + // store.delete(1, 2); + } + + @Test(expected = StatusRuntimeException.class) + public void testVectorSearch() { + MilvusVectorStore store = createTestStore(); + + // Test vector search (using text) + SearchWrapper wrapper = new SearchWrapper(); + wrapper.setText("Lifestyle"); + wrapper.setMaxResults(10); + + List results = store.search(wrapper); + System.out.println("Vector search results count: " + results.size()); + for (Document doc : results) { + System.out.println("Vector search result - ID: " + doc.getId() + + ", Score: " + doc.getScore() + ", Content: " + doc.getContent()); + } + } + + @Test(expected = StatusRuntimeException.class) + public void testNonVectorSearch() { + MilvusVectorStore store = createTestStore(); + + // Test non-vector search (condition query) + SearchWrapper wrapper = new SearchWrapper(); + wrapper.eq("id", 1); + + List results = store.search(wrapper); + System.out.println("Non-vector search results count: " + results.size()); + for (Document doc : results) { + System.out.println("Non-vector search result - ID: " + doc.getId() + + ", Content: " + doc.getContent()); + } + } + +}