ベクトルフィールドのインデックス
このガイドでは、コレクション内のベクトルフィールドにインデックスを作成および管理する基本操作を説明します。
概要
Zilliz Cloudは、インデックスファイルに保存されたメタデータを活用して、データを専門的な構造に整理し、検索またはクエリ時に要求された情報を迅速に取得できるようにします。
Zilliz Cloudは効率的な類似性検索を可能にするためにAUTOINDEXを利用しています。また、ベクトル埋め込み間の距離を測定するための以下のメトリックタイプを提供しています:コサイン類似度(COSINE)、ユークリッド距離(L2)、内積(IP)、JACCARD、およびHAMMING。ベクトルフィールドタイプとメトリックの詳細については、メトリックタイプおよびスキーマの説明を参照してください。
頻繁にアクセスされるベクトルフィールドとスカラーフィールドの両方に対してインデックスを作成することをお勧めします。コレクションに複数のベクトルフィールドが含まれている場合、各ベクトルフィールドごとに個別にインデックスを作成できます。
事前準備
コレクションの作成で説明されているように、コレクション作成リクエストにおいて以下の条件のいずれかが指定されている場合、Zilliz Cloudはコレクションを作成する際に自動的にインデックスを生成しメモリにロードします。
-
ベクトルフィールドの次元数とメトリックタイプ、または
-
スキーマとインデックスパラメータ。
以下のコードスニペットでは、既存のコードを再利用してZilliz Cloudに接続し、インデックスパラメータを指定せずにコレクションを作成します。この場合、コレクションにはインデックスが存在せず、アンロードされたままになります。
- Python
- Java
- NodeJS
from pymilvus import MilvusClient, DataType
CLUSTER_ENDPOINT = "YOUR_CLUSTER_ENDPOINT"
TOKEN = "YOUR_CLUSTER_TOKEN"
# 1. Milvusクライアントを設定
client = MilvusClient(
uri=CLUSTER_ENDPOINT,
token=TOKEN
)
# 2. スキーマを作成
# 2.1. スキーマを作成
schema = MilvusClient.create_schema(
auto_id=False,
enable_dynamic_field=True,
)
# 2.2. スキーマにフィールドを追加
schema.add_field(field_name="id", datatype=DataType.INT64, is_primary=True)
# dim値は1より大きい整数である必要があります
schema.add_field(field_name="vector", datatype=DataType.FLOAT_VECTOR, dim=5)
# 3. コレクションを作成
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. Milvusサーバーに接続
ConnectConfig connectConfig = ConnectConfig.builder()
.uri(CLUSTER_ENDPOINT)
.token(TOKEN)
.build();
MilvusClientV2 client = new MilvusClientV2(connectConfig);
// 2. コレクションを作成
// 2.1 スキーマを作成
CreateCollectionReq.CollectionSchema schema = client.createSchema();
// 2.2 スキーマにフィールドを追加
schema.addField(AddFieldReq.builder().fieldName("id").dataType(DataType.Int64).isPrimaryKey(true).autoID(false).build());
// 次元値は1より大きい整数である必要があります。
schema.addField(AddFieldReq.builder().fieldName("vector").dataType(DataType.FloatVector).dimension(5).build());
// 3 スキーマとインデックスパラメータなしでコレクションを作成
CreateCollectionReq customizedSetupReq = CreateCollectionReq.builder()
.collectionName("customized_setup")
.collectionSchema(schema)
.build();
client.createCollection(customizedSetupReq);
// 1. Milvusクライアントを設定
client = new MilvusClient({address, token});
// 2. コレクションのフィールドを定義
const fields = [
{
name: "id",
data_type: DataType.Int64,
is_primary_key: true,
auto_id: false
},
{
name: "vector",
data_type: DataType.FloatVector,
dim: 5 // dim値は1より大きくなければなりません。
},
]
// 3. コレクションを作成
res = await client.createCollection({
collection_name: "customized_setup",
fields: fields,
})
console.log(res.error_code)
// 出力
//
// Success
//
コレクションにインデックスを作成
コレクションに対してインデックスを作成するには、インデックスパラメータを設定してcreate_index()を呼び出す必要があります。
- Python
- Java
- NodeJS
# 4. インデックスを設定
# 4.1. インデックスパラメータを設定
index_params = MilvusClient.prepare_index_params()
# 4.2. ベクトルフィールドにインデックスを追加。
index_params.add_index(
field_name="vector",
metric_type="COSINE",
index_type="AUTOINDEX",
index_name="vector_index"
)
# 4.4. インデックスファイルを作成
client.create_index(
collection_name="customized_setup",
index_params=index_params
)
# 5. インデックスを説明
res = client.list_indexes(
collection_name="customized_setup"
)
import io.milvus.v2.common.IndexParam;
import io.milvus.v2.service.index.request.CreateIndexReq;
// 4 インデックスパラメータを準備
// 4.2 "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 インデックスファイルを作成
CreateIndexReq createIndexReq = CreateIndexReq.builder()
.collectionName("customized_setup")
.indexParams(indexParams)
.build();
client.createIndex(createIndexReq);
// 4. コレクションのインデックスを設定
// 4.1. インデックスパラメータを設定
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)
// 出力
//
// Success
//
提供されたコードスニペットでは、ベクトルフィールドにインデックスを確立して、インデックスタイプをAUTOINDEX、メトリックタイプをCOSINEに設定しています。さらに、スカラーフィールドにもインデックスタイプAUTOINDEXでインデックスが作成されています。インデックスタイプとメトリックタイプの詳細については、AUTOINDEXの説明およびメトリックタイプを参照してください。
現在、コレクション内の各フィールドに対して1つのインデックスファイルのみを作成できます。
インデックスの詳細を確認
インデックスを作成した後、その詳細を確認できます。
- Python
- Java
- NodeJS
# 5. インデックスを説明
res = client.list_indexes(
collection_name="customized_setup"
)
print(res)
# 出力
#
# [
# "vector_index"
# ]
res = client.describe_index(
collection_name="customized_setup",
index_name="vector_index"
)
print(res)
# 出力
#
# {
# "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. インデックスを説明
// 5.1 インデックス名を一覧表示
ListIndexesReq listIndexesReq = ListIndexesReq.builder()
.collectionName("customized_setup")
.build();
List<String> indexNames = client.listIndexes(listIndexesReq);
System.out.println(indexNames);
// 出力:
// [
// "vector_index"
// ]
// 5.2 インデックスを説明
DescribeIndexReq describeIndexReq = DescribeIndexReq.builder()
.collectionName("customized_setup")
.indexName("vector_index")
.build();
DescribeIndexResp describeIndexResp = client.describeIndex(describeIndexReq);
System.out.println(JSONObject.toJSON(describeIndexResp));
// 出力:
// {
// "metricType": "COSINE",
// "indexType": "AUTOINDEX",
// "fieldName": "vector",
// "indexName": "vector_index"
// }
// 5. インデックスを説明
res = await client.describeIndex({
collection_name: "customized_setup",
index_name: "vector_index"
})
console.log(JSON.stringify(res.index_descriptions, null, 2))
// 出力
//
// [
// {
// "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"
// }
// ]
//
特定のフィールドに作成されたインデックスファイルを確認し、このインデックスファイルを使用してインデックス化された行数の統計情報を収集できます。
インデックスの削除
必要なくなった場合、簡単にインデックスを削除できます。
インデックスを削除する前に、最初に解放されていることを確認してください。
- Python
- Java
- NodeJS
# 6. インデックスを削除
client.drop_index(
collection_name="customized_setup",
index_name="vector_index"
)
// 6. インデックスを削除
DropIndexReq dropIndexReq = DropIndexReq.builder()
.collectionName("customized_setup")
.indexName("vector_index")
.build();
client.dropIndex(dropIndexReq);
// 6. インデックスを削除
res = await client.dropIndex({
collection_name: "customized_setup",
index_name: "vector_index"
})
console.log(res.error_code)
// 出力
//
// Success
//
高度な機能
さらに、ご興味をお持ちになる可能性のあるいくつかの高度なベクトルインデックス機能があります。
インデックス構築レベルの調整 [READ MORE]
Zilliz Cloudは`buildlevel`というパラメータを導入しており、これによりユーザーは対象コレクションのストレージ容量と検索再現率のバランスを取ることができます。使用頻度が低いまたはより多くのストレージ容量が必要なコレクションでは、ストレージ容量の大幅な増加と引き換えにわずかな再現率の低下を許容できます。逆も同様です。このガイドでは、使用可能なオプションとコレクション用にインデックスを構築するためにそれらを使用する方法について説明します。
MINHASH_LSH [READ MORE]
効率的な重複除去と類似性検索は、大規模機械学習データセット、特に大規模言語モデル(LLM)の学習コーパスのクリーニングなどのタスクにとって重要です。数百万または数十億のドキュメントを扱う場合、従来の完全一致検索は遅くなりすぎてコストが高くなります。