optimize()

This operation optimizes the physical layout of a collection’s data, for example by compacting segments to a target size or rebuilding indexes.

public OptimizeTask optimize(OptimizeReq request)

Request Syntax

optimize(OptimizeReq.builder()
    .databaseName(String databaseName)
    .collectionName(String collectionName)
    .targetSize(String targetSize)
    .async(boolean async)
    .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.

  • targetSize(String targetSize)

    The target segment size, e.g. "512MB" or "1GB". A null value uses the server default.

  • async(boolean async)

    Whether to run the optimization in the background. When false (default), the operation blocks until the optimization finishes. When true, it returns an OptimizeTask immediately.

  • timeout(Long timeout)

    The timeout in milliseconds. A null value means no timeout.

RETURNS:

OptimizeTask

An OptimizeTask that tracks progress and provides the optimization result.

OptimizeTask

METHODS:

  • void start() — Starts the optimization task.

  • boolean isDone() — Returns whether the optimization has completed.

  • boolean isCancelled() — Returns whether the optimization was cancelled.

  • ProgressStage getProgress() — Returns the current progress stage.

  • List<String> getProgressHistoryAsStrings() — Returns the progress history as strings.

  • boolean cancel() — Attempts to cancel the optimization.

  • OptimizeResp getResult(Long timeoutMs) — Blocks until the result is available or the timeout elapses, then returns the OptimizeResp.

ProgressStage values: INITIALIZING, WAITING_FOR_INDEXES, COMPACTING, WAITING_FOR_COMPACTION, WAITING_FOR_INDEX_REBUILD, REFRESHING_LOAD, CANCELLED.

OptimizeResp

METHODS:

  • String getStatus() — Returns the operation status.

  • String getCollectionName() — Returns the name of the optimized collection.

  • Long getCompactionId() — Returns the compaction ID.

  • String getTargetSize() — Returns the target segment size.

  • List<String> getProgress() — Returns the progress messages.

EXCEPTIONS:

Example

import io.milvus.v2.client.ConnectConfig;
import io.milvus.v2.client.MilvusClientV2;
import io.milvus.v2.common.DataType;
import io.milvus.v2.service.collection.request.AddFieldReq;
import io.milvus.v2.service.collection.request.CreateCollectionReq;
import io.milvus.v2.common.IndexParam;
import io.milvus.v2.service.index.request.CreateIndexReq;
import io.milvus.v2.service.utility.OptimizeTask;
import io.milvus.v2.service.utility.request.OptimizeReq;
import java.util.Collections;

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

// 2. Create a collection with an "id" field and a "vector" field
CreateCollectionReq.CollectionSchema collectionSchema = MilvusClientV2.CreateSchema();
collectionSchema.addField(AddFieldReq.builder().fieldName("id").dataType(DataType.Int64).isPrimaryKey(Boolean.TRUE).autoID(Boolean.FALSE).build());
collectionSchema.addField(AddFieldReq.builder().fieldName("vector").dataType(DataType.FloatVector).dimension(2).build());
client.createCollection(CreateCollectionReq.builder()
        .collectionName("test")
        .collectionSchema(collectionSchema)
        .build());



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

// 4. Optimize the collection
OptimizeTask task = client.optimize(OptimizeReq.builder()
        .collectionName("test")
        .targetSize("2048MB")
        .build());

Try Managed Milvus for Free

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

Get Started
Feedback

Was this page helpful?