跳到主要内容

createIndex()

The MilvusClient interface. This method creates an index on a field in the specified collection.

R<RpcStatus> createIndex(CreateIndexParam requestParam);

CreateIndexParam

Use the CreateIndexParam.Builder to construct a CreateIndexParam object.

import io.milvus.param.CreateIndexParam;
CreateIndexParam.Builder builder = CreateIndexParam.newBuilder()

Methods of CreateIndexParam.Builder:

Method

Description

Parameters

withCollectionName(
String collectionName)

Set the target collection name. Collection name cannot be empty or null.

collectionName: The name of the target collection to create an index for.

withDatabaseName(String databaseName)

Sets the database name. database name can be null for default database.

databaseName: The database name.

withFieldName(String fieldName)

Set the target field name. Field name cannot be empty or null.

fieldName: The target field name

withIndexType(IndexType indexType)

Set the index type. Please refer to IndexType in Misc.

indexType: The index type

withIndexName(String indexName)

Set the name of index which will be created. Then you can use the index name to check the state of index. If no index name is specified, the default index name is empty string which means let the server determine it. The max length of index name is 255 characters.

indexName: The name of the index

withMetricType(MetricType metricType)

Sets the metric type. Please refer to MetricType in Misc.

metricType: The metric type


withExtraParam(String extraParam)


Sets the specific index parameters according to index type. For example, IVF index, the extra parameters can be "{"nlist":1024}".

extraParam:
Extra parameters in JSON format

withSyncMode(Boolean syncMode)

Enable sync mode. For sync mode, the client keeps waiting until all segments of the collection are successfully indexed. If sync mode is disabled, the createIndex() returns instantly. By default sync mode is enabled.

syncMode: true is sync mode

withSyncWaitingInterval(Long milliseconds)

Set the waiting interval in sync mode. With sync mode enabled, the client constantly checks index state by interval. Interval value must be greater than zero, and cannot be greater than Constant.MAX_WAITING_INDEX_INTERVAL. By default, interval value is 500 milliseconds.


milliseconds: Sync mode interval value(unit: millisecond)


withSyncWaitingTimeout( Long seconds)

Set the timeout value for sync mode. Timeout value must be greater than zero and no upper limit. Default value is 600 seconds.

seconds: Sync mode timeout value(unit: second)

build()

Construct a CreateAliasParam object.

N/A

The CreateIndexParam.Builder.build() can throw the following exceptions:

  • ParamException: error if the parameter is invalid.

Returns

This method catches all the exceptions and returns an R<RpcStatus> object.

  • If the API fails on the server side, it returns the error code and message from the server.

  • If the API fails by RPC exception, it returns R.Status.Unknown and the error message of the exception.

  • If the API succeeds, it returns R.Status.Success.

Example

import io.milvus.param.*;

CreateIndexParam param = CreateIndexParam.newBuilder()
.withCollectionName(COLLECTION_NAME)
.withFieldName("field1")
.withIndexType(IndexType.IVF_FLAT)
.withMetricType(MetricType.L2)
.withExtraParam("{\"nlist\":64}")
.build();
R<RpcStatus> response = client.createIndex(param)
if (response.getStatus() != R.Status.Success.getCode()) {
System.out.println(response.getMessage());
}