milvus集合管理

一、创建集合

集合由一个或多个分区组成。在创建新集合时,Milvus会创建一个默认分区_default

1.准备模式

需要创建的集合必须包含一个主键字段和一个向量字段。INT64和String是主键字段支持的数据类型。

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

python 复制代码
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"

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

python 复制代码
from pymilvus import Collection
collection = Collection(
    name=collection_name,
    schema=schema,
    using='default',
    shards_num=2
    )

二、重命名集合

使用rename_collection方法

python 复制代码
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

三、修改集合

主要还是修改生存时间

python 复制代码
collection.set_properties(properties={"collection.ttl.seconds": 1800})

四、检查集合信息

1.检查集合是否存在

python 复制代码
from pymilvus import utility
utility.has_collection("book")

2.检查集合的细节信息

python 复制代码
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.列出索引的集合

python 复制代码
from pymilvus import utility
utility.list_collections()

五、删除集合

python 复制代码
from pymilvus import utility
utility.drop_collection("book")

六、集合的别名

1.创建集合的别名

在创建集合时可以,也可以如下:

python 复制代码
from pymilvus import utility
utility.create_alias(
  collection_name = "book",
  alias = "publication"
)

2.删除别名

python 复制代码
from pymilvus import utility
utility.drop_alias(alias = "publication")

3.修改别名

python 复制代码
from pymilvus import utility
utility.alter_alias(
  collection_name = "book",
  alias = "publication"
)

七、加载集合

1.Milvus 中的所有搜索和查询操作都在内存中执行。

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

python 复制代码
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.获取副本信息

python 复制代码
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)
 

八、释放集合

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

python 复制代码
from pymilvus import Collection
collection = Collection("book")      # Get an existing collection.
collection.release()
相关推荐
初见无风24 分钟前
3.0 Lua代码中的闭包
开发语言·lua·lua5.4
Eiceblue26 分钟前
使用 Python 向 PDF 添加附件与附件注释
linux·开发语言·vscode·python·pdf
loong_XL1 小时前
AC自动机算法-字符串搜索算法:敏感词检测
开发语言·算法·c#
xrkhy1 小时前
Java全栈面试题及答案汇总(2)
java·开发语言
@LetsTGBot搜索引擎机器人2 小时前
从零打造 Telegram 中文生态:界面汉化 + 中文Bot + @letstgbot 搜索引擎整合实战
开发语言·python·搜索引擎·github·全文检索
洲覆2 小时前
缓存异常:缓存穿透、缓存击穿、缓存雪崩
开发语言·数据库·mysql·缓存
逻极2 小时前
变量与可变性:Rust中的数据绑定
开发语言·后端·rust
三次拒绝王俊凯3 小时前
java求职学习day47
java·开发语言·学习
合作小小程序员小小店3 小时前
基于可视化天气系统demo,基于python+ matplotlib+request爬虫,开发语言python,数据库无,10个可视化界面,需要的可以了联系。
开发语言·爬虫·python·matplotlib
宝桥南山3 小时前
.NET10 - 尝试一下Blazor Web Assembly Standalone App的fingerprint新特性
microsoft·微软·c#·asp.net·.net·.netcore