createIndex()

This operation creates an index for a specific collection.

public void createIndex(CreateIndexReq request)

Request Syntax

createIndex(CreateIndexReq.builder()
    .databaseName(String databaseName)
    .collectionName(String collectionName)
    .indexParams(List<IndexParam> indexParams)
    .sync(Boolean sync)
    .timeout(Long timeout)
    .build()
);

BUILDER METHODS:

  • databaseName(String databaseName) -

    The name of the database. Defaults to the current database if not specified.

  • collectionName(String collectionName) -

    The name of the target collection.

  • indexParams(List<IndexParam> indexParams) -

    A list of IndexParam objects defining the index configuration.

  • sync(Boolean sync) -

    Whether to wait synchronously until the operation completes. Defaults to Boolean.TRUE.

  • timeout(Long timeout) -

    The timeout duration in milliseconds. Defaults to 60000L.

RETURNS:

void

EXCEPTIONS:

  • MilvusClientException

    This exception will be raised when any error occurs during this operation.

Example

import io.milvus.v2.client.ConnectConfig;
import io.milvus.v2.client.MilvusClientV2;
import io.milvus.v2.common.IndexParam;
import io.milvus.v2.service.index.request.CreateIndexReq;

// 1. Set up a client
ConnectConfig connectConfig = ConnectConfig.builder()
        .uri("http://localhost:19530")
        .token("root:Milvus")
        .build();

MilvusClientV2 client = new MilvusClientV2(connectConfig);

// 2. Create an index for the field "vector"
IndexParam indexParam = IndexParam.builder()
        .metricType(IndexParam.MetricType.L2)
        .indexType(IndexParam.IndexType.AUTOINDEX)
        .fieldName("vector")
        .build();
CreateIndexReq createIndexReq = CreateIndexReq.builder()
        .collectionName("test")
        .indexParams(Collections.singletonList(indexParam))
        .build();
client.createIndex(createIndexReq);

Try Managed Milvus for Free

Zilliz Cloud is hassle-free, powered by Milvus and 10x faster.

Get Started
Feedback

Was this page helpful?