pymilvus

Example

You can find a example script in example.py.

Module Contents

Version

Get version of pymilvus by Milvus.__version__.

Client

class milvus.Milvus(host=None, port=None, handler='GRPC', **kwargs)[source]

Bases: object

add_vectors(collection_name, records, ids=None, partition_tag=None, params=None, **kwargs)

Insert vectors to a collection.

Parameters
  • ids (list[int]) -- list of id

  • collection_name (str) -- Name of the collection to insert vectors to.

  • records -- List of vectors to insert.

  • partition_tag (str or None. If partition_tag is None, vectors will be inserted to the collection rather than partitions.) -- Tag of a partition.

  • timeout (int) -- Time to wait for server response before timeout.

Returns

Status: Whether vectors are inserted successfully. ids: IDs of the inserted vectors.

Return type

(Status, list(int))

client_version()[source]

Returns the version of the client.

Returns

Version of the client.

Return type

(str)

collection_info(collection_name, timeout=10)[source]
compact(collection_name, timeout=None)[source]

Compacts segments in a collection. This function is recommended after deleting vectors.

Parameters

collection_name (str) -- Name of the collections to compact.

connect(host=None, port=None, uri=None, timeout=1)[source]

Connects to the Milvus server.

Parameters
  • host (str) -- (Optional) Ip address of the Milvus server. The default is 127.0.0.1.

  • port (str) -- (Optional) Port of the Milvus server. The default is 19530.

  • uri (str) -- (Optional) URI of the Milvus server. Only TCP is supported. The default is tcp://127.0.0.1:19530.

  • timeout (float) -- (Optional) Connection timeout in milliseconds. The default is 3000.

Returns

Indicates whether the connection is successful.

Return type

Status

Raises

NotConnectError

connected()[source]

Checks whether the client is connected to the Milvus server.

Returns

Whether the client is connected to the Milvus server.

Return type

bool

count_collection(collection_name, timeout=10)[source]

Returns the number of vectors in a collection.

Parameters

collection_name (str) -- target table name.

Returns

Status: indicate if operation is successful

res: int, table row count

create_collection(param, timeout=10)[source]

Creates a collection.

Parameters
  • param (dict) --

    Information needed to create a collection.

    `param={'collection_name': 'name',

    'dimension': 16, 'index_file_size': 1024 (default), 'metric_type': Metric_type.L2 (default) }`

  • timeout (double) -- Timeout in seconds.

Returns

Whether the operation is successful.

Return type

Status

create_index(collection_name, index_type=None, params=None, timeout=None)[source]

Creates index for a collection.

Parameters
  • collection_name (str) -- Collection used to create index.

  • index -- index params

  • timeout (int) --

    grpc request timeout.

    if timeout = -1, method invoke a synchronous call, waiting util grpc response else method invoke a asynchronous call, timeout work here

Returns

Whether the operation is successful.

create_partition(collection_name, partition_tag, timeout=10)[source]

create a partition for a collection.

param collection_name

Name of the collection.

type collection_name

str

param partition_name

Name of the partition.

type partition_name

str

param partition_tag

Name of the partition tag.

type partition_tag

str

param timeout

time waiting for response.

type timeout

int

Returns

Status: Whether the operation is successful.

delete_by_id(collection_name, id_array, timeout=None)[source]

Deletes vectors in a collection by vector ID.

describe_collection(collection_name, timeout=10)[source]

Returns information of a collection.

Parameters

collection_name (str) -- Name of the collection to describe.

Returns

(Status, table_schema) Status: indicate if query is successful table_schema: return when operation is successful

Return type

(Status, TableSchema)

describe_index(collection_name, timeout=10)[source]

Show index information of a collection.

Parameters

collection_name (str) -- table name been queried

Returns

Status: Whether the operation is successful. IndexSchema:

disconnect()[source]

Disconnects from the Milvus server.

Returns

Whether the client is disconnected from the Milvus server.

Return type

Status

drop_collection(collection_name, timeout=10)[source]

Deletes a collection by name.

Parameters

collection_name (str) -- Name of the table being deleted

Returns

Status, indicate if operation is successful

Return type

Status

drop_index(collection_name, timeout=10)[source]

Removes an index.

Parameters

collection_name (str) -- target collection name.

Returns

Status: Whether the operation is successful.

::rtype: Status

drop_partition(collection_name, partition_tag, timeout=10)[source]

Deletes a partition in a collection.

Parameters
  • collection_name (str) -- Collection name.

  • partition_tag (str) -- Partition name.

  • timeout (int) -- time waiting for response.

Returns

Status: Whether the operation is successful.

flush(collection_name_array=None)[source]

Flushes vector data in one collection or multiple collections to disk.

Parameters

collection_name -- Name of one or multiple collections to flush.

get_config(parent_key, child_key)[source]

Gets Milvus configurations.

get_vector_by_id(collection_name, vector_id, timeout=None)[source]
get_vector_ids(collection_name, segment_name, timeout=None)[source]
property handler
has_collection(collection_name, timeout=10)[source]

Checks whether a collection exists.

Parameters
  • collection_name (str) -- Name of the collection to check.

  • timeout (int) -- Timeout in seconds.

Returns

Status: indicate whether the operation is successful. bool if given collection_name exists

insert(collection_name, records, ids=None, partition_tag=None, params=None, **kwargs)[source]

Insert vectors to a collection.

Parameters
  • ids (list[int]) -- list of id

  • collection_name (str) -- Name of the collection to insert vectors to.

  • records -- List of vectors to insert.

  • partition_tag (str or None. If partition_tag is None, vectors will be inserted to the collection rather than partitions.) -- Tag of a partition.

  • timeout (int) -- Time to wait for server response before timeout.

Returns

Status: Whether vectors are inserted successfully. ids: IDs of the inserted vectors.

Return type

(Status, list(int))

preload_collection(collection_name, timeout=None)[source]

Loads a collection for caching.

Parameters

collection_name (str) -- table to preload

Returns

Status: indicate if invoke is successful

search(collection_name, top_k, query_records, partition_tags=None, params=None)[source]

Search vectors in a collection.

Parameters
  • collection_name (str) -- Name of the collection.

  • top_k (int) -- number of vertors which is most similar with query vectors

  • nprobe (int) -- cell number of probe

  • query_records (list[list[float32]]) -- vectors to query

  • partition_tags (list) -- tags to search

:return

Status: Whether the operation is successful. result: query result

Return type

(Status, TopKQueryResult)

search_in_files(collection_name, file_ids, query_records, top_k, params=None)[source]

Searches for vectors in specific files of a collection.

The Milvus server stores vector data into multiple files. Searching for vectors in specific files is a method used in Mishards. Obtain more detail about Mishards, see <a href="https://github.com/milvus-io/milvus/tree/master/shards">

Parameters
  • collection_name (str) -- table name been queried

  • file_ids (list[str] or list[int]) -- Specified files id array

  • query_records (list[list[float]]) -- all vectors going to be queried

  • query_ranges -- Optional ranges for conditional search. If not specified, search in the whole table

  • top_k (int) -- how many similar vectors will be searched

Returns

Status: indicate if query is successful results: query result

Return type

(Status, TopKQueryResult)

search_vectors(collection_name, top_k, query_records, partition_tags=None, params=None)

Search vectors in a collection.

Parameters
  • collection_name (str) -- Name of the collection.

  • top_k (int) -- number of vertors which is most similar with query vectors

  • nprobe (int) -- cell number of probe

  • query_records (list[list[float32]]) -- vectors to query

  • partition_tags (list) -- tags to search

:return

Status: Whether the operation is successful. result: query result

Return type

(Status, TopKQueryResult)

search_vectors_in_files(collection_name, file_ids, query_records, top_k, params=None)

Searches for vectors in specific files of a collection.

The Milvus server stores vector data into multiple files. Searching for vectors in specific files is a method used in Mishards. Obtain more detail about Mishards, see <a href="https://github.com/milvus-io/milvus/tree/master/shards">

Parameters
  • collection_name (str) -- table name been queried

  • file_ids (list[str] or list[int]) -- Specified files id array

  • query_records (list[list[float]]) -- all vectors going to be queried

  • query_ranges -- Optional ranges for conditional search. If not specified, search in the whole table

  • top_k (int) -- how many similar vectors will be searched

Returns

Status: indicate if query is successful results: query result

Return type

(Status, TopKQueryResult)

server_status(timeout=10)[source]

Returns the status of the Milvus server.

Returns

Status: Whether the operation is successful.

str : Status of the Milvus server.

Return type

(Status, str)

server_version(timeout=10)[source]

Returns the version of the Milvus server.

Returns

Status: Whether the operation is successful.

str : Version of the Milvus server.

Return type

(Status, str)

set_config(parent_key, child_key, value)[source]

Sets Milvus configurations.

set_hook(**kwargs)[source]
show_collections(timeout=10)[source]

Returns information of all collections.

Returns

Status: indicate if this operation is successful

collections: list of table names, return when operation

is successful

Return type

(Status, list[str])

show_partitions(collection_name, timeout=10)[source]

Show all partitions in a collection.

Parameters
  • collection_name (str) -- target table name.

  • timeout (int) -- time waiting for response.

Returns

Status: Whether the operation is successful. partition_list:

property status