了解 Milvus 操作

    请使用 Milvus 自带的 Python 客户端 - pymilvus 运行下列操作。

    1. 导入 pymilvus.

    2. 请用下列任一方式,将 Milvus 连接到您本地服务器。

      1. >>> milvus = Milvus()
      2. >>> milvus.connect(host='localhost', port='19530')
      3. Status(message='connected!', code=0)
      1. >>> milvus.connect(uri='tcp://localhost:19530')
      2. Status(message='connected!', code=0)

      创建表

    我们以创建表 test01 为例,向您展示如何创建一张数据表。以下是数据表相关参数,在创建表时可以根据实际需求选择:

    请使用 milvus.create_table 来创建表,后面跟表名、向量维度和索引方式:

    1. 准备表参数。

      1. # Prepare param
      2. >>> param = {'table_name':'test01', 'dimension':256, 'index_file_size':1024, 'metric_type':MetricType.L2}
    2. 创建表 test01。

      1. # Create a table
      2. >>> milvus.create_table(param)
      3. Status(message='Table test01 created!', code=0)
    3. 确认新创建表的信息。

      1. # Verify table info.
      2. >>> status, table = milvus.describe_table('test01')
      3. >>> status
      4. Status(message='Describe table successfully!')
      5. >>> table

    若要显示数据库中所有的表,请使用 milvus.show_tables

    1. >>> status, table = milvus.describe_table('test01')
    2. >>> status
    3. Status(message='Describe table successfully!')
    4. >>> table
    5. TableSchema(table_name='test01',dimension=256, index_file_size=1024, metric_type=<MetricType: L2>)

    若要显示表的行数, 使用 milvus.get_table_row_count,后面跟表名:

    1. # Show table rows
    2. >>> status, num = milvus.get_table_row_count('test01')
    3. >>> status
    4. Status(code=0, message='Success')
    5. >>> num
    6. 20

    请用下列命令确认某张表是否存在:

    1. >>> milvus.has_table(table_name='test01')

    将向量插入表

    以下是向量插入的参数列表:

    若要批量插入一组向量(在下面代码中以records表示),请使用 milvus.add_vectors ,后面跟表名和一组以逗号隔开的向量。

    成功后,将返回一组向量id。

    1. # Insert vectors
    2. >>> status, ids = milvus.add_vectors(table_name='test01', records=vectors)
    3. >>> status
    4. Status(code=0, message='Success')
    5. >>> ids # 20 ids returned
    6. 23455321135511233
    7. 12245748929023489
    8. ...

    您也可以为插入的向量提供用户自定义的向量 id:

    1. >>> vector_ids = [id for id in range(20)]
    2. >>> status, ids = milvus.add_vectors(table_name='test01', records=vectors, ids=vector_ids)
    3. >>> print(ids)
    4. [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,后面跟要查询的表名。

    以下是创建索引的参数列表:

    1. 准备索引参数。

      1. # Prepare index param
    2. 创建索引。

      1. >>> milvus.create_index('test01', index_param)
      2. >>> status
      3. Status(code=0, message='Build index successfully!')

    请使用 milvus.describe_index 来显示索引信息,后面跟表名:

    1. # Show index info
    2. >>> milvus.describe_index('test01')
    3. >>> status
    4. Status(code=0, message='Success'), IndexParam(table_name='test01', index_type=<IndexType: IVFLAT>, nlist=16384)

    若要删除索引,请使用下列命令:

    1. >>> milvus.drop_index('test01')
    2. >>> status
    3. Status(code=0, message='Success')

    查询向量

    以下是查询向量的参数列表:

    请使用 milvus.search_vectors 来搜索向量,后面跟要搜索的表的名字,要搜索的目标向量,以及您期望返回的与每个目标向量最相似的匹配向量个数。

    假设您要针对3条256维的目标向量(在下面代码中用q_records表示),搜索与每条目标向量相似度最高的前2条匹配向量,您可以:

    1. # Search 3 vectors
    2. >>> status, results = milvus.search_vectors(table_name='test01', query_records=q_records, top_k=2, nprobe=16)
    3. >>> status
    4. Status(message='Search vectors successfully!', code=0)
    5. >>> results # Searched top_k vectors
    6. >>> print(results) # Searched top_k vectors
    7. [
    8. [(id:15, distance:2.855304718017578),
    9. (id:16, distance:3.040700674057007)],
    10. [(id:11, distance:3.673950433731079),
    11. (id:15, distance:4.183730602264404)],
    12. ........
    13. [(id:6, distance:4.065953254699707),
    14. (id:1, distance:4.149323463439941)]
    15. ]

    若要过滤搜索结果,请使用 query_ranges 定义过滤条件。

    若您不再需要某张表,请使用 milvus.delete_table 将该表及其数据删除:

    1. # Drop table
    2. >>> milvus.delete_table(table_name='test01')
    3. Status(message='Delete table successfully!', code=0)

    接下来您可以