跳到主要内容
版本:v3.0.x

管理 Partition

Partition 是一个 Collection 的子集。每个 Partition 与其父 Collection 共享相同的数据结构,但只包含 Collection 中的一个数据子集。本页将帮助你了解如何管理 Partition。

Partition 概述

创建一个 Collection 时,Milvus 也会在该 Collection 中创建一个名为**_default 的**Partition。如果不添加其他 Partition,所有插入到 Collection 中的实体都会进入默认 Partition,所有搜索和查询也都在默认 Partition 内进行。

您可以添加更多 Partition,并根据特定条件将实体插入其中。这样就可以限制在某些 Partition 内进行搜索和查询,从而提高搜索性能。

一个 Collection 最多可以有 1,024 个 Partition。

说明 Partition Key功能是基于 Partition 的搜索优化,允许 Milvus 根据特定标量字段中的值将实体分配到不同的 Partition 中。该功能有助于实现面向 Partition 的多租户,并提高搜索性能。 本页将不讨论此功能。要了解更多信息,请参阅使用 Partition Key

列出 Partition

创建 Collection 时,Milvus 还会在该 Collection 中创建一个名为**_default 的**Partition。您可以按以下方式列出 Collection 中的 Partition。

from pymilvus import MilvusClient

client = MilvusClient(
uri="http://localhost:19530",
token="root:Milvus"
)

res = client.list_partitions(
collection_name="my_collection"
)

print(res)

# Output
#
# ["_default"]
import io.milvus.v2.service.partition.request.ListPartitionsReq;
import io.milvus.v2.client.ConnectConfig;
import io.milvus.v2.client.MilvusClientV2;

import java.util.*;

String CLUSTER_ENDPOINT = "http://localhost:19530";
String TOKEN = "root:Milvus";

// 1. Connect to Milvus server
ConnectConfig connectConfig = ConnectConfig.builder()
.uri(CLUSTER_ENDPOINT)
.token(TOKEN)
.build();

MilvusClientV2 client = new MilvusClientV2(connectConfig);

ListPartitionsReq listPartitionsReq = ListPartitionsReq.builder()
.collectionName("my_collection")
.build();

List<String> partitionNames = client.listPartitions(listPartitionsReq);
System.out.println(partitionNames);

// Output:
// [_default]
import { MilvusClient, DataType } from "@zilliz/milvus2-sdk-node";

const address = "http://localhost:19530";
const token = "root:Milvus";
const client = new MilvusClient({address, token});

let res = await client.listPartitions({
collection_name: "my_collection"
})

console.log(res);

// Output
// ["_default"]
import (
"context"

"github.com/milvus-io/milvus/client/v2/milvusclient"
)

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

milvusAddr := "localhost:19530"

client, err := milvusclient.New(ctx, &milvusclient.ClientConfig{
Address: milvusAddr,
})
if err != nil {
fmt.Println(err.Error())
// handle error
}
defer client.Close(ctx)

partitionNames, err := client.ListPartitions(ctx, milvusclient.NewListPartitionOption("my_collection"))
if err != nil {
fmt.Println(err.Error())
// handle error
}

fmt.Println(partitionNames)
export CLUSTER_ENDPOINT="http://localhost:19530"
export TOKEN="root:Milvus"

curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/partitions/list" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
--header "Request-Timeout: 10" \
-d '{
"collectionName": "my_collection"
}'

# {
# "code": 0,
# "data": [
# "_default"
# ]
# }

创建 Partition

您可以向 Collection 添加更多 Partition,并根据特定条件向这些 Partition 插入实体。

client.create_partition(
collection_name="my_collection",
partition_name="partitionA"
)

res = client.list_partitions(
collection_name="my_collection"
)

print(res)

# Output
#
# ["_default", "partitionA"]
import io.milvus.v2.service.partition.request.CreatePartitionReq;

CreatePartitionReq createPartitionReq = CreatePartitionReq.builder()
.collectionName("my_collection")
.partitionName("partitionA")
.build();

client.createPartition(createPartitionReq);

ListPartitionsReq listPartitionsReq = ListPartitionsReq.builder()
.collectionName("my_collection")
.build();

List<String> partitionNames = client.listPartitions(listPartitionsReq);
System.out.println(partitionNames);

// Output:
// [_default, partitionA]
await client.createPartition({
collection_name: "my_collection",
partition_name: "partitionA"
})

res = await client.listPartitions({
collection_name: "my_collection"
})

console.log(res)

// Output
// ["_default", "partitionA"]
import (
"fmt"

client "github.com/milvus-io/milvus/client/v2/milvusclient"
)

ctx, cancel := context.WithCancel(context.Background())
defer cancel()

err = client.CreatePartition(ctx, milvusclient.NewCreatePartitionOption("my_collection", "partitionA"))
if err != nil {
fmt.Println(err.Error())
// handle error
}

partitionNames, err := client.ListPartitions(ctx, milvusclient.NewListPartitionOption("my_collection"))
if err != nil {
fmt.Println(err.Error())
// handle error
}

fmt.Println(partitionNames)
// Output
// ["_default", "partitionA"]
export CLUSTER_ENDPOINT="http://localhost:19530"
export TOKEN="root:Milvus"

curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/partitions/create" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
--header "Request-Timeout: 10" \
-d '{
"collectionName": "my_collection",
"partitionName": "partitionA"
}'

# {
# "code": 0,
# "data": {}
# }

curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/partitions/list" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
--header "Request-Timeout: 10" \
-d '{
"collectionName": "my_collection"
}'

# {
# "code": 0,
# "data": [
# "_default",
# "partitionA"
# ]
# }

检查特定 Partition

以下代码片段演示了如何检查特定 Collection 中是否存在 Partition。

res = client.has_partition(
collection_name="my_collection",
partition_name="partitionA"
)

print(res)

# Output
#
# True
import io.milvus.v2.service.partition.request.HasPartitionReq;

HasPartitionReq hasPartitionReq = HasPartitionReq.builder()
.collectionName("my_collection")
.partitionName("partitionA")
.build();

Boolean hasPartitionRes = client.hasPartition(hasPartitionReq);
System.out.println(hasPartitionRes);

// Output:
// true
res = await client.hasPartition({
collection_name: "my_collection",
partition_name: "partitionA"
})

console.log(res.value)

// Output
// true
result, err := client.HasPartition(ctx, milvusclient.NewHasPartitionOption("my_collection", "partitionA"))
if err != nil {
fmt.Println(err.Error())
// handle error
}

fmt.Println(result)

// Output:
// true
export CLUSTER_ENDPOINT="http://localhost:19530"
export TOKEN="root:Milvus"

curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/partitions/has" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
--header "Request-Timeout: 10" \
-d '{
"collectionName": "my_collection",
"partitionName": "partitionA"
}'

# {
# "code": 0,
# "data": {
# "has": true
# }
# }

加载和释放 Partition

您可以分别加载或释放一个或多个 Partition。

加载 Partition

可以分别加载 Collection 中的特定 Partition。值得注意的是,如果 Collection 中存在未加载的 Partition,则 Collection 的加载状态会保持未加载状态。

client.load_partitions(
collection_name="my_collection",
partition_names=["partitionA"]
)

res = client.get_load_state(
collection_name="my_collection",
partition_name="partitionA"
)

print(res)
# Output
#
# {
# "state": "<LoadState: Loaded>"
# }
import io.milvus.v2.service.partition.request.LoadPartitionsReq;
import io.milvus.v2.service.collection.request.GetLoadStateReq;

LoadPartitionsReq loadPartitionsReq = LoadPartitionsReq.builder()
.collectionName("my_collection")
.partitionNames(Collections.singletonList("partitionA"))
.build();

client.loadPartitions(loadPartitionsReq);

GetLoadStateReq getLoadStateReq = GetLoadStateReq.builder()
.collectionName("my_collection")
.partitionName("partitionA")
.build();

Boolean getLoadStateRes = client.getLoadState(getLoadStateReq);
System.out.println(getLoadStateRes);

// True
await client.loadPartitions({
collection_name: "my_collection",
partition_names: ["partitionA"]
})

res = await client.getLoadState({
collection_name: "my_collection",
partition_name: "partitionA"
})

console.log(res)

// Output
//
// LoadStateLoaded
//
task, err := client.LoadPartitions(ctx, milvusclient.NewLoadPartitionsOption("my_collection", "partitionA"))
if err != nil {
fmt.Println(err.Error())
// handle error
}

// sync wait collection to be loaded
err = task.Await(ctx)
if err != nil {
fmt.Println(err.Error())
// handle error
}

state, err := client.GetLoadState(ctx, milvusclient.NewGetLoadStateOption("my_collection", "partitionA"))
if err != nil {
fmt.Println(err.Error())
// handle error
}
fmt.Println(state)
export CLUSTER_ENDPOINT="http://localhost:19530"
export TOKEN="root:Milvus"

curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/partitions/load" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
--header "Request-Timeout: 10" \
-d '{
"collectionName": "my_collection",
"partitionNames": ["partitionA"]
}'

# {
# "code": 0,
# "data": {}
# }

curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/collections/get_load_state" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
--header "Request-Timeout: 10" \
-d '{
"collectionName": "my_collection",
"partitionNames": ["partitionA"]
}'

# {
# "code": 0,
# "data": {
# "loadProgress": 100,
# "loadState": "LoadStateLoaded",
# "message": ""
# }
# }

释放 Partition

您还可以释放特定 Partition。

client.release_partitions(
collection_name="my_collection",
partition_names=["partitionA"]
)

res = client.get_load_state(
collection_name="my_collection",
partition_name="partitionA"
)

print(res)

# Output
#
# {
# "state": "<LoadState: NotLoaded>"
# }
import io.milvus.v2.service.partition.request.ReleasePartitionsReq;

ReleasePartitionsReq releasePartitionsReq = ReleasePartitionsReq.builder()
.collectionName("my_collection")
.partitionNames(Collections.singletonList("partitionA"))
.build();

client.releasePartitions(releasePartitionsReq);

GetLoadStateReq getLoadStateReq = GetLoadStateReq.builder()
.collectionName("my_collection")
.partitionName("partitionA")
.build();

Boolean getLoadStateRes = client.getLoadState(getLoadStateReq);
System.out.println(getLoadStateRes);

// False
await client.releasePartitions({
collection_name: "my_collection",
partition_names: ["partitionA"]
})

res = await client.getLoadState({
collection_name: "my_collection",
partition_name: "partitionA"
})

console.log(res)

// Output
//
// LoadStateNotLoaded
//
err = client.ReleasePartitions(ctx, milvusclient.NewReleasePartitionsOptions("my_collection", "partitionA"))
if err != nil {
fmt.Println(err.Error())
// handle error
}

state, err := client.GetLoadState(ctx, milvusclient.NewGetLoadStateOption("my_collection", "partitionA"))
if err != nil {
fmt.Println(err.Error())
// handle error
}
fmt.Println(state)
export CLUSTER_ENDPOINT="http://localhost:19530"
export TOKEN="root:Milvus"

curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/partitions/release" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
--header "Request-Timeout: 10" \
-d '{
"collectionName": "my_collection",
"partitionNames": ["partitionA"]
}'

# {
# "code": 0,
# "data": {}
# }

curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/collections/get_load_state" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
--header "Request-Timeout: 10" \
-d '{
"collectionName": "my_collection",
"partitionNames": ["partitionA"]
}'

# {
# "code": 0,
# "data": {
# "loadProgress": 0,
# "loadState": "LoadStateNotLoaded",
# "message": ""
# }
# }

Partition 内的数据操作符

插入和删除实体

您可以在特定操作符中执行插入、向 upsert 和删除操作。有关详情,请参阅

搜索和查询

可以在特定 Partition 内进行搜索和查询。详情请参阅

删除 Partition

您可以丢弃不再需要的 Partition。在丢弃 Partition 之前,请确保该 Partition 已被释放。

client.release_partitions(
collection_name="my_collection",
partition_names=["partitionA"]
)

client.drop_partition(
collection_name="my_collection",
partition_name="partitionA"
)

res = client.list_partitions(
collection_name="my_collection"
)

print(res)

# ["_default"]
import io.milvus.v2.service.partition.request.DropPartitionReq;
import io.milvus.v2.service.partition.request.ReleasePartitionsReq;
import io.milvus.v2.service.partition.request.ListPartitionsReq;

ReleasePartitionsReq releasePartitionsReq = ReleasePartitionsReq.builder()
.collectionName("my_collection")
.partitionNames(Collections.singletonList("partitionA"))
.build();

client.releasePartitions(releasePartitionsReq);

DropPartitionReq dropPartitionReq = DropPartitionReq.builder()
.collectionName("my_collection")
.partitionName("partitionA")
.build();

client.dropPartition(dropPartitionReq);

ListPartitionsReq listPartitionsReq = ListPartitionsReq.builder()
.collectionName("my_collection")
.build();

List<String> partitionNames = client.listPartitions(listPartitionsReq);
System.out.println(partitionNames);

// Output:
// [_default]
await client.releasePartitions({
collection_name: "my_collection",
partition_names: ["partitionA"]
})

await client.dropPartition({
collection_name: "my_collection",
partition_name: "partitionA"
})

res = await client.listPartitions({
collection_name: "my_collection"
})

console.log(res)

// Output
// ["_default"]
err = client.ReleasePartitions(ctx, milvusclient.NewReleasePartitionsOptions("my_collection", "partitionA"))
if err != nil {
fmt.Println(err.Error())
// handle error
}

err = client.DropPartition(ctx, milvusclient.NewDropPartitionOption("my_collection", "partitionA"))
if err != nil {
fmt.Println(err.Error())
// handle error
}

partitionNames, err := client.ListPartitions(ctx, milvusclient.NewListPartitionOption("my_collection"))
if err != nil {
fmt.Println(err.Error())
// handle error
}
fmt.Println(partitionNames)
export CLUSTER_ENDPOINT="http://localhost:19530"
export TOKEN="root:Milvus"

curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/partitions/release" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
--header "Request-Timeout: 10" \
-d '{
"collectionName": "my_collection",
"partitionNames": ["partitionA"]
}'

# {
# "code": 0,
# "data": {}
# }

curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/partitions/drop" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
--header "Request-Timeout: 10" \
-d '{
"collectionName": "my_collection",
"partitionName": "partitionA"
}'

# {
# "code": 0,
# "data": {}
# }

curl --request POST \
--url "${CLUSTER_ENDPOINT}/v2/vectordb/partitions/list" \
--header "Authorization: Bearer ${TOKEN}" \
--header "Content-Type: application/json" \
--header "Request-Timeout: 10" \
-d '{
"collectionName": "my_collection"
}'

# {
# "code": 0,
# "data": [
# "_default"
# ]
# }