索引向量欄位
本指南將教您如何在集合中的向量欄位上建立和管理索引的基本操作。
概述
利用索引檔案中儲存的元資料,Milvus 將資料組織成專門的結構,方便在搜尋或查詢時快速檢索所需的資訊。
Milvus 提供多種索引類型和度量來排序欄位值,以進行有效的相似性搜尋。下表列出了不同向量欄位類型所支援的索引類型和度量。目前,Milvus 支援各種類型的向量資料,包括浮點內嵌 (通常稱為浮點向量或密集向量)、二進位內嵌 (也稱為二進位向量),以及稀疏內嵌 (也稱為稀疏向量)。如需詳細資訊,請參閱「記憶體內索引與相似度指標」。
度量類型 | 索引類型 |
---|---|
|
|
公制類型 | 索引類型 |
---|---|
|
|
公制類型 | 索引類型 |
---|---|
IP | sparse_inverted_index |
從 Milvus 2.5.4 起,SPARSE_WAND
已經被廢棄。取而代之,建議使用"inverted_index_algo": "DAAT_WAND"
以達到等效,同時保持相容性。如需詳細資訊,請參閱Sparse Vector。
建議為向量欄位和經常被存取的標量欄位建立索引。
準備工作
正如在管理集合中解釋的,如果在集合創建請求中指定了以下任何條件,Milvus 會在創建集合時自動生成索引並將其載入記憶體:
向量欄位的維度和度量類型,或
模式和索引參數。
下面的程式碼片段重新利用現有程式碼,建立與 Milvus 實例的連線,並在未指定索引參數的情況下建立集合。在這種情況下,該集合缺乏索引,並保持未載入狀態。
要準備索引,請使用 MilvusClient
連接至 Milvus 伺服器,並使用 create_schema()
, add_field()
和 create_collection()
.
要準備索引,使用 MilvusClientV2
連接至 Milvus 伺服器,並使用 createSchema()
, addField()
和 createCollection()
.
要準備索引,使用 MilvusClient
連接至 Milvus 伺服器,並使用 createCollection()
.
from pymilvus import MilvusClient, DataType
# 1. Set up a Milvus client
client = MilvusClient(
uri="http://localhost:19530"
)
# 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)
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 = "http://localhost:19530";
// 1. Connect to Milvus server
ConnectConfig connectConfig = ConnectConfig.builder()
.uri(CLUSTER_ENDPOINT)
.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());
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,
autoID: false
},
{
name: "vector",
data_type: DataType.FloatVector,
dim: 5
},
]
// 3. Create a collection
res = await client.createCollection({
collection_name: "customized_setup",
fields: fields,
})
console.log(res.error_code)
// Output
//
// Success
//
為資料集建立索引
要為一個集合建立索引或為一個集合建立索引,使用 prepare_index_params()
來準備索引參數,並使用 create_index()
來建立索引。
若要為集合建立索引或為集合建立索引,請使用 IndexParam
準備索引參數和 createIndex()
來建立索引。
若要為集合建立索引或為集合建立索引,請使用 createIndex()
.
# 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="IVF_FLAT",
index_name="vector_index",
params={ "nlist": 128 }
)
# 4.3. Create an index file
client.create_index(
collection_name="customized_setup",
index_params=index_params,
sync=False # Whether to wait for index creation to complete before returning. Defaults to True.
)
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.IVF_FLAT)
.metricType(IndexParam.MetricType.COSINE)
.extraParams(Map.of("nlist", 128))
.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",
params: { "nlist": 128 }
})
console.log(res.error_code)
// Output
//
// Success
//
參數 | 說明 |
---|---|
field_name |
應用此物件的目標檔案名稱。 |
metric_type |
用來測量向量間相似性的演算法。可能的值有IP、L2、COSINE、JACCARD、HAMMING。只有指定欄位為向量欄位時,此項才可用。如需詳細資訊,請參閱Milvus 支援的索引。 |
index_type |
用來排列特定欄位資料的演算法名稱。有關適用的演算法,請參閱「記憶體內索引」和「磁碟上索引」。 |
index_name |
應用此物件後產生的索引檔案名稱。 |
params |
指定索引類型的微調參數。有關可能的關鍵和值範圍的詳細資訊,請參閱「In-memory Index」。 |
collection_name |
現有集合的名稱。 |
index_params |
包含IndexParam物件清單的IndexParams物件。 |
sync |
控制索引的建立方式與用戶端的請求有關。有效值:
|
參數 | 說明 |
---|---|
fieldName |
應用此 IndexParam 物件的目標欄位名稱。 |
indexName |
應用此物件後所產生的索引檔案名稱。 |
indexType |
用於排列特定欄位資料的演算法名稱。有關適用的演算法,請參閱「記憶體內索引」和「磁碟上索引」。 |
metricType |
索引要使用的距離公制。可能的值為IP、L2、COSINE、JACCARD、HAMMING。 |
extraParams |
額外索引參數。如需詳細資訊,請參閱「記憶體索引」和「磁碟上索引」。 |
參數 | 說明 |
---|---|
collection_name |
現有資料集的名稱。 |
field_name |
要建立索引的欄位名稱。 |
index_type |
要建立索引的類型。 |
metric_type |
用於量測向量距離的度量類型。 |
index_name |
要建立的索引名稱。 |
params |
其他特定於索引的參數。 |
注意事項
目前,您只能為集合中的每個欄位建立一個索引檔案。
檢查索引詳細資料
建立索引後,您可以檢查其詳細資料。
要檢查索引詳細資訊,請使用 list_indexes()
列出索引名稱,並使用 describe_index()
來取得索引詳細資料。
要檢查索引詳細資訊,請使用 describeIndex()
來取得索引詳細資料。
要檢查索引詳細資訊,請使用 describeIndex()
來取得索引詳細資料。
# 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": ,
# "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_index()
.
要丟棄索引,請使用 dropIndex()
.
要刪除索引,請使用 dropIndex()
.
# 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
//