Skip to main content

Python Sdk for Milvus

Project description

Milvus Python SDK -- pymilvus

version license

Using Milvus python sdk for Milvus Download

Pymilvus only supports python >= 3.4, is fully tested under 3.4, 3.5, 3.6, 3.7.

Pymilvus can be downloaded via pip. If no use, try pip3

$ pip install pymilvus

Different versions of Milvus and lowest/highest pymilvus version supported accordingly

Milvus version Lowest pymilvus version supported Highest pymivus version supported
0.3.0 - 0.1.13
0.3.1 0.1.14 0.1.25
0.4.0 0.2.0 -

You can download a specific version by:

$ pip install pymilvus==0.2.0

If you want to upgrade pymilvus to newest version

$ pip install --upgrade pymilvus

Import

from milvus import Milvus, IndexType, MetricType, Status

Getting started

Initial a Milvus instance and connect to the sever

>>> milvus = Milvus()

>>> milvus.connect(host='SERVER-HOST', port='SERVER-PORT')
Status(code=0, message='Successfully connected!')

Once successfully connected, you can get the version of server

>>> milvus.server_version()
(Status(code=0, message='Success'), 0.4.0)  # this is example version, the real version may vary

Add a new table

First set param

>>> param = {'table_name':'test01', 'dimension':256, 'index_file_size':1024, 'metric_type':MetricType.L2}

Then create table

>>> milvus.create_table(param)
Status(code=0, message='Create table successfully!')

Describe the table we just created

>>> milvus.describe_table('test01')
(Status(code=0, message='Describe table successfully!'), TableSchema(table_name='test01', dimension=256, index_file_size=1024, metric_type=<MetricType: L2>))

Add vectors into table test01

First create 20 vectors of 256-dimension.

  • Note that random and pprint we used here is for creating fake vectors data and pretty print, you may not need them in your project
>>> import random
>>> from pprint import pprint

>>> dim = 256  # Dimension of the vector

# Initialize 20 vectors of 256-dimension
>>> vectors = [[random.random() for _ in range(dim)] for _ in range(20)]

Then add vectors into table test01

>>> status, ids = milvus.add_vectors(table_name='test01', records=vectors)
>>> print(status)
Status(code=0, message='Success')
>>> pprint(ids) # List of ids returned
23455321135511233
12245748929023489
...

You can also specify vectors id

>>> vector_ids = [i for i in range(20)]
>>> status, ids = milvus.add_vectors(table_name='test01', records=vectors, ids=vector_ids)
>>> pprint(ids)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]

Get vectors num

>>> milvus.get_table_row_count('test01')
(Status(code=0, message='Success!'), 20)

Load vectors into memory

>>> milvus.preload_table('test01')
Status(code=0, message='')

Create index

>>> index_param = {'index_type': IndexType.IVFLAT, 'nlist': 16384}
>>> milvus.create_index('test01', index_param)
Status(code=0, message='Build index successfully!')

Then show index information

>>> client.describe_index('test01')
(Status(code=0, message='Successfully'), IndexParam(_table_name='test01', _index_type=<IndexType: IVFLAT>, _nlist=16384))

Search vectors

# create 5 vectors of 256-dimension
>>> q_records = [[random.random() for _ in range(dim)] for _ in range(5)]

Then get results

>>> status, results = milvus.search_vectors(table_name='test01', query_records=q_records, top_k=1, nprobe=16)
>>> print(status)
Status(code=0, message='Search vectors successfully!')
>>> pprint(results) # Searched top_k vectors
[
[QueryResult(id=0, distance=34.85963439941406)],
[QueryResult(id=0, distance=36.73900604248047)],
[QueryResult(id=0, distance=34.35655975341797)],
[QueryResult(id=18, distance=36.19701385498047)],
[QueryResult(id=5, distance=39.11549758911133)]
]

Drop index

>>> milvus.drop_index('test01')
Status(code=0, message='')

Delete vectors by date range

>>> milvus.delete_vectors_by_range('test01', '2019-06-01', '2020-01-01')
Status(code=0, message='')

Delete the table we just created

>>> milvus.delete_table(table_name='test01')
Status(code=0, message='Success')

Disconnect with the server

>>> milvus.disconnect()
Status(code=0, message='Success')

Example python

There are some small examples in examples/, you can find more guide there.

Build docs

$ sphinx-build -b html doc/en/ doc/en/build

If you encounter any problems or bugs, please open new issues

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pymilvus-0.2.0.tar.gz (22.3 kB view details)

Uploaded Source

Built Distribution

pymilvus-0.2.0-py3-none-any.whl (28.7 kB view details)

Uploaded Python 3

File details

Details for the file pymilvus-0.2.0.tar.gz.

File metadata

  • Download URL: pymilvus-0.2.0.tar.gz
  • Upload date:
  • Size: 22.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.34.0 CPython/3.6.9

File hashes

Hashes for pymilvus-0.2.0.tar.gz
Algorithm Hash digest
SHA256 dbd094cc4a87db44739adac7b66366248d166d8f9e881d73fa51919a10fc65bf
MD5 cd2fd23a71526491a2e3f6633ec30913
BLAKE2b-256 357fe7c934e6d9e89e7f8e1552c21ff1cdde741a1a1047acf585f7b74f31b499

See more details on using hashes here.

File details

Details for the file pymilvus-0.2.0-py3-none-any.whl.

File metadata

  • Download URL: pymilvus-0.2.0-py3-none-any.whl
  • Upload date:
  • Size: 28.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.34.0 CPython/3.6.9

File hashes

Hashes for pymilvus-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2362d011e33322b1029dea870b8441a94a8860d4277bb1a7ad5de9fa8b3ba1bf
MD5 ba73af0d92531d3712872da83c81c4e8
BLAKE2b-256 dcc1288e3bc7481e969cf71c8a53fca6c6485439aea2c31517385fc0e84f9b4d

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page