标量 Field Index
在 Milvus 中,标量 index 用于通过特定非向量 field 值加速元过滤,类似于传统数据库 index。本指南将指导您为整数、字符串等 field 创建和配置标量 index。
标量 index 类型
-
自动 index:Milvus 根据标量 field 的数据类型自动决定 index 类型。当您不需要控制特定 index 类型时,这是合适的选择。
-
自定义 index:您指定确切的 index 类型,例如倒排 index 或 bitmap index。这为 index 类型选择提供了更多控制。
自动 index
要使用自动 index,在 add_index()
中省略 index_type 参数,以便 Milvus 可以根据标量 field 类型推断 index 类型。
要使用自动 index,在 IndexParam
中省略 indexType 参数,以便 Milvus 可以根据标量 field 类型推断 index 类型。
要使用自动 index,在 createIndex()
中省略 index_type 参数,以便 Milvus 可以根据标量 field 类型推断 index 类型。
有关标量数据类型与默认 index 算法之间的映射关系,请参阅 标量 field index 算法。
# Auto indexing
client = MilvusClient(
uri="http://localhost:19530"
)
index_params = MilvusClient.prepare_index_params() # Prepare an empty IndexParams object, without having to specify any index parameters
index_params.add_index(
field_name="scalar_1", # Name of the scalar field to be indexed
index_type="", # Type of index to be created. For auto indexing, leave it empty or omit this parameter.
index_name="default_index" # Name of the index to be created
)
client.create_index(
collection_name="test_scalar_index", # Specify the collection name
index_params=index_params
)
import io.milvus.v2.common.IndexParam;
import io.milvus.v2.service.index.request.CreateIndexReq;
IndexParam indexParamForScalarField = IndexParam.builder()
.fieldName("scalar_1") // Name of the scalar field to be indexed
.indexName("default_index") // Name of the index to be created
.indexType("") // Type of index to be created. For auto indexing, leave it empty or omit this parameter.
.build();
List<IndexParam> indexParams = new ArrayList<>();
indexParams.add(indexParamForVectorField);
CreateIndexReq createIndexReq = CreateIndexReq.builder()
.collectionName("test_scalar_index") // Specify the collection name
.indexParams(indexParams)
.build();
client.createIndex(createIndexReq);
await client.createIndex({
collection_name: "test_scalar_index", // Specify the collection name
field_name: "scalar_1", // Name of the scalar field to be indexed
index_name: "default_index", // Name of the index to be created
index_type: "" // Type of index to be created. For auto indexing, leave it empty or omit this parameter.
})
Custom indexing
要使用自定义 index,在 add_index()
中使用 index_type 参数指定特定的 index 类型。
要使用自定义 index,在 IndexParam
中使用 indexType 参数指定特定的 index 类型。
要使用自定义 index,在 createIndex()
中使用 index_type 参数指定特定的 index 类型。
以下示例为标量 field scalar_2
创建倒排 index。
index_params = MilvusClient.prepare_index_params() # Prepare an IndexParams object
index_params.add_index(
field_name="scalar_2", # Name of the scalar field to be indexed
index_type="INVERTED", # Type of index to be created
index_name="inverted_index" # Name of the index to be created
)
client.create_index(
collection_name="test_scalar_index", # Specify the collection name
index_params=index_params
)
import io.milvus.v2.common.IndexParam;
import io.milvus.v2.service.index.request.CreateIndexReq;
IndexParam indexParamForScalarField = IndexParam.builder()
.fieldName("scalar_1") // Name of the scalar field to be indexed
.indexName("inverted_index") // Name of the index to be created
.indexType("INVERTED") // Type of index to be created
.build();
List<IndexParam> indexParams = new ArrayList<>();
indexParams.add(indexParamForVectorField);
CreateIndexReq createIndexReq = CreateIndexReq.builder()
.collectionName("test_scalar_index") // Specify the collection name
.indexParams(indexParams)
.build();
client.createIndex(createIndexReq);
await client.createIndex({
collection_name: "test_scalar_index", // Specify the collection name
field_name: "scalar_1", // Name of the scalar field to be indexed
index_name: "inverted_index", // Name of the index to be created
index_type: "INVERTED" // Type of index to be created
})
方法和参数
-
prepare_index_params()
准备一个 IndexParams 对象。
-
add_index()
向 IndexParams 对象添加 index 配置。
-
field_name (string)
要建立 index 的标量 field 名称。
-
index_type (string):
要创建的标量 index 类型。对于隐式 index,将其留空或省略此参数。
对于自定义 index,有效值包括:
-
index_name (string)
要创建的标量 index 名称。每个标量 field 支持一个 index。
-
-
create_index()
在指定的 collection 中创建 index。
-
collection_name (string)
要为其创建 index 的 collection 名称。
-
index_params
包含 index 配置的 IndexParams 对象。
-
方法和参数
- IndexParam
准备一个 IndexParam 对象。
- fieldName (String) 要建立 index 的标量 field 名称。
- indexName (String) 要创建的标量 index 名称。每个标量 field 支持一个 index。
- indexType (String)
要创建的标量 index 类型。对于隐式 index,将其留空或省略此参数。
对于自定义 index,有效值包括:
- INVERTED:(推荐)倒排 index 由包含所有按字母顺序排序的标记化单词的术语字典组成。有关详细信息,请参阅 标量 Index。
- STL_SORT:使用标准模板库排序算法对标量 field 进行排序。支持布尔和数字 field(例如 INT8、INT16、INT32、INT64、FLOAT、DOUBLE)。
- Trie:用于快速前缀搜索和检索的树数据结构。支持 VARCHAR field。
- CreateIndexReq
在指定的 collection 中创建 index。
- collectionName (String) 要为其创建 index 的 collection 名称。
- indexParams (List<IndexParam>) 包含 index 配置的 IndexParam 对象列表。
方法和参数
-
createIndex
在指定的 collection 中创建 index。
- collection_name (string) 要为其创建 index 的 collection 名称。
- field_name (string) 要建立 index 的标量 field 名称。
- index_name (string) 要创建的标量 index 名称。每个标量 field 支持一个 index。
- index_type (string)
要创建的标量 index 类型。对于隐式 index,将其留空或省略此参数。
对于自定义 index,有效值包括:
- INVERTED:(推荐)倒排 index 由包含所有按字母顺序排序的标记化单词的术语字典组成。有关详细信息,请参阅 标量 Index。
- STL_SORT:使用标准模板库排序算法对标量 field 进行排序。支持布尔和数字 field(例如 INT8、INT16、INT32、INT64、FLOAT、DOUBLE)。
- Trie:用于快速前缀搜索和检索的树数据结构。支持 VARCHAR field。
Verifying the result
使用 list_indexes()
方法验证标量 index 的创建:
使用 listIndexes()
方法验证标量 index 的创建:
使用 listIndexes()
方法验证标量 index 的创建:
client.list_indexes(
collection_name="test_scalar_index" # Specify the collection name
)
# Output:
# ['default_index','inverted_index']
import java.util.List;
import io.milvus.v2.service.index.request.ListIndexesReq;
ListIndexesReq listIndexesReq = ListIndexesReq.builder()
.collectionName("test_scalar_index") // Specify the collection name
.build();
List<String> indexNames = client.listIndexes(listIndexesReq);
System.out.println(indexNames);
// Output:
// [
// "default_index",
// "inverted_index"
// ]
res = await client.listIndexes({
collection_name: 'test_scalar_index'
})
console.log(res.indexes)
// Output:
// [
// "default_index",
// "inverted_index"
// ]
Limits
- 目前,标量 index 支持 INT8、INT16、INT32、INT64、FLOAT、DOUBLE、BOOL、VARCHAR 和 ARRAY 数据类型,但不支持 JSON 数据类型。