FLAT
FLAT索引是最簡單直接的浮點向量索引和搜尋方法之一。它依賴粗暴的方法,將每個查詢向量直接與資料集中的每個向量進行比較,而不需要任何進階的預處理或資料結構化。這種方法保證了精確度,提供 100% 的回復率,因為每個潛在的匹配都會被評估。
不過,這種徹底的搜尋方法也有其優缺點。與 Milvus 中的其他索引類型相比,FLAT 索引是最慢的索引選項,因為它會對每個查詢執行資料集的完整掃描。因此,它並不適合擁有大量資料集的環境,因為在這種環境中,效能是最重要的。FLAT 索引的主要優點是簡單可靠,因為它不需要訓練或複雜的參數設定。
建立索引
要在 Milvus 的向量場上建立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_vector_field_name", # Name of the vector field to be indexed
index_type="FLAT", # Type of the index to create
index_name="vector_index", # Name of the index to create
metric_type="L2", # Metric type used to measure similarity
params={} # No additional parameters required for FLAT
)
在此設定中
index_type
:要建立的索引類型。在本範例中,設定值為FLAT
。metric_type
:用來計算向量間距離的方法。支援的值包括COSINE
,L2
, 和IP
。如需詳細資訊,請參閱公制類型。params
:FLAT 索引不需要額外的參數。
一旦配置好索引參數,就可以直接使用create_index()
方法或在create_collection
方法中傳入索引參數來建立索引。詳情請參閱建立集合。
在索引上搜尋
索引建立且實體插入後,您就可以在索引上執行相似性搜尋。
res = MilvusClient.search(
collection_name="your_collection_name", # Collection name
data=[[0.1, 0.2, 0.3, 0.4, 0.5]], # Query vector
limit=3, # TopK results to return
search_params={"params": {}} # No additional parameters required for FLAT
)
索引參數
對於 FLAT 索引,在索引建立或搜尋過程中都不需要額外的參數。