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
方法中传递索引参数来创建索引。详情请参阅创建 Collections。
在索引上搜索
建立索引并插入实体后,就可以在索引上执行相似性搜索。
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 索引,在创建索引或搜索过程中都不需要额外的参数。