From 90b25cd0d408fa8874388eef0d9f74cd28aede31 Mon Sep 17 00:00:00 2001 From: groot Date: Tue, 3 Dec 2024 17:48:33 +0800 Subject: [PATCH] Fix a bug of V2 that dynamic values not inserted (#1210) Signed-off-by: yhmo --- .../java/io/milvus/v2/utils/SchemaUtils.java | 1 + .../v2/client/MilvusClientV2DockerTest.java | 58 +++++++++++++++++++ 2 files changed, 59 insertions(+) diff --git a/src/main/java/io/milvus/v2/utils/SchemaUtils.java b/src/main/java/io/milvus/v2/utils/SchemaUtils.java index 88be2b355..33f77dcd0 100644 --- a/src/main/java/io/milvus/v2/utils/SchemaUtils.java +++ b/src/main/java/io/milvus/v2/utils/SchemaUtils.java @@ -112,6 +112,7 @@ public static FunctionSchema convertToGrpcFunction(CreateCollectionReq.Function public static CreateCollectionReq.CollectionSchema convertFromGrpcCollectionSchema(CollectionSchema schema) { CreateCollectionReq.CollectionSchema collectionSchema = CreateCollectionReq.CollectionSchema.builder() + .enableDynamicField(schema.getEnableDynamicField()) .build(); List fieldSchemas = new ArrayList<>(); for (FieldSchema fieldSchema : schema.getFieldsList()) { diff --git a/src/test/java/io/milvus/v2/client/MilvusClientV2DockerTest.java b/src/test/java/io/milvus/v2/client/MilvusClientV2DockerTest.java index fb1696e79..39e03cafa 100644 --- a/src/test/java/io/milvus/v2/client/MilvusClientV2DockerTest.java +++ b/src/test/java/io/milvus/v2/client/MilvusClientV2DockerTest.java @@ -1978,4 +1978,62 @@ void testDocInOut() { System.out.println(result); } } + + @Test + void testDynamicField() { + String collectionName = generator.generate(10); + + client.createCollection(CreateCollectionReq.builder() + .collectionName(collectionName) + .dimension(DIMENSION) + .build()); + + List rows = new ArrayList<>(); + Gson gson = new Gson(); + for (int i = 0; i < 100; i++) { + JsonObject row = new JsonObject(); + row.addProperty("id", i); + row.add("vector", gson.toJsonTree(utils.generateFloatVector())); + row.addProperty(String.format("dynamic_%d", i), "this is dynamic value"); // this value is stored in dynamic field + rows.add(row); + } + InsertResp insertR = client.insert(InsertReq.builder() + .collectionName(collectionName) + .data(rows) + .build()); + + // query + QueryResp countR = client.query(QueryReq.builder() + .collectionName(collectionName) + .filter("") + .outputFields(Collections.singletonList("count(*)")) + .consistencyLevel(ConsistencyLevel.STRONG) + .build()); + Assertions.assertEquals(100L, (long)countR.getQueryResults().get(0).getEntity().get("count(*)")); + + GetResp getR = client.get(GetReq.builder() + .collectionName(collectionName) + .ids(Collections.singletonList(50L)) + .outputFields(Collections.singletonList("*")) + .build()); + Assertions.assertEquals(1, getR.getGetResults().size()); + QueryResp.QueryResult queryR = getR.getGetResults().get(0); + Assertions.assertTrue(queryR.getEntity().containsKey("dynamic_50")); + Assertions.assertEquals("this is dynamic value", queryR.getEntity().get("dynamic_50")); + + // search + SearchResp searchR = client.search(SearchReq.builder() + .collectionName(collectionName) + .data(Collections.singletonList(new FloatVec(utils.generateFloatVector()))) + .filter("id == 10") + .topK(10) + .outputFields(Collections.singletonList("dynamic_10")) + .build()); + List> searchResults = searchR.getSearchResults(); + Assertions.assertEquals(1, searchResults.size()); + Assertions.assertEquals(1, searchResults.get(0).size()); + SearchResp.SearchResult r = searchResults.get(0).get(0); + Assertions.assertTrue(r.getEntity().containsKey("dynamic_10")); + Assertions.assertEquals("this is dynamic value", r.getEntity().get("dynamic_10")); + } }