跳到主要内容

FLAT

FLAT index 是用于索引和搜索浮点向量最简单、最直接的方法之一。它依赖于暴力搜索方法,其中每个查询向量直接与数据集中的每个向量进行比较,没有任何高级预处理或数据结构化。这种方法保证准确性,提供 100% 的召回率,因为每个潜在匹配都会被评估。

然而,这种穷举搜索方法有所权衡。FLAT index 是最慢的索引选项,因为它对每个查询都执行数据集的完全扫描。因此,它不适合有大量数据集且性能有关注的环境。FLAT index 的主要优势是其简单性和可靠性,因为它不需要训练或复杂的参数配置。

构建 Index

要在 Milvus 中的向量 field 上构建 FLAT index,使用 add_index() 方法,为 index 指定 index_typemetric_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:要构建的 index 类型。在此示例中,将值设置为 FLAT

  • metric_type:用于计算向量之间距离的方法。支持的值包括 COSINEL2IP。有关详细信息,请参阅 度量类型

  • params:FLAT index 不需要额外参数。

配置 index 参数后,您可以直接使用 create_index() 方法创建 index,或在 create_collection 方法中传递 index 参数。有关详细信息,请参阅 创建 Collection

在 Index 上搜索

构建 index 并插入 entity 后,您可以在 index 上执行相似性搜索。

res = MilvusClient.search(
collection_name="your_collection_name", # Collection name
anns_field="vector_field", # Vector field 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
)

Index 参数

对于 FLAT index,在 index 创建或搜索过程中都不需要额外的参数。