BIN_IVF_FLAT
BIN_IVF_FLAT索引是IVF_FLAT索引的變體,專門用於二進位嵌入。它首先將向量資料分割成多個叢集(nlist 單元),然後將目標輸入向量與每個叢集的中心進行比較,從而提高查詢效率。BIN_IVF_FLAT 可大幅減少查詢時間,同時允許使用者微調精確度與速度之間的平衡。如需詳細資訊,請參閱IVF_FLAT。
建立索引
要在 Milvus 中建立向量場的BIN_IVF_FLAT 索引,請使用add_index() 方法,指定index_type,metric_type, 以及索引的附加參數。
from pymilvus import MilvusClient
# Prepare index building params
index_params = MilvusClient.prepare_index_params()
index_params.add_index(
field_name="your_binary_vector_field_name", # Name of the vector field to be indexed
index_type="BIN_IVF_FLAT", # Type of the index to create
index_name="vector_index", # Name of the index to create
metric_type="HAMMING", # Metric type used to measure similarity
params={
"nlist": 64, # Number of clusters for the index
} # Index building params
)
在此設定中
index_type:要建立的索引類型。在本範例中,設定值為BIN_IVF_FLAT。metric_type:用來計算向量間距離的方法。二元嵌入的支援值包括HAMMING(預設值) 和JACCARD。詳情請參閱公制類型。params:建立索引的附加設定選項。nlist:分割資料集的叢集數。
要瞭解
BIN_IVF_FLAT索引可用的更多建立參數,請參閱索引建立參數。
一旦配置好索引參數,您就可以直接使用create_index() 方法或在create_collection 方法中傳入索引參數來建立索引。如需詳細資訊,請參閱建立集合。
在索引上搜尋
索引建立且實體插入後,您就可以在索引上執行相似性搜尋。
search_params = {
"params": {
"nprobe": 10, # Number of clusters to search
}
}
res = MilvusClient.search(
collection_name="your_collection_name", # Collection name
anns_field="binary_vector_field", # Binary vector field
data=[query_binary_vector], # Query binary vector
limit=3, # TopK results to return
search_params=search_params
)
在此配置中
params:在索引上搜尋的其他設定選項。nprobe:要搜尋的群集數量。
要瞭解
BIN_IVF_FLAT索引可用的更多搜尋參數,請參閱特定於索引的搜尋參數。
索引參數
本節概述用於建立索引和在索引上執行搜尋的參數。
索引建立參數
下表列出了建立索引時可在params 中設定的參數。
參數 |
說明 |
值範圍 |
調整建議 |
|---|---|---|---|
|
在建立索引時,使用 k-means 演算法建立的叢集數目。 每個叢集由一個中心點代表,儲存向量清單。增加此參數可減少每個叢集中的向量數量,從而建立更小、更集中的分割。 |
類型:整數範圍:[1, 65536] 預設值: |
較大的 |
特定於索引的搜尋參數
下表列出在索引上搜尋時,可在search_params.params 中設定的參數。
參數 |
說明 |
值範圍 |
調整建議 |
|---|---|---|---|
|
搜尋候選資料的叢集數。 較高的值允許搜尋更多的叢集,藉由擴大搜尋範圍來改善召回率,但代價是增加查詢延遲。 |
類型:整數範圍:[1,nlist] 預設值: |
增加此值可提高召回率,但可能會減慢搜尋速度。請依 在大多數情況下,我們建議您設定此範圍內的值:[1, nlist]。 |