milvus集合管理

一、创建集合

集合由一个或多个分区组成。在创建新集合时,Milvus会创建一个默认分区_default
1.准备模式
需要创建的集合必须包含一个主键字段和一个向量字段。INT64和String是主键字段支持的数据类型。

首先,准备必要的参数,包括字段模式、集合模式和集合名称。

from pymilvus import CollectionSchema, FieldSchema, DataType
book_id = FieldSchema(
  name="book_id",
  dtype=DataType.INT64,
  is_primary=True,
)
book_name = FieldSchema(
  name="book_name",
  dtype=DataType.VARCHAR,
  max_length=200,
  # The default value will be used if this field is left empty during data inserts or upserts.
  # The data type of `default_value` must be the same as that specified in `dtype`.
  default_value="Unknown"
)
word_count = FieldSchema(
  name="word_count",
  dtype=DataType.INT64,
  # The default value will be used if this field is left empty during data inserts or upserts.
  # The data type of `default_value` must be the same as that specified in `dtype`.
  default_value=9999
)
book_intro = FieldSchema(
  name="book_intro",
  dtype=DataType.FLOAT_VECTOR,
  dim=2
)
# 集合的定义
schema = CollectionSchema(
  fields=[book_id, book_name, word_count, book_intro],
  description="Test book search",
  enable_dynamic_field=True
)
collection_name = "book"

也可以使用预定义的模式新建集合:

from pymilvus import Collection
collection = Collection(
    name=collection_name,
    schema=schema,
    using='default',
    shards_num=2
    )

二、重命名集合

使用rename_collection方法

from pymilvus import Collection, FieldSchema, CollectionSchema, DataType, connections, utility
connections.connect(alias="default")
schema = CollectionSchema(fields=[
...     FieldSchema("int64", DataType.INT64, description="int64", is_primary=True),
...     FieldSchema("float_vector", DataType.FLOAT_VECTOR, is_primary=False, dim=128),
... ])
collection = Collection(name="old_collection", schema=schema)
utility.rename_collection("old_collection", "new_collection") # Output: True
utility.drop_collection("new_collection")
utility.has_collection("new_collection") # Output: False

三、修改集合

主要还是修改生存时间

collection.set_properties(properties={"collection.ttl.seconds": 1800})

四、检查集合信息

1.检查集合是否存在

from pymilvus import utility
utility.has_collection("book")

2.检查集合的细节信息

from pymilvus import Collection
collection = Collection("book")  # Get an existing collection.

collection.schema                # Return the schema.CollectionSchema of the collection.
collection.description           # Return the description of the collection.
collection.name                  # Return the name of the collection.
collection.is_empty              # Return the boolean value that indicates if the collection is empty.
collection.num_entities          # Return the number of entities in the collection.
collection.primary_field         # Return the schema.FieldSchema of the primary key field.
collection.partitions            # Return the list[Partition] object.
collection.indexes               # Return the list[Index] object.
collection.properties		# Return the expiration time of data in the collection.

3.列出索引的集合

from pymilvus import utility
utility.list_collections()

五、删除集合

from pymilvus import utility
utility.drop_collection("book")

六、集合的别名

1.创建集合的别名
在创建集合时可以,也可以如下:

from pymilvus import utility
utility.create_alias(
  collection_name = "book",
  alias = "publication"
)

2.删除别名

from pymilvus import utility
utility.drop_alias(alias = "publication")

3.修改别名

from pymilvus import utility
utility.alter_alias(
  collection_name = "book",
  alias = "publication"
)

七、加载集合

1.Milvus 中的所有搜索和查询操作都在内存中执行。
在当前版本中,所有在线查询节点都将根据用户指定的副本数分为多个副本组。所有副本组都应具有最小的内存资源来加载所提供的集合的一个副本。

from pymilvus import Collection, utility
 
# Get an existing collection.
collection = Collection("book")      
collection.load(replica_number=2)
 
# Check the loading progress and loading status
utility.load_state("book")
# Output: <LoadState: Loaded>
 
utility.loading_progress("book")
# Output: {'loading_progress': 100%}
 

2.获取副本信息

from pymilvus import Collection
collection = Collection("book")      # Get an existing collection.
collection.load(replica_number=2)    # Load collection as 2 replicas
result = collection.get_replicas()
print(result)
 

八、释放集合
在搜索或查询后释放集合以减少内存使用

from pymilvus import Collection
collection = Collection("book")      # Get an existing collection.
collection.release()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Milvus 是一个开源的向量相似度搜索引擎,而Spring Boot 是一个用于构建基于 Java 的独立、生产级的应用程序的框架。 Milvus Spring Boot 是将 Milvus 与 Spring Boot 框架结合使用的一种方式。借助 Spring Boot,我们可以更方便地构建基于 Milvus 的应用程序。 首先,我们可以使用 Spring Boot 的依赖管理功能,将 Milvus 的 Java 客户端库添加到项目中。这样,我们就可以在我们的应用程序中直接使用 Milvus 的功能,如向量的插入、查询和删除等。 其次,Spring Boot 提供了强大的配置管理功能,我们可以轻松地将 Milvus 的连接配置信息添加到应用程序的配置文件中,例如指定 Milvus 的 IP 地址、端口号和连接池大小等。这样,我们就可以灵活地管理 Milvus 与其他组件的连接。 另外,Spring Boot 还提供了便捷的 RESTful API 开发功能。我们可以利用这一特性,将 Milvus 的搜索引擎功能以接口的形式暴露给客户端,使得客户端可以通过 HTTP 请求来进行向量的检索。这样,我们可以轻松地建立一个灵活、高性能的分布式向量搜索系统。 总的来说,Milvus Spring Boot 结合了 Milvus 的强大功能和 Spring Boot 的便捷开发特性,使得我们可以更快速、灵活地搭建起一个高性能的向量搜索应用程序。它在大数据、人工智能等领域有广泛的应用前景,可以应对各种复杂的向量查询需求。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

灵海之森

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值