Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions sdk-core/src/main/java/io/milvus/v2/utils/ConvertUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class ConvertUtils {
public static DataType toProtoDataType(io.milvus.v2.common.DataType dt) {
Expand Down Expand Up @@ -222,8 +223,21 @@ public DescribeCollectionResp convertDescCollectionResp(DescribeCollectionRespon
.collectionSchema(SchemaUtils.convertFromGrpcCollectionSchema(response.getSchema()))
.autoID(response.getSchema().getFieldsList().stream().anyMatch(FieldSchema::getAutoID))
.enableDynamicField(response.getSchema().getEnableDynamicField())
.fieldNames(response.getSchema().getFieldsList().stream().map(FieldSchema::getName).collect(java.util.stream.Collectors.toList()))
.vectorFieldNames(response.getSchema().getFieldsList().stream().filter(fieldSchema -> ParamUtils.isVectorDataType(fieldSchema.getDataType())).map(FieldSchema::getName).collect(java.util.stream.Collectors.toList()))
.fieldNames(Stream.concat(
response.getSchema().getFieldsList().stream().map(FieldSchema::getName),
response.getSchema().getStructArrayFieldsList().stream().map(StructArrayFieldSchema::getName))
.distinct()
.collect(Collectors.toList()))
.vectorFieldNames(Stream.concat(
response.getSchema().getFieldsList().stream()
.filter(fieldSchema -> ParamUtils.isVectorDataType(fieldSchema.getDataType()))
.map(FieldSchema::getName),
response.getSchema().getStructArrayFieldsList().stream()
.flatMap(structField -> structField.getFieldsList().stream()
.filter(subField -> ParamUtils.isVectorDataType(subField.getElementType()))
.map(subField -> structField.getName() + "[" + subField.getName() + "]")))
.distinct()
.collect(Collectors.toList()))
.primaryFieldName(response.getSchema().getFieldsList().stream().filter(FieldSchema::getIsPrimaryKey).map(FieldSchema::getName).collect(java.util.stream.Collectors.toList()).get(0))
.createTime(response.getCreatedTimestamp())
.createUtcTime(response.getCreatedUtcTimestamp())
Expand Down
86 changes: 86 additions & 0 deletions sdk-core/src/test/java/io/milvus/v2/utils/ConvertUtilsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/

package io.milvus.v2.utils;

import io.milvus.grpc.CollectionSchema;
import io.milvus.grpc.ConsistencyLevel;
import io.milvus.grpc.DataType;
import io.milvus.grpc.DescribeCollectionResponse;
import io.milvus.grpc.FieldSchema;
import io.milvus.grpc.StructArrayFieldSchema;
import io.milvus.v2.service.collection.request.AddFieldReq;
import io.milvus.v2.service.collection.request.CreateCollectionReq;
import io.milvus.v2.service.collection.response.DescribeCollectionResp;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class ConvertUtilsTest {
@Test
void testConvertDescCollectionRespFieldNamesIncludeStructFields() {
FieldSchema idField = FieldSchema.newBuilder()
.setName("id")
.setDataType(DataType.Int64)
.setIsPrimaryKey(true)
.build();

FieldSchema vectorField = FieldSchema.newBuilder()
.setName("vector")
.setDataType(DataType.FloatVector)
.build();

CreateCollectionReq.StructFieldSchema structFieldSchema = CreateCollectionReq.StructFieldSchema.builder()
.name("clips")
.maxCapacity(10)
.build();
structFieldSchema.addField(AddFieldReq.builder()
.fieldName("vec")
.dataType(io.milvus.v2.common.DataType.FloatVector)
.dimension(8)
.build());

StructArrayFieldSchema rpcStructFieldSchema = SchemaUtils.convertToGrpcStructFieldSchema(structFieldSchema);

CollectionSchema schema = CollectionSchema.newBuilder()
.setEnableDynamicField(false)
.addFields(idField)
.addFields(vectorField)
.addStructArrayFields(rpcStructFieldSchema)
.build();

DescribeCollectionResponse response = DescribeCollectionResponse.newBuilder()
.setCollectionName("test")
.setCollectionID(1L)
.setDbName("default")
.setSchema(schema)
.setNumPartitions(1)
.setCreatedTimestamp(0L)
.setCreatedUtcTimestamp(0L)
.setConsistencyLevel(ConsistencyLevel.Bounded)
.setShardsNum(1)
.build();

DescribeCollectionResp resp = new ConvertUtils().convertDescCollectionResp(response);
Assertions.assertTrue(resp.getFieldNames().contains("id"));
Assertions.assertTrue(resp.getFieldNames().contains("vector"));
Assertions.assertTrue(resp.getFieldNames().contains("clips"));
Assertions.assertTrue(resp.getVectorFieldNames().contains("vector"));
Assertions.assertTrue(resp.getVectorFieldNames().contains("clips[vec]"));
}
}
Loading