本指南将指导您使用所选的 SDK 创建和管理 Collections。
在 Milvus 中,您可以将向量嵌入存储在 Collections 中。一个 Collections 中的所有向量嵌入都具有相同的维度和距离度量相似性。
Milvus Collections 支持动态字段(即 Schema 中未预定义的字)和主键的自动递增。
为了适应不同的偏好,Milvus 提供了两种创建 Collections 的方法。其中一种提供快速设置,其他则允许对 Collections 模式和索引参数进行详细定制。
此外,您还可以在必要时查看、加载、释放和删除集合。
您可以通过以下任一方式创建 Collections:
在人工智能行业大跃进的背景下,大多数开发人员只需要一个简单而又充满活力的 Collections 就可以开始使用了。Milvus 只需三个参数就能快速设置这样的 Collections:
要创建的 Collections 名称、
要插入的向量 Embeddings 的维度,以及
用于衡量向量嵌入之间相似性的度量类型。
from pymilvus import MilvusClient, DataType
client = MilvusClient(
uri="http://localhost:19530"
)
client.create_collection(
collection_name="quick_setup" ,
dimension=5
)
res = client.get_load_state(
collection_name="quick_setup"
)
print (res)
import io.milvus.v2.client.ConnectConfig;
import io.milvus.v2.client.MilvusClientV2;
import io.milvus.v2.service.collection.request.GetLoadStateReq;
import io.milvus.v2.service.collection.request.CreateCollectionReq;
String CLUSTER_ENDPOINT = "http://localhost:19530" ;
ConnectConfig connectConfig = ConnectConfig.builder()
.uri(CLUSTER_ENDPOINT)
.build();
MilvusClientV2 client = new MilvusClientV2 (connectConfig);
CreateCollectionReq quickSetupReq = CreateCollectionReq.builder()
.collectionName("quick_setup" )
.dimension(5 )
.build();
client.createCollection(quickSetupReq);
GetLoadStateReq quickSetupLoadStateReq = GetLoadStateReq.builder()
.collectionName("quick_setup" )
.build();
Boolean res = client.getLoadState(quickSetupLoadStateReq);
System.out.println(res);
address = "http://localhost:19530"
client = new MilvusClient ({address});
let res = await client.createCollection ({
collection_name : "quick_setup" ,
dimension : 5 ,
});
console .log (res.error_code )
res = await client.getLoadState ({
collection_name : "quick_setup"
})
console .log (res.state )
import (
"context"
"fmt"
"log"
"time"
milvusClient "github.com/milvus-io/milvus-sdk-go/v2/client"
"github.com/milvus-io/milvus-sdk-go/v2/entity"
)
func main () {
ctx := context.Background()
ctx, cancel := context.WithTimeout(ctx, 2 *time.Second)
defer cancel()
client, err := milvusClient.NewClient(ctx, milvusClient.Config{
Address: "localhost:19530" ,
})
if err != nil {
log.Fatal("failed to connect to milvus:" , err.Error())
}
defer client.Close()
err = client.NewCollection(ctx, "quick_setup" , 5 )
if err != nil {
log.Fatal("failed to create collection:" , err.Error())
}
stateLoad, err := client.GetLoadState(context.Background(), "quick_setup" , []string {})
if err != nil {
log.Fatal("failed to get load state:" , err.Error())
}
fmt.Println(stateLoad)
}
$ export MILVUS_URI="localhost:19530"
$ curl -X POST "http://${MILVUS_URI} /v2/vectordb/collections/create" \
-H "Content-Type: application/json" \
-d '{
"collectionName": "quick_setup",
"dimension": 5
}'
$ curl -X POST "http://${MILVUS_URI} /v2/vectordb/collections/get_load_state" \
-H "Content-Type: application/json" \
-d '{
"collectionName": "quick_setup"
}'
上述代码生成的 Collections 只包含两个字段:id
(作为主键)和vector
(作为向量字段),默认情况下启用auto_id
和enable_dynamic_field
设置。
从提供的代码中自动索引和加载的 Collections 可以立即插入数据。
与其让 Milvus 为你的 Collections 决定几乎一切,你可以自己决定 Collections 的Schema 和索引参数 。
Schema 定义了 Collections 的结构。在 Schema 中,您可以选择启用或禁用enable_dynamic_field
、添加预定义字段以及为每个字段设置属性。有关概念和可用数据类型的详细解释,请参阅Schema Explained 。
要设置模式,可使用entity.NewSchema()
创建模式对象,使用schema.WithField()
为模式添加字段。
schema = MilvusClient.create_schema(
auto_id=False ,
enable_dynamic_field=True ,
)
schema.add_field(field_name="my_id" , datatype=DataType.INT64, is_primary=True )
schema.add_field(field_name="my_vector" , datatype=DataType.FLOAT_VECTOR, dim=5 )
import io.milvus.v2.common.DataType;
import io.milvus.v2.service.collection.request.CreateCollectionReq;
CreateCollectionReq.CollectionSchema schema = client.createSchema();
schema.addField(AddFieldReq.builder()
.fieldName("my_id" )
.dataType(DataType.Int64)
.isPrimaryKey(true )
.autoID(false )
.build());
schema.addField(AddFieldReq.builder()
.fieldName("my_vector" )
.dataType(DataType.FloatVector)
.dimension(5 )
.build());
const fields = [
{
name : "my_id" ,
data_type : DataType .Int64 ,
is_primary_key : true ,
auto_id : false
},
{
name : "my_vector" ,
data_type : DataType .FloatVector ,
dim : 5
},
]
schema := entity.NewSchema()
schema.WithField(
entity.NewField().
WithName("my_id" ).
WithDataType(entity.FieldTypeInt64).
WithIsPrimaryKey(false ).
WithIsAutoID(true )).
WithField(
entity.NewField().
WithName("my_vector" ).
WithDataType(entity.FieldTypeFloatVector).
WithDim(5 ))
export fields='[{ \
"fieldName": "my_id", \
"dataType": "Int64", \
"isPrimary": true \
}, \
{ \
"fieldName": "my_vector", \
"dataType": "FloatVector", \
"elementTypeParams": { \
"dim": 5 \
} \
}]'
参数
参数
auto_id
确定主字段是否自动递增。 设置为True 时 ,主字段将自动递增。在这种情况下,主字段不应包含在要插入的数据中,以免出错。自动生成的 ID 有固定长度,不能更改。
enable_dynamic_field
决定如果插入到目标 Collections 的数据包括未在 Collections 模式中定义的字段,Milvus 是否将未定义字段的值保存在动态字段中。 设置为 "true "时,Milvus 将创建一个名为$meta 的字段,用于保存插入数据中的任何未定义字段及其值。
field_name
字段的名称。
datatype
字段的数据类型。有关可用数据类型的列表,请参阅数据类型 。
is_primary
当前字段是否为集合中的主字段。 每个集合只有一个主字段。主字段应为DataType.INT64 类型或DataType.VARCHAR 类型。
dim
向量 Embeddings 的维数。 对于DataType.FLOAT_VECTOR 、DataType .BINARY _VECTOR 、DataType .FLOAT16_VECTOR 或DataType.BFLOAT16_VECTOR 类型的字段,这是必填项。如果使用DataType.SPARSE_FLOAT_VECTOR ,请省略此参数。
参数
说明
fieldName
字段的名称。
dataType
字段的数据类型。有关可用数据类型的列表,请参阅DataType 。
isPrimaryKey
当前字段是否为集合中的主字段。 每个集合只有一个主字段。主字段应为DataType.Int64 类型或DataType.VarChar 类型。
autoID
是否允许主字段自动递增。 设置为true 时 ,主字段将自动递增。在这种情况下,主字段不应包含在要插入的数据中,以免出错。
dimension
向量 Embeddings 的维数。 对于DataType.FloatVector 、DataType .BinaryVector 、DataType .Float16Vector 或DataType.BFloat16Vector 类型的字段,此项为必填项。
参数
字段名称。
name
字段的名称。
data_type
字段的数据类型。有关所有可用数据类型的枚举,请参阅DataType 。
is_primary_key
当前字段是否为集合中的主字段。 每个集合只有一个主字段。主字段应为DataType.INT64 类型或DataType.VARCHAR 类型。
auto_id
主字段是否会在数据插入此 Collections 时自动递增。 默认值为 "假 "。设置为True 时 ,主字段将自动递增。如果需要使用自定义 Schema 设置 Collections,请跳过该参数。
dim
保存向量嵌入的 Collections 字段的维度。 该值应为大于 1 的整数,通常由用于生成向量嵌入的模型决定。
参数
描述
WithName()
字段的名称。
WithDataType()
字段的数据类型。
WithIsPrimaryKey()
当前字段是否为集合中的主字段。 每个集合只有一个主字段。主字段应为entity.FieldTypeInt64 类型或entity.FieldTypeVarChar 类型。
WithIsAutoID()
主字段是否会在数据插入此 Collections 时自动递增。 默认值为false 。设置为true 时 ,主字段将自动递增。如果需要使用自定义 Schema 设置 Collections,请跳过该参数。
WithDim()
保存向量嵌入的 Collections 字段的维度。 该值应为大于 1 的整数,通常由用于生成向量嵌入的模型决定。
参数
描述
fieldName
要在目标 Collections 中创建的字段名称。
dataType
字段值的数据类型。
isPrimary
当前字段是否为主字段。将其设置为True
后,当前字段就是主字段。
elementTypeParams
额外字段参数。
dim
FloatVector 或 BinaryVector 字段的可选参数,用于确定向量维度。
索引参数决定了 Milvus 如何在 Collections 中组织数据。您可以通过调整metric_type
和index_type
来定制特定字段的索引过程。对于向量字段,您可以根据所处理的向量类型,灵活选择COSINE
,L2
,IP
,HAMMING
或JACCARD
作为metric_type
。更多信息,请参阅 "相似度指标" 。
# 3.3. Prepare index parameters
index_params = client.prepare_index_params()
# 3.4. Add indexes
index_params.add_index(
field_name="my_id" ,
index_type="STL_SORT"
)
index_params.add_index(
field_name="my_vector" ,
index_type="IVF_FLAT" ,
metric_type="IP" ,
params ={ "nlist" : 128 }
)
import io.milvus.v2.common.IndexParam;
IndexParam indexParamForIdField = IndexParam.builder()
.fieldName("my_id" )
.indexType(IndexParam.IndexType.STL_SORT)
.build();
IndexParam indexParamForVectorField = IndexParam.builder()
.fieldName("my_vector" )
.indexType(IndexParam.IndexType.IVF_FLAT)
.metricType(IndexParam.MetricType.L2)
.extraParams(Map.of("nlist" , 1024 ))
.build();
List<IndexParam> indexParams = new ArrayList <>();
indexParams.add(indexParamForIdField);
indexParams.add(indexParamForVectorField);
const index_params = [{
field_name: "my_id" ,
index_type: "STL_SORT"
},{
field_name: "my_vector" ,
index_type: "IVF_FLAT" ,
metric_type: "IP" ,
params : { nlist: 1024 }
}]
idxID := entity.NewScalarIndexWithType(entity.Sorted)
idxVector, err := entity.NewIndexIvfFlat(entity.IP, 1024 )
if err != nil {
log.Fatal("failed to new index:" , err.Error())
}
export indexParams='[{ \
"fieldName": "my_id", \
"indexName": "my_id", \
"params": { \
"index_type": "SLT_SORT" \
} \
}, { \
"fieldName": "my_vector", \
"metricType": "COSINE", \
"indexName": "my_vector", \
"params": { \
"index_type": "IVF_FLAT", \
"nlist": 1024 \
} \
}]'
参数
说明
field_name
应用此对象的目标文件名称。
index_type
用于在特定字段中排列数据的算法名称。有关适用算法,请参阅内存索引 和磁盘索引 。
metric_type
用于衡量向量间相似性的算法。可能的值有IP 、L2 、COSINE 、JACCARD 、HAMMING 。只有指定字段为向量字段时才可用。更多信息,请参阅Milvus 支持的索引 。
params
指定索引类型的微调参数。有关可能的键和值范围的详细信息,请参阅内存索引 。
参数
说明
fieldName
应用此 IndexParam 对象的目标字段的名称。
indexType
用于在特定字段中排列数据的算法名称。有关适用算法,请参阅内存索引 和磁盘索引 。
metricType
索引使用的距离度量。可能的值有IP 、L2 、COSINE 、JACCARD 、HAMMING 。
extraParams
额外的索引参数。有关详情,请参阅内存索引 和磁盘索引 。
参数
说明
field_name
要创建索引的目标字段的名称。
index_type
用于在特定字段中排列数据的算法名称。有关适用算法,请参阅内存索引 和磁盘索引 。
metric_type
用于衡量向量间相似性的算法。可能的值有IP 、L2 、COSINE 、JACCARD 、HAMMING 。只有指定字段为向量字段时才可用。更多信息,请参阅Milvus 支持的索引 。
params
指定索引类型的微调参数。有关可能的键和值范围的详细信息,请参阅内存索引 。
参数
说明
index_type
用于在特定字段中排列数据的算法名称。有关适用算法,请参阅内存索引 和磁盘索引 。
metric_type
用于衡量向量间相似性的算法。可能的值有IP 、L2 、COSINE 、JACCARD 、HAMMING 。只有当指定字段是向量字段时才可用。更多信息,请参阅Milvus 支持的索引 。
nlist
簇单位数。簇单位用于 Milvus 中基于 IVF(反转文件)的索引。对于 IVF_FLAT,索引将向量数据划分为 "nlist "簇单位,然后比较目标输入向量与每个簇中心之间的距离1。必须介于 1 和 65536 之间。
参数
说明
fieldName
要创建索引的目标字段的名称。
indexName
要创建的索引的名称。默认值为目标字段名称。
metricType
用于衡量向量间相似性的算法。可能的值有IP 、L2 、COSINE 、JACCARD 、HAMMING 。只有指定字段为向量字段时才可用。更多信息,请参阅Milvus 支持的索引 。
params
索引类型和相关设置。详情请参阅内存索引 。
params.index_type
要创建的索引类型。
params.nlist
群集单元数。这适用于与 IVF 相关的索引类型。
上面的代码片段演示了如何分别为向量场和标量场设置索引参数。对于向量场,同时设置度量类型和索引类型。对于标量字段,只需设置索引类型。建议为向量场和任何经常用于筛选的标量场创建索引。
你可以选择分别创建 Collections 和索引文件,或者在创建 Collections 时同时加载索引。
创建集合时同时加载索引。
client.create_collection(
collection_name="customized_setup_1" ,
schema=schema,
index_params=index_params
)
time.sleep(5 )
res = client.get_load_state(
collection_name="customized_setup_1"
)
print (res)
import io.milvus.v2.service.collection.request.CreateCollectionReq;
import io.milvus.v2.service.collection.request.GetLoadStateReq;
CreateCollectionReq customizedSetupReq1 = CreateCollectionReq.builder()
.collectionName("customized_setup_1" )
.collectionSchema(schema)
.indexParams(indexParams)
.build();
client.createCollection(customizedSetupReq1);
GetLoadStateReq customSetupLoadStateReq1 = GetLoadStateReq.builder()
.collectionName("customized_setup_1" )
.build();
res = client.getLoadState(customSetupLoadStateReq1);
System.out.println(res);
res = await client.createCollection ({
collection_name : "customized_setup_1" ,
fields : fields,
index_params : index_params,
})
console .log (res.error_code )
res = await client.getLoadState ({
collection_name : "customized_setup_1"
})
console .log (res.state )
$ curl -X POST "http://${MILVUS_URI} /v2/vectordb/collections/create" \
-H "Content-Type: application/json" \
-d '{
"collectionName": "customized_setup_1",
"schema": {
"autoId": false,
"enabledDynamicField": false,
"fields": [
{
"fieldName": "my_id",
"dataType": "Int64",
"isPrimary": true
},
{
"fieldName": "my_vector",
"dataType": "FloatVector",
"elementTypeParams": {
"dim": "5"
}
}
]
},
"indexParams": [
{
"fieldName": "my_vector",
"metricType": "COSINE",
"indexName": "my_vector",
"params": {
"index_type": "IVF_FLAT",
"nlist": "1024"
}
},
{
"fieldName": "my_id",
"indexName": "my_id",
"params": {
"index_type": "STL_SORT"
}
}
]
}'
$ curl -X POST "http://${MILVUS_URI} /v2/vectordb/collections/get_load_state" \
-H "Content-Type: application/json" \
-d '{
"collectionName": "customized_setup_1"
}'
上面创建的 Collection 会自动加载。要了解加载和释放集合的更多信息,请参阅加载和释放集合 。
分别创建 Collections 和索引文件。
client.create_collection(
collection_name="customized_setup_2" ,
schema=schema,
)
res = client.get_load_state(
collection_name="customized_setup_2"
)
print (res)
CreateCollectionReq customizedSetupReq2 = CreateCollectionReq.builder()
.collectionName("customized_setup_2" )
.collectionSchema(schema)
.build();
client.createCollection(customizedSetupReq2);
res = await client.createCollection ({
collection_name : "customized_setup_2" ,
fields : fields,
})
console .log (res.error_code )
res = await client.getLoadState ({
collection_name : "customized_setup_2"
})
console .log (res.state )
schema.CollectionName = "customized_setup_2"
client.CreateCollection(ctx, schema, entity.DefaultShardNumber)
stateLoad, err := client.GetLoadState(context.Background(), "customized_setup_2" , []string {})
if err != nil {
log.Fatal("failed to get load state:" , err.Error())
}
fmt.Println(stateLoad)
$ curl -X POST "http://${MILVUS_URI} /v2/vectordb/collections/create" \
-H "Content-Type: application/json" \
-d '{
"collectionName": "customized_setup_2",
"schema": {
"autoId": false,
"enabledDynamicField": false,
"fields": [
{
"fieldName": "my_id",
"dataType": "Int64",
"isPrimary": true
},
{
"fieldName": "my_vector",
"dataType": "FloatVector",
"elementTypeParams": {
"dim": "5"
}
}
]
}
}'
$ curl -X POST "http://${MILVUS_URI} /v2/vectordb/collections/get_load_state" \
-H "Content-Type: application/json" \
-d '{
"collectionName": "customized_setup_2"
}'
上面创建的 Collection 不会自动加载。您可以按如下方式为 Collections 创建索引。以单独的方式为 Collection 创建索引不会自动加载 Collection。有关详细信息,请参阅加载和释放 Collections 。
参数
说明
collection_name
Collection 的名称。
schema
此 Collection 的 Schema。 设置为 "无 " 表示将使用默认设置创建此 Collection。 要使用自定义Schema 设置 Collection,需要创建一个CollectionSchema 对象并在此处引用它。在这种情况下,Milvus 会忽略请求中携带的所有其他 Schema 相关设置。
index_params
在此 Collections 中建立向量字段索引的参数。要使用自定义 Schema 设置集合并自动将集合加载到内存中,需要创建一个 IndexParams 对象并在此处引用。 至少应为此集合中的向量字段添加一个索引。如果希望稍后再设置索引参数,也可以跳过此参数。
参数
说明
collectionName
Collections 的名称。
collectionSchema
此 Collection 的 Schema。 留空表示将以默认设置创建此 Collection。要使用自定义模式设置集合,需要创建一个CollectionSchema 对象并在此处引用。
indexParams
在此 Collection 中建立向量场索引的参数。要使用自定义 Schema 设置集合并自动将集合加载到内存中,需要创建一个包含 IndexParam 对象列表的IndexParams 对象,并在此处引用该对象。
参数
说明
collection_name
Collections 的名称。
fields
Collections 中的字段。
index_params
要创建的 Collections 的索引参数。
参数
说明
schema.CollectionName
Collections 的名称。
schema
此 Collections 的 Schema。
index_params
要创建的 Collections 的索引参数。
参数
说明
collectionName
Collections 的名称。
schema
Schema 负责组织目标 Collections 中的数据。一个有效的 Schema 应该有多个字段,其中必须包括一个主键、一个向量字段和几个标量字段。
schema.autoID
是否允许主字段自动递增。设置为 True 时,主字段将自动递增。在这种情况下,主字段不应包含在要插入的数据中,以免出错。在 is_primary 设置为 True 的字段中设置此参数。
schema.enableDynamicField
是否允许使用保留的 $meta 字段来保存键值对中的非 Schema 定义字段。
fields
字段对象列表。
fields.fieldName
要在目标 Collections 中创建的字段名称。
fields.dataType
字段值的数据类型。
fields.isPrimary
当前字段是否为主字段。设置为 True 时,当前字段将成为主字段。
fields.elementTypeParams
额外字段参数。
fields.elementTypeParams.dim
FloatVector 或 BinaryVector 字段的可选参数,用于确定向量维度。
上面创建的 Collection 不会自动加载。你可以按以下方法为 Collections 创建索引。以单独方式为集合创建索引不会自动加载集合。有关详细信息,请参阅加载和释放集合 。
client.create_index(
collection_name="customized_setup_2" ,
index_params=index_params
)
res = client.get_load_state(
collection_name="customized_setup_2"
)
print (res)
CreateIndexReq createIndexReq = CreateIndexReq.builder()
.collectionName("customized_setup_2" )
.indexParams(indexParams)
.build();
client.createIndex(createIndexReq);
GetLoadStateReq customSetupLoadStateReq2 = GetLoadStateReq.builder()
.collectionName("customized_setup_2" )
.build();
res = client.getLoadState(customSetupLoadStateReq2);
System.out.println(res);
res = await client.createIndex ({
collection_name : "customized_setup_2" ,
field_name : "my_vector" ,
index_type : "IVF_FLAT" ,
metric_type : "IP" ,
params : { nlist : 1024 }
})
res = await client.getLoadState ({
collection_name : "customized_setup_2"
})
console .log (res.state )
client.CreateIndex(ctx, "customized_setup_2" , "my_id" , idxID, false )
client.CreateIndex(ctx, "customized_setup_2" , "my_vector" , idxVector, false )
stateLoad, err = client.GetLoadState(context.Background(), "customized_setup_2" , []string {})
if err != nil {
log.Fatal("failed to get load state:" , err.Error())
}
fmt.Println(stateLoad)
$ curl -X POST "http://${MILVUS_URI} /v2/vectordb/indexes/create" \
-H "Content-Type: application/json" \
-d '{
"collectionName": "customized_setup_2",
"indexParams": [
{
"metricType": "L2",
"fieldName": "my_vector",
"indexName": "my_vector",
"indexConfig": {
"index_type": "IVF_FLAT",
"nlist": "1024"
}
}
]
}'
$ curl -X POST "http://${MILVUS_URI} /v2/vectordb/collections/get_load_state" \
-H "Content-Type: application/json" \
-d '{
"collectionName": "customized_setup_2"
}'
参数
说明
collection_name
Collections 的名称。
index_params
IndexParams 对象,包含一个IndexParam 对象列表。
参数
说明
collectionName
Collections 名称。
indexParams
IndexParam 对象列表。
参数
说明
collection_name
Collections 的名称。
field_name
要创建索引的字段名称。
index_type
用于在特定字段中排列数据的算法名称。有关适用算法,请参阅内存索引 和磁盘索引 。
metric_type
用于衡量向量间相似性的算法。可能的值有IP 、L2 、COSINE 、JACCARD 、HAMMING 。只有指定字段为向量字段时才可用。更多信息,请参阅Milvus 支持的索引 。
params
指定索引类型的微调参数。有关可能的键和值范围的详细信息,请参阅内存索引 。
参数
说明
collName
Collections 的名称。
fieldName
要创建索引的字段名称。
idx
用于在特定字段中排列数据的算法名称。有关适用算法,请参阅内存索引 和磁盘索引 。
async
此操作是否异步。
opts
指定索引类型的微调参数。可以在此请求中包含多个 `entity.IndexOption`。有关可能的键和值范围的详细信息,请参阅内存索引 。
参数
集合名称。
collectionName
Collections 的名称。
indexParams
要创建的 Collection 的索引参数。
indexParams.metricType
用于建立索引的相似度度量类型。默认值为 COSINE。
indexParams.fieldName
要创建索引的目标字段的名称。
indexParams.indexName
要创建的索引的名称,值默认为目标字段名称。
indexParams.indexConfig.index_type
要创建的索引类型。
indexParams.indexConfig.nlist
群集单元数。这适用于与 IVF 相关的索引类型。
res = client.describe_collection(
collection_name="customized_setup_2"
)
print (res)
import io.milvus.v2.service.collection.request.DescribeCollectionReq;
import io.milvus.v2.service.collection.response.DescribeCollectionResp;
// 4. View collections
DescribeCollectionReq describeCollectionReq = DescribeCollectionReq.builder()
.collectionName("customized_setup_2" )
.build();
DescribeCollectionResp describeCollectionRes = client.describeCollection(describeCollectionReq);
System.out.println(JSONObject.toJSON(describeCollectionRes));
// Output:
// {
// "createTime" : 449005822816026627,
// "collectionSchema" : {"fieldSchemaList" : [
// {
// "autoID" : false ,
// "dataType" : "Int64" ,
// "name" : "my_id" ,
// "description" : "" ,
// "isPrimaryKey" : true ,
// "maxLength" : 65535,
// "isPartitionKey" : false
// },
// {
// "autoID" : false ,
// "dataType" : "FloatVector" ,
// "name" : "my_vector" ,
// "description" : "" ,
// "isPrimaryKey" : false ,
// "dimension" : 5,
// "maxLength" : 65535,
// "isPartitionKey" : false
// }
// ]},
// "vectorFieldName" : ["my_vector" ],
// "autoID" : false ,
// "fieldNames" : [
// "my_id" ,
// "my_vector"
// ],
// "description" : "" ,
// "numOfPartitions" : 1,
// "primaryFieldName" : "my_id" ,
// "enableDynamicField" : true ,
// "collectionName" : "customized_setup_2"
// }
res = await client.describeCollection ({
collection_name : "customized_setup_2"
})
console .log (res)
res, err := client.DescribeCollection(ctx, "customized_setup_2" )
if err != nil {
log.Fatal("failed to describe collection:" , err.Error())
}
fmt.Printf("ConsistencyLevel: %v\nID: %v\nLoaded: %v\nName: %v\nPhysicalChannels: %v\nProperties: %v\nSchemaField1: %v\nSchemaField2: %v\nShardNum: %v\nVirtualChannels: %v\nSchemaAutoID: %v\nSchemaCollectionName: %v\nSchemaDescription: %v" ,
res.ConsistencyLevel, res.ID, res.Loaded, res.Name, res.PhysicalChannels,
res.Properties, res.Schema.Fields[0 ], res.Schema.Fields[1 ], res.ShardNum,
res.VirtualChannels, res.Schema.AutoID, res.Schema.CollectionName, res.Schema.Description)
curl -X POST "http://${MILVUS_URI} /v2/vectordb/collections/describe" \
-H "Content-Type: application/json" \
-d '{
"dbName": "default",
"collectionName": "test_collection"
}'
要列出所有现有的 Collections,可以按以下方式操作:
res = client.list_collections()
print (res)
import io.milvus.v2.service.collection.response.ListCollectionsResp;
ListCollectionsResp listCollectionsRes = client.listCollections();
System.out.println(listCollectionsRes.getCollectionNames());
ListCollectionsResp listCollectionsRes = client.listCollections();
System.out.println(listCollectionsRes.getCollectionNames());
collections, err := client.ListCollections(ctx)
if err != nil {
log.Fatal("failed to list collection:" , err.Error())
}
for _, c := range collections {
log.Println(c.Name)
}
$ curl -X POST "http://${MILVUS_URI} /v2/vectordb/collections/list" \
-H "Content-Type: application/json" \
-d '{
"dbName": "default"
}'
在加载 Collection 的过程中,Milvus 会将 Collection 的索引文件加载到内存中。相反,在释放 Collections 时,Milvus 会从内存中卸载索引文件。在某个 Collection 中进行搜索之前,请确保该 Collection 已加载。
要加载一个 Collection,请使用 load_collection()
方法,指定 Collections 名称。还可以设置replica_number
,以确定加载 Collections 时在查询节点上创建多少个数据段内存副本。
Milvus Standalone:replica_number
的最大允许值为 1。
Milvus 群集:最大值不应超过 Milvus 配置中设置的queryNode.replicas
。有关其他详细信息,请参阅查询节点相关配置 。
client.load_collection(
collection_name="customized_setup_2" ,
replica_number=1
)
res = client.get_load_state(
collection_name="customized_setup_2"
)
print (res)
import io.milvus.v2.service.collection.request.LoadCollectionReq;
LoadCollectionReq loadCollectionReq = LoadCollectionReq.builder()
.collectionName("customized_setup_2" )
.build();
client.loadCollection(loadCollectionReq);
GetLoadStateReq loadStateReq = GetLoadStateReq.builder()
.collectionName("customized_setup_2" )
.build();
res = client.getLoadState(loadStateReq);
System.out.println(res);
res = await client.loadCollection ({
collection_name : "customized_setup_2"
})
console .log (res.error_code )
await sleep (3000 )
res = await client.getLoadState ({
collection_name : "customized_setup_2"
})
console .log (res.state )
err = client.LoadCollection(ctx, "customized_setup_2" , false )
if err != nil {
log.Fatal("failed to laod collection:" , err.Error())
}
stateLoad, err := client.GetLoadState(context.Background(), "customized_setup_2" , []string {})
if err != nil {
log.Fatal("failed to get load state:" , err.Error())
}
fmt.Println(stateLoad)
$ curl -X POST "http://${MILVUS_URI} /v2/vectordb/collections/load" \
-H "Content-Type: application/json" \
-d '{
"collectionName": "customized_setup_2"
}'
$ curl -X POST "http://${MILVUS_URI} /v2/vectordb/collections/get_load_state" \
-H "Content-Type: application/json" \
-d '{
"collectionName": "customized_setup_2"
}'
此功能目前处于公开预览阶段。应用程序接口和功能将来可能会更改。
收到加载请求后,Milvus 会将所有向量字段索引和所有标量字段数据加载到内存中。如果某些字段不参与搜索和查询,您可以将其排除在加载之外,以减少内存使用,提高搜索性能。
client.load_collection(
collection_name="customized_setup_2" ,
load_fields=["my_id" , "my_vector" ],
skip_load_dynamic_field=True
)
res = client.get_load_state(
collection_name="customized_setup_2"
)
print (res)
请注意,只有load_fields
中列出的字段才能用作搜索和查询的筛选条件和输出字段。在列表中应始终包含主键。不加载的字段名将不能用于筛选或输出。
可以使用skip_load_dynamic_field=True
跳过加载动态字段。Milvus 将动态字段视为单个字段,因此动态字段中的所有键将一起被包含或排除。
client.release_collection(
collection_name="customized_setup_2"
)
res = client.get_load_state(
collection_name="customized_setup_2"
)
print (res)
import io.milvus.v2.service.collection.request.ReleaseCollectionReq;
ReleaseCollectionReq releaseCollectionReq = ReleaseCollectionReq.builder()
.collectionName("customized_setup_2" )
.build();
client.releaseCollection(releaseCollectionReq);
res = client.getLoadState(loadStateReq);
System.out.println(res);
res = await client.releaseCollection ({
collection_name : "customized_setup_2"
})
console .log (res.error_code )
res = await client.getLoadState ({
collection_name : "customized_setup_2"
})
console .log (res.state )
errRelease := client.ReleaseCollection(context.Background(), "customized_setup_2" )
if errRelease != nil {
log.Fatal("failed to release collection:" , errRelease.Error())
}
fmt.Println(errRelease)
stateLoad, err = client.GetLoadState(context.Background(), "customized_setup_2" , []string {})
if err != nil {
log.Fatal("failed to get load state:" , err.Error())
}
fmt.Println(stateLoad)
$ curl -X POST "http://${MILVUS_URI} /v2/vectordb/collections/release" \
-H "Content-Type: application/json" \
-d '{
"collectionName": "customized_setup_2"
}'
$ curl -X POST "http://${MILVUS_URI} /v2/vectordb/collections/get_load_state" \
-H "Content-Type: application/json" \
-d '{
"collectionName": "customized_setup_2"
}'
您可以为集合指定别名,使它们在特定上下文中更有意义。您可以为一个 Collection 指定多个别名,但多个 Collection 不能共享一个别名。
client.create_alias(
collection_name="customized_setup_2" ,
alias ="bob"
)
client.create_alias(
collection_name="customized_setup_2" ,
alias ="alice"
)
import io.milvus.v2.service.utility.request.CreateAliasReq;
CreateAliasReq createAliasReq = CreateAliasReq.builder()
.collectionName("customized_setup_2" )
.alias("bob" )
.build();
client.createAlias(createAliasReq);
createAliasReq = CreateAliasReq.builder()
.collectionName("customized_setup_2" )
.alias("alice" )
.build();
client.createAlias(createAliasReq);
res = await client.createAlias ({
collection_name : "customized_setup_2" ,
alias : "bob"
})
console .log (res.error_code )
res = await client.createAlias ({
collection_name : "customized_setup_2" ,
alias : "alice"
})
console .log (res.error_code )
$ curl -X POST "http://${MILVUS_URI} /v2/vectordb/aliases/create" \
-H "Content-Type: application/json" \
-d '{
"collectionName": "customized_setup_2",
"aliasName": "bob"
}'
$ curl -X POST "http://${MILVUS_URI} /v2/vectordb/aliases/create" \
-H "Content-Type: application/json" \
-d '{
"collectionName": "customized_setup_2",
"aliasName": "alice"
}'
参数
说明
collection_name
要创建别名的 Collections 名称。
alias
Collections 的别名。操作前,请确保别名不存在。如果已经存在,则会出现异常。
参数
参数
collectionName
要创建别名的 Collection 的名称。
alias
Collections 的别名。在进行此操作前,请确保别名不存在。如果已经存在,则会出现异常。
参数
参数
collection_name
要创建别名的 Collection 的名称。
alias
Collections 的别名。在进行此操作前,请确保别名不存在。如果已经存在,则会出现异常。
参数
参数
collectionName
要创建别名的 Collection 的名称。
aliasName
Collections 的别名。在进行此操作前,请确保别名不存在。如果已经存在,则会出现异常。
res = client.list_aliases(
collection_name="customized_setup_2"
)
print (res)
import io.milvus.v2.service.utility.request.ListAliasesReq;
import io.milvus.v2.service.utility.response.ListAliasResp;
ListAliasesReq listAliasesReq = ListAliasesReq.builder()
.collectionName("customized_setup_2" )
.build();
ListAliasResp listAliasRes = client.listAliases(listAliasesReq);
System.out.println(listAliasRes.getAlias());
res = await client.listAliases ({
collection_name : "customized_setup_2"
})
console .log (res.aliases )
$ curl -X POST "http://${MILVUS_URI} /v2/vectordb/aliases/list" \
-H "Content-Type: application/json" \
-d '{
"collectionName": "customized_setup_2"
}'
res = client.describe_alias(
alias ="bob"
)
print (res)
import io.milvus .v2 .service .utility .request .DescribeAliasReq ;
import io.milvus .v2 .service .utility .response .DescribeAliasResp ;
DescribeAliasReq describeAliasReq = DescribeAliasReq .builder ()
.alias ("bob" )
.build ();
DescribeAliasResp describeAliasRes = client.describeAlias (describeAliasReq);
System .out .println (JSON Object .toJSON (describeAliasRes));
res = await client.describeAlias ({
collection_name : "customized_setup_2" ,
alias : "bob"
})
console .log (res)
$ curl -X POST "http://${MILVUS_URI} /v2/vectordb/aliases/describe" \
-H "Content-Type: application/json" \
-d '{
"aliasName": "bob"
}'
要将别名重新分配给其他 Collections,请使用 alter_alias()
方法,指定 Collections 名称和别名。
要将别名重新指定给其他 Collections,请使用 alterAlias()
方法,指定 Collections 名称和别名。
要将别名重新分配给其他 Collections,请使用 alterAlias()
方法,指定 Collections 名称和别名。
client.alter_alias(
collection_name="customized_setup_1" ,
alias="alice"
)
res = client.list_aliases(
collection_name="customized_setup_1"
)
print (res)
res = client.list_aliases(
collection_name="customized_setup_2"
)
print (res)
import io.milvus.v2.service.utility.request.AlterAliasReq;
AlterAliasReq alterAliasReq = AlterAliasReq.builder()
.collectionName("customized_setup_1" )
.alias("alice" )
.build();
client.alterAlias(alterAliasReq);
listAliasesReq = ListAliasesReq.builder()
.collectionName("customized_setup_1" )
.build();
listAliasRes = client.listAliases(listAliasesReq);
System.out.println (listAliasRes.getAlias());
listAliasesReq = ListAliasesReq.builder()
.collectionName("customized_setup_2" )
.build();
listAliasRes = client.listAliases(listAliasesReq);
System.out.println (listAliasRes.getAlias());
res = await client.alterAlias ({
collection_name : "customized_setup_1" ,
alias : "alice"
})
console .log (res.error_code )
res = await client.listAliases ({
collection_name : "customized_setup_1"
})
console .log (res.aliases )
res = await client.listAliases ({
collection_name : "customized_setup_2"
})
console .log (res.aliases )
$ curl -X POST "http://${MILVUS_URI} /v2/vectordb/aliases/alter" \
-H "Content-Type: application/json" \
-d '{
"collectionName": "customized_setup_1",
"aliasName": "alice"
}'
$ curl -X POST "http://${MILVUS_URI} /v2/vectordb/aliases/list" \
-H "Content-Type: application/json" \
-d '{
"collectionName": "customized_setup_1"
}'
$ curl -X POST "http://${MILVUS_URI} /v2/vectordb/aliases/list" \
-H "Content-Type: application/json" \
-d '{
"collectionName": "customized_setup_2"
}'
client.drop_alias(
alias ="bob"
)
client.drop_alias(
alias ="alice"
)
import io.milvus.v2.service.utility.request.DropAliasReq;
DropAliasReq dropAliasReq = DropAliasReq.builder()
.alias("bob" )
.build();
client.dropAlias(dropAliasReq);
dropAliasReq = DropAliasReq.builder()
.alias("alice" )
.build();
client.dropAlias(dropAliasReq);
res = await client.dropAlias ({
alias : "bob"
})
console .log (res.error_code )
res = await client.dropAlias ({
alias : "alice"
})
console .log (res.error_code )
$ curl -X POST "http://${MILVUS_URI} /v2/vectordb/aliases/drop" \
-H "Content-Type: application/json" \
-d '{
"aliasName": "bob"
}'
$ curl -X POST "http://${MILVUS_URI} /v2/vectordb/aliases/drop" \
-H "Content-Type: application/json" \
-d '{
"aliasName": "alice"
}'
可以为 Collections 设置属性,如ttl.seconds
和mmap.enabled
。更多信息,请参阅set_properties() 。
为 Collections 中的数据设置生存时间(TTL),指定数据在自动删除前应保留多长时间。
from pymilvus import Collection, connections
connections.connect(host="localhost" , port="19530" )
collection = Collection("quick_setup" )
collection.set_properties(
properties={
"collection.ttl.seconds" : 60
}
)
为 Collections 配置内存映射 (MMAP) 属性,该属性决定数据是否映射到内存中以提高查询性能。有关详细信息,请参阅配置内存映射 。
在设置 MMAP 属性之前,请先释放 Collection。否则会出错。
from pymilvus import Collection, connections
connections.connect(host="localhost" , port="19530" )
collection = Collection("quick_setup" )
collection.release()
collection.set_properties(
properties={
"mmap.enabled" : True
}
)
如果不再需要某个 Collection,您可以放弃该 Collection。
client.drop_collection(
collection_name="quick_setup"
)
client.drop_collection(
collection_name="customized_setup_1"
)
client.drop_collection(
collection_name="customized_setup_2"
)
import io.milvus.v2.service.collection.request.DropCollectionReq;
DropCollectionReq dropQuickSetupParam = DropCollectionReq.builder()
.collectionName("quick_setup" )
.build();
client.dropCollection(dropQuickSetupParam);
DropCollectionReq dropCustomizedSetupParam = DropCollectionReq.builder()
.collectionName("customized_setup_1" )
.build();
client.dropCollection(dropCustomizedSetupParam);
dropCustomizedSetupParam = DropCollectionReq.builder()
.collectionName("customized_setup_2" )
.build();
client.dropCollection(dropCustomizedSetupParam);
res = await client.dropCollection ({
collection_name : "customized_setup_2"
})
console .log (res.error_code )
res = await client.dropCollection ({
collection_name : "customized_setup_1"
})
console .log (res.error_code )
res = await client.dropCollection ({
collection_name : "quick_setup"
})
console .log (res.error_code )
err = client.DropCollection (ctx, "quick_setup" )
if err != nil {
log.Fatal ("failed to drop collection:" , err.Error ())
}
err = client.DropCollection (ctx, "customized_setup_2" )
if err != nil {
log.Fatal ("failed to drop collection:" , err.Error ())
}
$ curl -X POST "http://${MILVUS_URI} /v2/vectordb/collections/drop" \
-H "Content-Type: application/json" \
-d '{
"collectionName": "quick_setup"
}'
$ curl -X POST "http://${MILVUS_URI} /v2/vectordb/collections/drop" \
-H "Content-Type: application/json" \
-d '{
"collectionName": "customized_setup_1"
}'
$ curl -X POST "http://${MILVUS_URI} /v2/vectordb/collections/drop" \
-H "Content-Type: application/json" \
-d '{
"collectionName": "customized_setup_2"
}'