了解 Milvus 操作
请使用 Milvus 自带的 Python 客户端 - pymilvus 运行下列操作。
导入 pymilvus.
请用下列任一方式,将 Milvus 连接到您本地服务器。
>>> milvus = Milvus()
>>> milvus.connect(host='localhost', port='19530')
Status(message='connected!', code=0)
>>> milvus.connect(uri='tcp://localhost:19530')
Status(message='connected!', code=0)
创建表
我们以创建表 test01 为例,向您展示如何创建一张数据表。以下是数据表相关参数,在创建表时可以根据实际需求选择:
请使用 milvus.create_table
来创建表,后面跟表名、向量维度和索引方式:
准备表参数。
# Prepare param
>>> param = {'table_name':'test01', 'dimension':256, 'index_file_size':1024, 'metric_type':MetricType.L2}
创建表 test01。
# Create a table
>>> milvus.create_table(param)
Status(message='Table test01 created!', code=0)
确认新创建表的信息。
# Verify table info.
>>> status, table = milvus.describe_table('test01')
>>> status
Status(message='Describe table successfully!')
>>> table
若要显示数据库中所有的表,请使用 milvus.show_tables
:
>>> status, table = milvus.describe_table('test01')
>>> status
Status(message='Describe table successfully!')
>>> table
TableSchema(table_name='test01',dimension=256, index_file_size=1024, metric_type=<MetricType: L2>)
若要显示表的行数, 使用 milvus.get_table_row_count
,后面跟表名:
# Show table rows
>>> status, num = milvus.get_table_row_count('test01')
>>> status
Status(code=0, message='Success')
>>> num
20
请用下列命令确认某张表是否存在:
>>> milvus.has_table(table_name='test01')
将向量插入表
以下是向量插入的参数列表:
若要批量插入一组向量(在下面代码中以records
表示),请使用 milvus.add_vectors
,后面跟表名和一组以逗号隔开的向量。
成功后,将返回一组向量id。
# Insert vectors
>>> status, ids = milvus.add_vectors(table_name='test01', records=vectors)
>>> status
Status(code=0, message='Success')
>>> ids # 20 ids returned
23455321135511233
12245748929023489
...
您也可以为插入的向量提供用户自定义的向量 id:
>>> vector_ids = [id for id in range(20)]
>>> status, ids = milvus.add_vectors(table_name='test01', records=vectors, ids=vector_ids)
>>> print(ids)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19]
若要查询某张表中一共插入了几条向量(表的总行数),请使用 milvus.get_table_row_count
,后面跟要查询的表名。
以下是创建索引的参数列表:
准备索引参数。
# Prepare index param
创建索引。
>>> milvus.create_index('test01', index_param)
>>> status
Status(code=0, message='Build index successfully!')
请使用 milvus.describe_index
来显示索引信息,后面跟表名:
# Show index info
>>> milvus.describe_index('test01')
>>> status
Status(code=0, message='Success'), IndexParam(table_name='test01', index_type=<IndexType: IVFLAT>, nlist=16384)
若要删除索引,请使用下列命令:
>>> milvus.drop_index('test01')
>>> status
Status(code=0, message='Success')
查询向量
以下是查询向量的参数列表:
请使用 milvus.search_vectors
来搜索向量,后面跟要搜索的表的名字,要搜索的目标向量,以及您期望返回的与每个目标向量最相似的匹配向量个数。
假设您要针对3条256维的目标向量(在下面代码中用q_records表示),搜索与每条目标向量相似度最高的前2条匹配向量,您可以:
# Search 3 vectors
>>> status, results = milvus.search_vectors(table_name='test01', query_records=q_records, top_k=2, nprobe=16)
>>> status
Status(message='Search vectors successfully!', code=0)
>>> results # Searched top_k vectors
>>> print(results) # Searched top_k vectors
[
[(id:15, distance:2.855304718017578),
(id:16, distance:3.040700674057007)],
[(id:11, distance:3.673950433731079),
(id:15, distance:4.183730602264404)],
........
[(id:6, distance:4.065953254699707),
(id:1, distance:4.149323463439941)]
]
若要过滤搜索结果,请使用 query_ranges
定义过滤条件。
若您不再需要某张表,请使用 milvus.delete_table
将该表及其数据删除:
# Drop table
>>> milvus.delete_table(table_name='test01')
Status(message='Delete table successfully!', code=0)