Skip to content

Commit 26484ca

Browse files
chocoboxxfxuxiaofeng
andauthored
DescribeCollection fieldNames/vectorFieldNames for Array<Struct> schema (#1763)
* fix(v2): include structFields in DescribeCollection fieldNames - Merge fieldsList and structArrayFieldsList when building DescribeCollectionResp.fieldNames Signed-off-by: xuxiaofeng <xuxiaofeng.work@bytedance.com> * fix(v2): include struct vector subfields in DescribeCollection vectorFieldNames - Build vectorFieldNames from both fieldsList and structArrayFieldsList - For Array<Struct>, append vector subfields as "parent[subField]" (e.g. clips[vec]) Signed-off-by: xuxiaofeng <xuxiaofeng.work@bytedance.com> --------- Signed-off-by: xuxiaofeng <xuxiaofeng.work@bytedance.com> Co-authored-by: xuxiaofeng <xuxiaofeng.work@bytedance.com>
1 parent 3b8a911 commit 26484ca

2 files changed

Lines changed: 102 additions & 2 deletions

File tree

sdk-core/src/main/java/io/milvus/v2/utils/ConvertUtils.java

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@
4040
import java.util.List;
4141
import java.util.Map;
4242
import java.util.stream.Collectors;
43+
import java.util.stream.Stream;
4344

4445
public class ConvertUtils {
4546
public static DataType toProtoDataType(io.milvus.v2.common.DataType dt) {
@@ -222,8 +223,21 @@ public DescribeCollectionResp convertDescCollectionResp(DescribeCollectionRespon
222223
.collectionSchema(SchemaUtils.convertFromGrpcCollectionSchema(response.getSchema()))
223224
.autoID(response.getSchema().getFieldsList().stream().anyMatch(FieldSchema::getAutoID))
224225
.enableDynamicField(response.getSchema().getEnableDynamicField())
225-
.fieldNames(response.getSchema().getFieldsList().stream().map(FieldSchema::getName).collect(java.util.stream.Collectors.toList()))
226-
.vectorFieldNames(response.getSchema().getFieldsList().stream().filter(fieldSchema -> ParamUtils.isVectorDataType(fieldSchema.getDataType())).map(FieldSchema::getName).collect(java.util.stream.Collectors.toList()))
226+
.fieldNames(Stream.concat(
227+
response.getSchema().getFieldsList().stream().map(FieldSchema::getName),
228+
response.getSchema().getStructArrayFieldsList().stream().map(StructArrayFieldSchema::getName))
229+
.distinct()
230+
.collect(Collectors.toList()))
231+
.vectorFieldNames(Stream.concat(
232+
response.getSchema().getFieldsList().stream()
233+
.filter(fieldSchema -> ParamUtils.isVectorDataType(fieldSchema.getDataType()))
234+
.map(FieldSchema::getName),
235+
response.getSchema().getStructArrayFieldsList().stream()
236+
.flatMap(structField -> structField.getFieldsList().stream()
237+
.filter(subField -> ParamUtils.isVectorDataType(subField.getElementType()))
238+
.map(subField -> structField.getName() + "[" + subField.getName() + "]")))
239+
.distinct()
240+
.collect(Collectors.toList()))
227241
.primaryFieldName(response.getSchema().getFieldsList().stream().filter(FieldSchema::getIsPrimaryKey).map(FieldSchema::getName).collect(java.util.stream.Collectors.toList()).get(0))
228242
.createTime(response.getCreatedTimestamp())
229243
.createUtcTime(response.getCreatedUtcTimestamp())
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package io.milvus.v2.utils;
21+
22+
import io.milvus.grpc.CollectionSchema;
23+
import io.milvus.grpc.ConsistencyLevel;
24+
import io.milvus.grpc.DataType;
25+
import io.milvus.grpc.DescribeCollectionResponse;
26+
import io.milvus.grpc.FieldSchema;
27+
import io.milvus.grpc.StructArrayFieldSchema;
28+
import io.milvus.v2.service.collection.request.AddFieldReq;
29+
import io.milvus.v2.service.collection.request.CreateCollectionReq;
30+
import io.milvus.v2.service.collection.response.DescribeCollectionResp;
31+
import org.junit.jupiter.api.Assertions;
32+
import org.junit.jupiter.api.Test;
33+
34+
public class ConvertUtilsTest {
35+
@Test
36+
void testConvertDescCollectionRespFieldNamesIncludeStructFields() {
37+
FieldSchema idField = FieldSchema.newBuilder()
38+
.setName("id")
39+
.setDataType(DataType.Int64)
40+
.setIsPrimaryKey(true)
41+
.build();
42+
43+
FieldSchema vectorField = FieldSchema.newBuilder()
44+
.setName("vector")
45+
.setDataType(DataType.FloatVector)
46+
.build();
47+
48+
CreateCollectionReq.StructFieldSchema structFieldSchema = CreateCollectionReq.StructFieldSchema.builder()
49+
.name("clips")
50+
.maxCapacity(10)
51+
.build();
52+
structFieldSchema.addField(AddFieldReq.builder()
53+
.fieldName("vec")
54+
.dataType(io.milvus.v2.common.DataType.FloatVector)
55+
.dimension(8)
56+
.build());
57+
58+
StructArrayFieldSchema rpcStructFieldSchema = SchemaUtils.convertToGrpcStructFieldSchema(structFieldSchema);
59+
60+
CollectionSchema schema = CollectionSchema.newBuilder()
61+
.setEnableDynamicField(false)
62+
.addFields(idField)
63+
.addFields(vectorField)
64+
.addStructArrayFields(rpcStructFieldSchema)
65+
.build();
66+
67+
DescribeCollectionResponse response = DescribeCollectionResponse.newBuilder()
68+
.setCollectionName("test")
69+
.setCollectionID(1L)
70+
.setDbName("default")
71+
.setSchema(schema)
72+
.setNumPartitions(1)
73+
.setCreatedTimestamp(0L)
74+
.setCreatedUtcTimestamp(0L)
75+
.setConsistencyLevel(ConsistencyLevel.Bounded)
76+
.setShardsNum(1)
77+
.build();
78+
79+
DescribeCollectionResp resp = new ConvertUtils().convertDescCollectionResp(response);
80+
Assertions.assertTrue(resp.getFieldNames().contains("id"));
81+
Assertions.assertTrue(resp.getFieldNames().contains("vector"));
82+
Assertions.assertTrue(resp.getFieldNames().contains("clips"));
83+
Assertions.assertTrue(resp.getVectorFieldNames().contains("vector"));
84+
Assertions.assertTrue(resp.getVectorFieldNames().contains("clips[vec]"));
85+
}
86+
}

0 commit comments

Comments
 (0)