创建 Scalar Index
支持通过标量索引(Scalar index)加速非向量字段的元数据过滤。Scalar index 类似传统数据库中的索引。
本教程将介绍如何针对数据类型为整数、字符串等的标量字段创建和设置 Scalar index。
自动索引(Auto indexing)
Milvus 能够根据标量字段类型推理索引类型。如需使用自动索引,请在创建索引时省略 index_type 参数。更多有关标量字段数据类型和默认索引算法的对应关系,请参考标量字段索引算法。
以下为示例代码:
- Python
- Java
- NodeJS
# Auto indexing
CLUSTER_ENDPOINT = "YOUR_CLUSTER_ENDPOINT"
TOKEN = "YOUR_CLUSTER_TOKEN"
# 1. Set up a Milvus client
client = MilvusClient(
uri=CLUSTER_ENDPOINT,
token=TOKEN
)
index_params = client.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
# highlight-next-line
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
// highlight-next-line
.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
// highlight-next-line
index_type: "" // Type of index to be created. For auto indexing, leave it empty or omit this parameter.
})