コレクションの作成
スキーママ、インデックスパラメータ、メトリック型、作成時にロードするかどうかを定義することでコレクションを作成できます。このページでは、最初からコレクションを作成する方法を紹介します。
強力なデータ分離が必要で、少数のテナントのみを管理する場合は、テナントごとに個別のコレクションを作成できます。
ただし、クラスタープランに応じて最大16,384個のコレクションしか作成できません。したがって、大規模なマルチテナントでは、使用ケースに応じてパーティションベースまたはパーティションキーベースのマルチテナントなどの代替戦略を使用することを検討してください。詳細については、マルチテナントの実装を参照してください。
概要
コレクションは固定カラムと可変行を持つ2次元テーブルです。各カラムはフィールドを表し、各行はエンティティを表します。このような構造的データ管理を実装するにはスキーママが必要です。挿入するすべてのエンティティは、スキーママで定義された制約を満たす必要があります。
コレクションのすべての側面を決定できます。これには、コレクションが要件を完全に満たすようにするために必要な、そのスキーママ、インデックスパラメータ、メトリック型、作成時にロードするかどうかを含みます。
コレクションを作成するには以下が必要です:
-
インデックスパラメータを設定(オプション)
スキーママの作成
スキーママは、コレクションのデータ構造を定義します。コレクションを作成する際には、要件に基づいてスキーママを設計する必要があります。詳細については、スキーママの説明を参照してください。
以下のコードスニペットは、有効化された動的フィールドとmy_id、my_vector、my_varcharという名前の3つの必須フィールドを持つスキーママを作成します。
任意のスカラー項目にデフォルト値を設定し、NULL可能にできます。詳細については、NULL可能とデフォルトを参照してください。
- Python
- Java
- NodeJS
- Go
- cURL
# 3. カスタム設定モードでコレクションを作成
from pymilvus import MilvusClient, DataType
client = MilvusClient(
uri="YOUR_CLUSTER_ENDPOINT",
token="YOUR_CLUSTER_TOKEN"
)
# 3.1. スキーママを作成
schema = MilvusClient.create_schema(
auto_id=False,
enable_dynamic_field=True,
)
# 3.2. スキーママにフィールドを追加
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)
schema.add_field(field_name="my_varchar", datatype=DataType.VARCHAR, max_length=512)
import io.milvus.v2.common.DataType;
import io.milvus.v2.client.ConnectConfig;
import io.milvus.v2.client.MilvusClientV2;
import io.milvus.v2.service.collection.request.AddFieldReq;
import io.milvus.v2.service.collection.request.CreateCollectionReq;
String CLUSTER_ENDPOINT = "YOUR_CLUSTER_ENDPOINT";
String TOKEN = "YOUR_CLUSTER_TOKEN";
// 1. Milvusサーバーに接続
ConnectConfig connectConfig = ConnectConfig.builder()
.uri(CLUSTER_ENDPOINT)
.token(TOKEN)
.build();
MilvusClientV2 client = new MilvusClientV2(connectConfig);
// 3. カスタム設定モードでコレクションを作成
// 3.1 スキーママを作成
CreateCollectionReq.CollectionSchema schema = client.createSchema();
// 3.2 スキーママにフィールドを追加
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());
schema.addField(AddFieldReq.builder()
.fieldName("my_varchar")
.dataType(DataType.VarChar)
.maxLength(512)
.build());
import { MilvusClient, DataType } from "@zilliz/milvus2-sdk-node";
const address = "YOUR_CLUSTER_ENDPOINT";
const token = "YOUR_CLUSTER_TOKEN";
const client = new MilvusClient({address, token});
// 3. カスタム設定モードでコレクションを作成
// 3.1 フールドを定義
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
},
{
name: "my_varchar",
data_type: DataType.VarChar,
max_length: 512
}
]
import (
"context"
"fmt"
"github.com/milvus-io/milvus/client/v2/entity"
"github.com/milvus-io/milvus/client/v2/index"
"github.com/milvus-io/milvus/client/v2/milvusclient"
"github.com/milvus-io/milvus/pkg/v2/common"
)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
milvusAddr := "YOUR_CLUSTER_ENDPOINT"
client, err := milvusclient.New(ctx, &milvusclient.ClientConfig{
Address: milvusAddr,
})
if err != nil {
fmt.Println(err.Error())
// handle error
}
defer client.Close(ctx)
schema := entity.NewSchema().WithDynamicFieldEnabled(true).
WithField(entity.NewField().WithName("my_id").WithIsAutoID(false).WithDataType(entity.FieldTypeInt64).WithIsPrimaryKey(true)).
WithField(entity.NewField().WithName("my_vector").WithDataType(entity.FieldTypeFloatVector).WithDim(5)).
WithField(entity.NewField().WithName("my_varchar").WithDataType(entity.FieldTypeVarChar).WithMaxLength(512))
export schema='{
"autoId": false,
"enabledDynamicField": false,
"fields": [
{
"fieldName": "my_id",
"dataType": "Int64",
"isPrimary": true
},
{
"fieldName": "my_vector",
"dataType": "FloatVector",
"elementTypeParams": {
"dim": "5"
}
},
{
"fieldName": "my_varchar",
"dataType": "VarChar",
"elementTypeParams": {
"max_length": 512
}
}
]
}'
(オプション)インデックスパラメータの設定
特定のフィールドにインデックスを作成すると、このフィールドに対する検索が高速化されます。インデックスはコレクション内のエンティティの順序を記録します。以下のコードスニペットに示すように、metric_typeおよびindex_typeを使用して、Zilliz Cloudがフィールドをインデックス化し、ベクトル埋め込み間の類似性を測定する適切な方法を選択できます。
Zilliz Cloudでは、すべてのベクトルフィールドにAUTOINDEXをインデックスタイプとして使用し、必要に応じてメトリック型としてCOSINE、L2、IPのいずれかを使用できます。
上記のコードスニペットに示されているように、ベクトルフィールドにはインデックスタイプとメトリック型の両方を設定する必要があり、スカラー項目にはインデックスタイプのみを設定する必要があります。インデックスはベクトルフィールドに必須であり、フィルタリング条件で頻繁に使用されるスカラー項目にもインデックスを作成することをお勧めします。
詳細については、インデックスの管理を参照してください。
- Python
- Java
- NodeJS
- Go
- cURL
# 3.3. インデックスパラメータを準備
index_params = client.prepare_index_params()
# 3.4. インデックスを追加
index_params.add_index(
field_name="my_id",
index_type="AUTOINDEX"
)
index_params.add_index(
field_name="my_vector",
index_type="AUTOINDEX",
metric_type="COSINE"
)
import io.milvus.v2.common.IndexParam;
import java.util.*;
// 3.3 インデックスパラメータを準備
IndexParam indexParamForIdField = IndexParam.builder()
.fieldName("my_id")
.indexType(IndexParam.IndexType.AUTOINDEX)
.build();
IndexParam indexParamForVectorField = IndexParam.builder()
.fieldName("my_vector")
.indexType(IndexParam.IndexType.AUTOINDEX)
.metricType(IndexParam.MetricType.COSINE)
.build();
List<IndexParam> indexParams = new ArrayList<>();
indexParams.add(indexParamForIdField);
indexParams.add(indexParamForVectorField);
// 3.2 インデックスパラメータを準備
const index_params = [{
field_name: "my_id",
index_type: "AUTOINDEX"
},{
field_name: "my_vector",
index_type: "AUTOINDEX",
metric_type: "COSINE"
}]
import (
"github.com/milvus-io/milvus/client/v2/entity"
"github.com/milvus-io/milvus/client/v2/index"
"github.com/milvus-io/milvus/client/v2/milvusclient"
)
collectionName := "customized_setup_1"
indexOptions := []milvusclient.CreateIndexOption{
milvusclient.NewCreateIndexOption(collectionName, "my_vector", index.NewAutoIndex(entity.COSINE)),
milvusclient.NewCreateIndexOption(collectionName, "my_id", index.NewAutoIndex(entity.COSINE)),
}
export indexParams='[
{
"fieldName": "my_vector",
"metricType": "COSINE",
"indexName": "my_vector",
"indexType": "AUTOINDEX"
},
{
"fieldName": "my_id",
"indexName": "my_id",
"indexType": "AUTOINDEX"
}
]'
コレクションの作成
インデックスパラメータを使用してコレクションを作成した場合、Zilliz Cloudは作成時にコレクションを自動的にロードします。この場合、インデックスパラメータで言及されているすべてのフィールドがインデックス化されます。
以下のコードスニペットは、インデックスパラメータとともにコレクションを作成し、そのロード状態を確認する方法を示しています。
- Python
- Java
- NodeJS
- Go
- cURL
# 3.5. インデックス付きでコレクションを作成
client.create_collection(
collection_name="customized_setup_1",
schema=schema,
index_params=index_params
)
res = client.get_load_state(
collection_name="customized_setup_1"
)
print(res)
# 出力
#
# {
# "state": "<LoadState: Loaded>"
# }
import io.milvus.v2.service.collection.request.CreateCollectionReq;
import io.milvus.v2.service.collection.request.GetLoadStateReq;
// 3.4 インデックスパラメータとスキーママでコレクションを作成
CreateCollectionReq customizedSetupReq1 = CreateCollectionReq.builder()
.collectionName("customized_setup_1")
.collectionSchema(schema)
.indexParams(indexParams)
.build();
client.createCollection(customizedSetupReq1);
// 3.5 コレクションのロード状態を取得
GetLoadStateReq customSetupLoadStateReq1 = GetLoadStateReq.builder()
.collectionName("customized_setup_1")
.build();
Boolean loaded = client.getLoadState(customSetupLoadStateReq1);
System.out.println(loaded);
// 出力:
// true
// 3.3 フールドとインデックスパラメータでコレクションを作成
res = await client.createCollection({
collection_name: "customized_setup_1",
fields: fields,
index_params: index_params,
})
console.log(res.error_code)
// 出力
//
// Success
//
res = await client.getLoadState({
collection_name: "customized_setup_1"
})
console.log(res.state)
// 出力
//
// LoadStateLoaded
//
err = client.CreateCollection(ctx, milvusclient.NewCreateCollectionOption("customized_setup_1", schema).
WithIndexOptions(indexOptions...))
if err != nil {
fmt.Println(err.Error())
// handle error
}
fmt.Println("collection created")
export CLUSTER_ENDPOINT="YOUR_CLUSTER_ENDPOINT"
export TOKEN="YOUR_CLUSTER_TOKEN"
curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/collections/create" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
-d "{
\"collectionName\": \"customized_setup_1\",
\"schema\": $schema,
\"indexParams\": $indexParams
}"
インデックスパラメータを設定せずにコレクションを作成し、後で追加することもできます。この場合、Zilliz Cloudは作成時にコレクションをロードしません。既存のコレクションにインデックスを作成する方法の詳細については、AUTOINDEXの説明を参照してください。
以下のコードスニペットは、インデックスなしでコレクションを作成する方法を示し、作成時はロード状態がロードされていないままになります。
- Python
- Java
- NodeJS
- Go
- cURL
# 3.6. インデックスを分離してコレクションを作成
client.create_collection(
collection_name="customized_setup_2",
schema=schema,
)
res = client.get_load_state(
collection_name="customized_setup_2"
)
print(res)
# 出力
#
# {
# "state": "<LoadState: NotLoad>"
# }
// 3.6 インデックスを分離してコレクションを作成
CreateCollectionReq customizedSetupReq2 = CreateCollectionReq.builder()
.collectionName("customized_setup_2")
.collectionSchema(schema)
.build();
client.createCollection(customizedSetupReq2);
GetLoadStateReq customSetupLoadStateReq2 = GetLoadStateReq.builder()
.collectionName("customized_setup_2")
.build();
Boolean loaded = client.getLoadState(customSetupLoadStateReq2);
System.out.println(loaded);
// 出力:
// false
// 3.4 インデックスを分離してコレクションを作成
res = await client.createCollection({
collection_name: "customized_setup_2",
fields: fields,
})
console.log(res.error_code)
// 出力
//
// Success
//
res = await client.getLoadState({
collection_name: "customized_setup_2"
})
console.log(res.state)
// 出力
//
// LoadStateNotLoad
//
err = client.CreateCollection(ctx, milvusclient.NewCreateCollectionOption("customized_setup_2", schema))
if err != nil {
fmt.Println(err.Error())
// handle error
}
fmt.Println("collection created")
state, err := client.GetLoadState(ctx, milvusclient.NewGetLoadStateOption("customized_setup_2"))
if err != nil {
fmt.Println(err.Error())
// handle error
}
fmt.Println(state.State)
export CLUSTER_ENDPOINT="YOUR_CLUSTER_ENDPOINT"
export TOKEN="YOUR_CLUSTER_TOKEN"
curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/collections/create" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
-d "{
\"collectionName\": \"customized_setup_2\",
\"schema\": $schema
}"
curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/collections/get_load_state" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
-d "{
\"collectionName\": \"customized_setup_2\"
}"
コレクションプロパティの設定
コレクションを作成する際にプロパティを設定して、サービスに合わせることができます。適用可能なプロパティは以下の通りです。
シャード数の設定
シャードはコレクションの水平スライスであり、各シャードはデータ入力チャネルに対応します。デフォルトでは、すべてのコレクションは1つのシャードを持ちます。データ量とワークロードに合わせて、コレクション作成時にシャード数を指定できます。
シャード数を設定する際の一般的なガイドラインは以下の通りです:
- データサイズ: 慣例として、2億エンティティにつき1つのシャードを用いるのが一般的です。また、総データサイズに基づいて見積もることもできます。たとえば、挿入する予定のデータ量に応じて、100GBごとに1つのシャードを追加することもできます。
以下のコードスニペットは、コレクション作成時にシャード数を設定する方法を示しています。
- Python
- Java
- NodeJS
- Go
- cURL
# シャード数付き
client.create_collection(
collection_name="customized_setup_3",
schema=schema,
num_shards=1
)
// シャード数付き
CreateCollectionReq customizedSetupReq3 = CreateCollectionReq.builder()
.collectionName("customized_setup_3")
.collectionSchema(collectionSchema)
.numShards(1)
.build();
client.createCollection(customizedSetupReq3);
const createCollectionReq = {
collection_name: "customized_setup_3",
schema: schema,
shards_num: 1
}
err = client.CreateCollection(ctx, milvusclient.NewCreateCollectionOption("customized_setup_3", schema).WithShardNum(1))
if err != nil {
fmt.Println(err.Error())
// handle error
}
fmt.Println("collection created")
export params='{
"shardsNum": 1
}'
export CLUSTER_ENDPOINT="YOUR_CLUSTER_ENDPOINT"
export TOKEN="YOUR_CLUSTER_TOKEN"
curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/collections/create" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
-d "{
\"collectionName\": \"customized_setup_3\",
\"schema\": $schema,
\"params\": $params
}"
mmapの有効化
Zilliz Cloudはデフォルトですべてのコレクションでmmapを有効にしており、Zilliz Cloudが生フィールドデータを完全にロードする代わりにメモリにマッピングできるようにしています。これによりメモリの使用量が削減され、コレクション容量が拡大します。mmapの詳細については、mmapの使用を参照してください。
- Python
- Java
- NodeJS
- Go
# mmap付き
client.create_collection(
collection_name="customized_setup_4",
schema=schema,
enable_mmap=False
)
import io.milvus.param.Constant;
// MMap付き
CreateCollectionReq customizedSetupReq4 = CreateCollectionReq.builder()
.collectionName("customized_setup_4")
.collectionSchema(schema)
.property(Constant.MMAP_ENABLED, "false")
.build();
client.createCollection(customizedSetupReq4);
client.create_collection({
collection_name: "customized_setup_4",
schema: schema,
properties: {
'mmap.enabled': true,
},
})
err = client.CreateCollection(ctx, milvusclient.NewCreateCollectionOption("customized_setup_4", schema).
WithProperty(common.MmapEnabledKey, true))
if err != nil {
fmt.Println(err.Error())
// handle error
}
fmt.Println("collection created")
export params='{
"mmap.enabled": True
}'
export CLUSTER_ENDPOINT="YOUR_CLUSTER_ENDPOINT"
export TOKEN="YOUR_CLUSTER_TOKEN"
curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/collections/create" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
-d "{
\"collectionName\": \"customized_setup_5\",
\"schema\": $schema,
\"params\": $params
}"
コレクションTTLの設定
コレクション内のデータが特定の期間後に削除される必要がある場合は、TTL(Time-To-Live)を秒単位で設定することを検討してください。TTLがタイムアウトすると、Zilliz Cloudはコレクション内のエンティティを削除します。削除は非同期操作であるため、削除が完了する前に検索およびクエリは引き続き可能です。
以下のコードスニペットは、TTLを1日(86400秒)に設定します。最低でも数日間のTTLを設定することをお勧めします。
- Python
- Java
- NodeJS
- Go
- cURL
# TTL付き
client.create_collection(
collection_name="customized_setup_5",
schema=schema,
properties={
"collection.ttl.seconds": 86400
}
)
import io.milvus.param.Constant;
// TTL付き
CreateCollectionReq customizedSetupReq5 = CreateCollectionReq.builder()
.collectionName("customized_setup_5")
.collectionSchema(schema)
.property(Constant.TTL_SECONDS, "86400")
.build();
client.createCollection(customizedSetupReq5);
const createCollectionReq = {
collection_name: "customized_setup_5",
schema: schema,
properties: {
"collection.ttl.seconds": 86400
}
}
err = client.CreateCollection(ctx, milvusclient.NewCreateCollectionOption("customized_setup_5", schema).
WithProperty(common.CollectionTTLConfigKey, true))
if err != nil {
fmt.Println(err.Error())
// handle error
}
fmt.Println("collection created")
export params='{
"ttlSeconds": 86400
}'
export CLUSTER_ENDPOINT="YOUR_CLUSTER_ENDPOINT"
export TOKEN="YOUR_CLUSTER_TOKEN"
curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/collections/create" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
-d "{
\"collectionName\": \"customized_setup_5\",
\"schema\": $schema,
\"params\": $params
}"
一貫性レベルの設定
コレクションを作成する際には、コレクション内の検索およびクエリの一貫性レベルを設定できます。特定の検索またはクエリ中にコレクションの一貫性レベルを変更することもできます。
- Python
- Java
- NodeJS
- Go
- cURL
# 一貫性レベル付き
client.create_collection(
collection_name="customized_setup_6",
schema=schema,
# highlight-next
consistency_level="Bounded",
)
import io.milvus.v2.common.ConsistencyLevel;
// 一貫性レベル付き
CreateCollectionReq customizedSetupReq6 = CreateCollectionReq.builder()
.collectionName("customized_setup_6")
.collectionSchema(schema)
.consistencyLevel(ConsistencyLevel.BOUNDED)
.build();
client.createCollection(customizedSetupReq6);
const createCollectionReq = {
collection_name: "customized_setup_6",
schema: schema,
// highlight-next
consistency_level: "Bounded",
}
client.createCollection(createCollectionReq);
err = client.CreateCollection(ctx, milvusclient.NewCreateCollectionOption("customized_setup_6", schema).
WithConsistencyLevel(entity.ClBounded))
if err != nil {
fmt.Println(err.Error())
// handle error
}
fmt.Println("collection created")
export params='{
"consistencyLevel": "Bounded"
}'
export CLUSTER_ENDPOINT="YOUR_CLUSTER_ENDPOINT"
export TOKEN="YOUR_CLUSTER_TOKEN"
curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/collections/create" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
-d "{
\"collectionName\": \"customized_setup_6\",
\"schema\": $schema,
\"params\": $params
}"
一貫性レベルの詳細については、一貫性レベルを参照してください。
動的フィールドの有効化
コレクションの動的フィールドは、$metaという名前の予約済みJavaScript Object Notation(JSON)フィールドです。このフィールドを有効にすると、Zilliz Cloudは各エンティティに含まれるスキーママ定義されていないすべてのフィールドとその値を、予約済みフィールド内のキーバリューとして保存します。
動的フィールドの使用方法の詳細については、動的フィールドを参照してください。