ベクトルフィールドのインデックス作成
このガイドでは、コレクション内のベクトルフィールドに対してインデックスを作成・管理する基本的な操作を説明します。
概要
Zilliz Cloud は、インデックスファイルに保存されたメタデータを活用して、データを特殊な構造で整理し、検索やクエリ時に要求された情報を高速に取得できるようにします。
Zilliz Cloud は効率的な類似検索を実現するために AUTOINDEX を採用しています。また、ベクトル埋め込み間の距離を測定するための以下のメトリックタイプも提供しています:コサイン類似度(COSINE)、ユークリッド距離(L2)、内積(IP)、JACCARD、および HAMMING。ベクトルフィールドのタイプとメトリックに関する詳細は、メトリックタイプs および Schema Explained を参照してください。
頻繁にアクセスされるベクトルフィールドおよびスカラーフィールドの両方にインデックスを作成することを推奨します。コレクションに複数のベクトルフィールドが含まれる場合、各ベクトルフィールドに対して個別にインデックスを作成できます。
準備
Create Collection で説明されているように、コレクション作成リクエストで以下のいずれかの条件が指定されている場合、Zilliz Cloud はコレクション作成時に自動的にインデックスを生成し、メモリにロードします。
- ベクトルフィールドの次元数とメトリックタイプ、または
- スキーマとインデックスパラメータ。
以下のコードスニペットでは、既存のコードを再利用して Zilliz Cloud への接続を確立し、インデックスパラメータを指定せずにコレクションを作成します。この場合、コレクションにはインデックスが存在せず、ロードもされていません。
- Python
- Java
- NodeJS
from pymilvus import MilvusClient, DataType
CLUSTER_ENDPOINT = "YOUR_CLUSTER_ENDPOINT"
TOKEN = "YOUR_CLUSTER_TOKEN"
# 1. Set up a Milvus client
client = MilvusClient(
uri=CLUSTER_ENDPOINT,
token=TOKEN
)
# 2. Create schema
# 2.1. Create schema
schema = MilvusClient.create_schema(
auto_id=False,
enable_dynamic_field=True,
)
# 2.2. Add fields to schema
schema.add_field(field_name="id", datatype=DataType.INT64, is_primary=True)
# The dim value should be an integer greater than 1
schema.add_field(field_name="vector", datatype=DataType.FLOAT_VECTOR, dim=5)
# 3. Create collection
client.create_collection(
collection_name="customized_setup",
schema=schema,
)
import io.milvus.v2.client.ConnectConfig;
import io.milvus.v2.client.MilvusClientV2;
import io.milvus.v2.common.DataType;
import io.milvus.v2.service.collection.request.CreateCollectionReq;
String CLUSTER_ENDPOINT = "YOUR_CLUSTER_ENDPOINT";
String TOKEN = "YOUR_TOKEN";
// 1. Connect to Milvus server
ConnectConfig connectConfig = ConnectConfig.builder()
.uri(CLUSTER_ENDPOINT)
.token(TOKEN)
.build();
MilvusClientV2 client = new MilvusClientV2(connectConfig);
// 2. Create a collection
// 2.1 Create schema
CreateCollectionReq.CollectionSchema schema = client.createSchema();
// 2.2 Add fields to schema
schema.addField(AddFieldReq.builder().fieldName("id").dataType(DataType.Int64).isPrimaryKey(true).autoID(false).build());
// The dimension value should be an integer greater than 1.
schema.addField(AddFieldReq.builder().fieldName("vector").dataType(DataType.FloatVector).dimension(5).build());
// 3 Create a collection without schema and index parameters
CreateCollectionReq customizedSetupReq = CreateCollectionReq.builder()
.collectionName("customized_setup")
.collectionSchema(schema)
.build();
client.createCollection(customizedSetupReq);
// 1. Set up a Milvus Client
client = new MilvusClient({address, token});
// 2. Define fields for the collection
const fields = [
{
name: "id",
data_type: DataType.Int64,
is_primary_key: true,
auto_id: false
},
{
name: "vector",
data_type: DataType.FloatVector,
dim: 5 // The dim value should be greater than 1.
},
]
// 3. Create a collection
res = await client.createCollection({
collection_name: "customized_setup",
fields: fields,
})
console.log(res.error_code)
// Output
//
// Success
//
コレクションにインデックスを作成する
コレクションのインデックスを作成するには、インデックスパラメータを設定し、create_index() を呼び出します。
- Python
- Java
- NodeJS
# 4. Set up index
# 4.1. Set up the index parameters
index_params = MilvusClient.prepare_index_params()
# 4.2. Add an index on the vector field.
index_params.add_index(
field_name="vector",
metric_type="COSINE",
index_type="AUTOINDEX",
index_name="vector_index"
)
# 4.4. Create an index file
client.create_index(
collection_name="customized_setup",
index_params=index_params
)
# 5. Describe index
res = client.list_indexes(
collection_name="customized_setup"
)
import io.milvus.v2.common.IndexParam;
import io.milvus.v2.service.index.request.CreateIndexReq;
// 4 Prepare index parameters
// 4.2 Add an index for the vector field "vector"
IndexParam indexParamForVectorField = IndexParam.builder()
.fieldName("vector")
.indexName("vector_index")
.indexType(IndexParam.IndexType.AUTOINDEX)
.metricType(IndexParam.MetricType.COSINE)
.build();
List<IndexParam> indexParams = new ArrayList<>();
indexParams.add(indexParamForVectorField);
// 4.3 Crate an index file
CreateIndexReq createIndexReq = CreateIndexReq.builder()
.collectionName("customized_setup")
.indexParams(indexParams)
.build();
client.createIndex(createIndexReq);
// 4. Set up index for the collection
// 4.1. Set up the index parameters
res = await client.createIndex({
collection_name: "customized_setup",
field_name: "vector",
index_type: "AUTOINDEX",
metric_type: "COSINE",
index_name: "vector_index"
})
console.log(res.error_code)
// Output
//
// Success
//
提供されたコードスニペットでは、ベクトルフィールドに対してインデックスを設定し、インデックスタイプを AUTOINDEX、メトリックタイプを COSINE にしています。さらに、スカラーフィールドにもインデックスタイプ AUTOINDEX を使用してインデックスを作成しています。インデックスタイプおよびメトリックタイプの詳細については、AUTOINDEX Explained および メトリックタイプs を参照してください。
現在、コレクション内の各フィールドに対して作成できるインデックスファイルは1つだけです。
インデックスの詳細を確認する
インデックスを作成したら、その詳細を確認できます。
- Python
- Java
- NodeJS
# 5. Describe index
res = client.list_indexes(
collection_name="customized_setup"
)
print(res)
# Output
#
# [
# "vector_index"
# ]
res = client.describe_index(
collection_name="customized_setup",
index_name="vector_index"
)
print(res)
# Output
#
# {
# "index_type": "AUTOINDEX",
# "metric_type": "COSINE",
# "field_name": "vector",
# "index_name": "vector_index"
# }
import io.milvus.v2.service.index.request.DescribeIndexReq;
import io.milvus.v2.service.index.response.DescribeIndexResp;
// 5. Describe index
// 5.1 List the index names
ListIndexesReq listIndexesReq = ListIndexesReq.builder()
.collectionName("customized_setup")
.build();
List<String> indexNames = client.listIndexes(listIndexesReq);
System.out.println(indexNames);
// Output:
// [
// "vector_index"
// ]
// 5.2 Describe an index
DescribeIndexReq describeIndexReq = DescribeIndexReq.builder()
.collectionName("customized_setup")
.indexName("vector_index")
.build();
DescribeIndexResp describeIndexResp = client.describeIndex(describeIndexReq);
System.out.println(JSONObject.toJSON(describeIndexResp));
// Output:
// {
// "metricType": "COSINE",
// "indexType": "AUTOINDEX",
// "fieldName": "vector",
// "indexName": "vector_index"
// }
// 5. Describe the index
res = await client.describeIndex({
collection_name: "customized_setup",
index_name: "vector_index"
})
console.log(JSON.stringify(res.index_descriptions, null, 2))
// Output
//
// [
// {
// "params": [
// {
// "key": "index_type",
// "value": "AUTOINDEX"
// },
// {
// "key": "metric_type",
// "value": "COSINE"
// }
// ],
// "index_name": "vector_index",
// "indexID": "449007919953063141",
// "field_name": "vector",
// "indexed_rows": "0",
// "total_rows": "0",
// "state": "Finished",
// "index_state_fail_reason": "",
// "pending_index_rows": "0"
// }
// ]
//
特定のフィールドに作成されたインデックスファイルを確認し、そのインデックスファイルを使用してインデックスされた行数の統計情報を収集できます。
Drop an index
インデックスが不要になった場合は、簡単に削除できます。
インデックスを削除する前に、事前にリリース済みであることを確認してください。
- Python
- Java
- NodeJS
# 6. Drop index
client.drop_index(
collection_name="customized_setup",
index_name="vector_index"
)
// 6. Drop index
DropIndexReq dropIndexReq = DropIndexReq.builder()
.collectionName("customized_setup")
.indexName("vector_index")
.build();
client.dropIndex(dropIndexReq);
// 6. Drop the index
res = await client.dropIndex({
collection_name: "customized_setup",
index_name: "vector_index"
})
console.log(res.error_code)
// Output
//
// Success
//
高度な機能
ベクターインデックスに関して、他にもいくつかの高度な機能があります。
構築レベルの調整 [READ MORE]
Zilliz Cloud では、対象コレクションのストレージ容量と検索再現率のバランスを調整できる `buildlevel` というパラメータを導入しています。使用頻度が低いコレクションや、より多くのストレージスペースが必要なコレクションでは、わずかな再現率の低下と引き換えにストレージ容量を大幅に増やすことができ、その逆も可能です。本ガイドでは、利用可能なオプションと、コレクションのインデックス構築にそれらを使用する方法について説明します。
MINHASH_LSH [READ MORE]
大規模な機械学習データセット、特に大規模言語モデル(LLM)のトレーニング用コーパスのクリーニングなどのタスクにおいて、効率的な重複排除と類似性検索は不可欠です。数百万から数十億のドキュメントを扱う場合、従来の完全一致では速度とコストの面で非現実的になります。