跳到主要内容

管理 Partition

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

概述

创建 collection 时,Milvus 也会在 collection 中创建一个名为 _default 的 partition。如果您不打算添加任何其他 partition,则插入到 collection 中的所有实体都会进入默认 partition,所有搜索和查询也会在默认 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" \
-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" \
-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" \
-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" \
-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" \
-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" \
-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" \
-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" \
-d '{
"collectionName": "my_collection",
"partitionNames": ["partitionA"]
}'

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

Partition 内的数据操作

插入和删除实体

您可以在特定 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" \
-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" \
-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" \
-d '{
"collectionName": "my_collection"
}'

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