Partition()

This is the constructor method to create a partition in the specified collection.

Invocation

Partition(collection, name, description='', **kwargs)

Parameters

ParameterDescriptionTypeRequired
collectionName of the collectionStringTrue
nameName of the partition to createStringTrue
descriptionDescription of the collectionStringFalse

Return

A new partition object created with the specified name.

Properties

PropertyDescriptionType
nameName of the partitionString
descriptionDescription of the collectionString
is_emptyBoolean value to indicate if the partition is emptyBool
num_entitiesNumber of entities in the partitionInteger

Example

from pymilvus import Partition
partition = Partition("book", "novel", "")

delete()

This method deletes entities from a specified partition.

Invocation

delete(expr, timeout=None, **kwargs)

Parameters

ParameterDescriptionTypeRequired
exprBoolean expression that specifies the primary keys of the entities to deleteStringTrue
timeoutAn optional duration of time in seconds to allow for the RPC. If it is set to None, the client keeps waiting until the server responds or error occurs.FloatFalse

Return

A MutationResult object.

Properties

PropertyDescriptionType
delete_countNumber of the entities to deleteInteger

Raises

  • RpcError: error if gRPC encounter an error.
  • ParamError: error if the parameters are invalid.
  • BaseException: error if the return result from server is not ok.

Example

import random
data = [
  [i for i in range(2000)],
  [i for i in range(10000, 12000)],
  [[random.random() for _ in range(2)] for _ in range(2000)],
]
from pymilvus import Partition
partition = Partition("book", "novel", "")
partition.insert(data)
expr = "book_id in [0,1]"
partition.delete(expr)

drop()

This method drops a partition and the data within.

Invocation

drop(timeout=None, **kwargs)

Parameters

ParameterDescriptionTypeRequired
timeoutAn optional duration of time in seconds to allow for the RPC. If it is set to None, the client keeps waiting until the server responds or error occurs.FloatFalse

Return

No return.

Raises

PartitionNotExistException: error if the partition does not exist.

Example

from pymilvus import Partition
partition = Partition("book", "novel", "")
partition.drop()

get_replicas()

This method checks the replica information.

Invocation

get_replicas()

Return

The information about replica groups and the corresponding query nodes and shard.

Raises

Example

from pymilvus import Partition
partition = Partition("book", "novel")
partition.load(replica_number=2)    # Load partition as 2 replicas
result = partition.get_replicas()
print(result)

insert()

This method inserts data into a specified partition.

Invocation

insert(data, timeout=None, **kwargs)

Parameters

ParameterDescriptionTypeRequired
dataData to insertlist-like(list, tuple)True
timeoutAn optional duration of time in seconds to allow for the RPC. If it is set to None, the client keeps waiting until the server responds or error occurs.FloatFalse

Return

A MutationResult object.

Properties

PropertyDescriptionType
insert_countNumber of the inserted entitiesInteger
primary_keysList of the primary keys of the inserted entitieslist[String]

Raises

PartitionNotExistException: error if the partition does not exist.

Example

import random
data = [
  [i for i in range(2000)],
  [i for i in range(10000, 12000)],
  [[random.random() for _ in range(2)] for _ in range(2000)],
]
from pymilvus import Partition
partition = Partition("book", "novel", "")
partition.insert(data)

load()

This method loads the specified partition to memory (for search or query).

Invocation

load(timeout=None, **kwargs)

Parameters

ParameterDescriptionTypeRequired
replica_numberNumber of the replica(s) to loadIntegerFalse
timeoutAn optional duration of time in seconds to allow for the RPC. If it is set to None, the client keeps waiting until the server responds or error occurs.FloatFalse
kwargs
  • _async

  • Boolean value to indicate if to invoke asynchronously
BoolFalse

Return

No return.

Raises

InvalidArgumentException: error if the argument is invalid.

Example

from pymilvus import Partition
partition = Partition("book", "novel", "")
partition.load()

query()

This method conducts a vector query in a specified partition.

Invocation

query(expr, output_fields=None, timeout=None, **kwargs)

Parameters

ParameterDescriptionTypeRequired
exprBoolean expression to filter the dataStringTrue
output_fieldsList of names of fields to outputlist[String]False
timeoutAn optional duration of time in seconds to allow for the RPC. If it is set to None, the client keeps waiting until the server responds or error occurs.FloatFalse
kwargs
  • consistency_level
  • guarantee_timestamp
  • graceful_time
  • travel_timestamp

  • Consistency level used in the search
  • Milvus searches on the data view before this timestamp when it is provided. Otherwise, it searches the most updated data view. It can be only used in Customized level of consistency.
  • PyMilvus will use current timestamp minus the graceful_time as the guarantee_timestamp for search. It can be only used in Bounded level of consistency.
  • Timestamp that is used for Time Travel. Users can specify a timestamp in a search to get results based on a data view at a specified point in time.

  • String/Integer
  • Integer
  • Integer
  • Integer
False

Return

A list that contains all results.

Raises

  • RpcError: error if gRPC encounter an error.
  • ParamError: error if the parameters are invalid.
  • BaseException: error if the return result from server is not ok.

Example

from pymilvus import Partition
partition = Partition("novel")
res = collection.query(
  expr = "book_id in [2,4,6,8]", 
  output_fields = ["book_id", "book_intro"],
  consistency_level="Strong"
)
sorted_res = sorted(res, key=lambda k: k['book_id'])
sorted_res

release()

This method releases the specified partition from memory.

Invocation

release(timeout=None, **kwargs)

Parameters

ParameterDescriptionTypeRequired
timeoutAn optional duration of time in seconds to allow for the RPC. If it is set to None, the client keeps waiting until the server responds or error occurs.FloatFalse

Return

No return.

Raises

PartitionNotExistException: error if the partition does not exist.

Example

from pymilvus import Partition
partition = Partition("book", "novel", "")
partition.load()
partition.release()

This method conducts a vector similarity search in a specified partition.

Invocation

search(data, anns_field, param, limit, expr=None, output_fields=None, timeout=None, round_decimal=-1, **kwargs)

Parameters

ParameterDescriptionTypeRequired
dataData to search withlist[list[Float]]True
anns_fieldName of the vector field to search onStringTrue
paramSpecific search parameter(s) of the index on the vector fieldDictTrue
limitNumber of nearest records to returnIntegerTrue
exprBoolean expression to filter the dataStringFalse
output_fieldsList of names of fields to outputlist[String]False
timeoutAn optional duration of time in seconds to allow for the RPC. If it is set to None, the client keeps waiting until the server responds or error occurs.FloatFalse
round_decimalNumber of the decimal places of the returned distanceIntegerFalse
kwargs
  • _async
  • _callback
  • consistency_level
  • guarantee_timestamp
  • graceful_time
  • travel_timestamp

  • Boolean value to indicate if to invoke asynchronously
  • Function that will be invoked after server responds successfully. It takes effect only if _async is set to True.
  • Consistency level used in the search
  • Milvus searches on the data view before this timestamp when it is provided. Otherwise, it searches the most updated data view. It can be only used in Customized level of consistency.
  • PyMilvus will use current timestamp minus the graceful_time as the guarantee_timestamp for search. It can be only used in Bounded level of consistency.
  • Timestamp that is used for Time Travel. Users can specify a timestamp in a search to get results based on a data view at a specified point in time.

  • Bool
  • Function
  • String/Integer
  • Integer
  • Integer
  • Integer
False

Return

A SearchResult object, an iterable, 2d-array-like class whose first dimension is the number of vectors to query (nq), and the second dimension is the number of limit (topk).

Raises

  • RpcError: error if gRPC encounter an error.
  • ParamError: error if the parameters are invalid.
  • BaseException: error if the return result from server is not ok.

Example

search_params = {"metric_type": "L2", "params": {"nprobe": 10}}
from pymilvus import Partition
partition = Partition("novel")
results = partition.search(
    data=[[0.1, 0.2]], 
    anns_field="book_intro", 
    param=search_params, 
    limit=10, 
    expr=None,
    consistency_level="Strong"
)
results[0].ids
results[0].distances