compact()
This operation starts a compaction job that merges small segments in a collection to improve storage layout and query efficiency.
Request Syntax
compact(
collection_name: str,
is_clustering: Optional[bool] = False,
is_l0: Optional[bool] = False,
target_size: Optional[int] = None,
target_size_unit: str = "mb",
timeout: Optional[float] = None,
**kwargs,
) -> int
PARAMETERS:
collection_name (str) -
[REQUIRED]
The name of the target collection.
is_clustering (bool) -
Whether to perform a clustering compaction. Defaults to False.
is_l0 (bool) -
Whether to perform an L0 compaction, which specifically handles L0 segments by merging delete operations into existing data segments. Defaults to False.
target_size (int) -
Target segment size after compaction. Must be a positive integer. The unit is specified by target_size_unit (default MB). If not provided, the server uses its default target size.
target_size_unit (str) -
Unit for target_size. Supported values: "b", "kb", “mb” (default), "gb", "tb", "pb". The value is converted to MB before being sent to the server.
timeout (float) -
The timeout duration for this operation. Setting this to None indicates that this operation timeouts when any response arrives or any error occurs.
kwargs (dict) -
Optional request context parameters.
RETURN TYPE:
int
RETURNS:
Compaction job ID for follow-up status queries.
A compaction job ID, which can be used to get the compaction status.
EXCEPTIONS:
ParamError
Raised when
target_sizeis not an integer or whentarget_size_unitis invalid.MilvusException
This exception will be raised when any error occurs during this operation.
Examples
from pymilvus import MilvusClient
client = MilvusClient(uri="http://localhost:19530", token="root:Milvus")
# Create a collection and insert some data
client.create_collection(collection_name="book_catalog", dimension=4)
client.insert(collection_name="book_catalog", data=[{"id": i, "vector": [0.1] * 4} for i in range(10)])
# Flush the data so that the inserted segments are sealed and become compactable
client.flush(collection_name="book_catalog")
job_id = client.compact(
collection_name="book_catalog",
target_size=1024,
target_size_unit="mb",
)
print(job_id)