基本集合操作
Weaviate 中的每个对象都属于且仅属于一个集合。使用此页上的示例来管理您的集合。
较新的 Weaviate 文档讨论“集合”。较旧的 Weaviate 文档则使用“类”。您将在整个文档中看到这两个术语。
从 Weaviate Python 客户端 v4.16.0 开始,向量化器配置 API 已更新。
从 Weaviate JS/TS 客户端 v3.8.0 开始,向量化器配置 API 已更新。
操作要求:更新到最新客户端版本并迁移您的代码以使用 新的向量化器配置 API。
创建集合
要创建集合,请至少指定集合名称。如果您没有指定任何属性,auto-schema 将创建它们。
Weaviate 遵循 GraphQL 命名约定。
- 集合名称以大写字母开头。
- 属性名称以小写字母开头。
如果使用大写字母开头来定义属性名称,Weaviate 会在内部将其更改为小写字母。
如果某个片段无法工作或您有任何反馈,请打开一个 GitHub issue。
client.collections.create("Article")
- 手动定义您的数据模式:避免使用
auto-schema功能,而是手动 定义集合的属性。 - 避免创建过多的集合:使用过多的集合可能导致可伸缩性问题,例如高内存使用率和查询性能下降。相反,请考虑 使用多租户,其中单个集合被细分为多个租户。有关详细信息,请参阅 入门指南:使用集合的缩放限制。
创建集合并定义属性
属性是集合中的数据字段。每个属性都有一个名称和一个数据类型。
其他信息
如果某个片段无法工作或您有任何反馈,请打开一个 GitHub issue。
from weaviate.classes.config import Property, DataType
# Note that you can use `client.collections.create_from_dict()` to create a collection from a v3-client-style JSON object
client.collections.create(
"Article",
properties=[
Property(name="title", data_type=DataType.TEXT),
Property(name="body", data_type=DataType.TEXT),
],
)
使用向量化器创建集合
为将在创建对象和执行向量搜索查询时生成向量嵌入的集合指定一个 vectorizer。
如果某个片段无法工作或您有任何反馈,请打开一个 GitHub issue。
from weaviate.classes.config import Configure, Property, DataType
client.collections.create(
"Article",
vector_config=Configure.Vectors.text2vec_openai(),
properties=[
Property(name="title", data_type=DataType.TEXT),
Property(name="body", data_type=DataType.TEXT),
],
)
在 管理集合:向量化器和向量索引 中了解有关向量化器和向量索引配置的更多信息。
禁用自动模式
默认情况下,Weaviate 会创建缺失的集合和缺失的属性。当您手动配置集合时,您可以更精确地控制集合设置。
要禁用 auto-schema,请在您的系统配置文件中设置 AUTOSCHEMA_ENABLED: 'false'。
检查集合是否存在
获取一个布尔值,指示给定的集合是否存在。
如果某个片段无法工作或您有任何反馈,请打开一个 GitHub issue。
exists = client.collections.exists("Article") # Returns a boolean
读取单个集合定义
从模式中检索集合定义。
如果某个片段无法工作或您有任何反馈,请打开一个 GitHub issue。
articles = client.collections.use("Article")
articles_config = articles.config.get()
print(articles_config)
示例配置:文本对象
此文本对象配置定义了以下内容
- 集合名称 (
Article) - 向量化器模块 (
text2vec-cohere) 和模型 (embed-multilingual-v2.0) - 一组属性 (
title,body) 和text数据类型。
{
"class": "Article",
"vectorizer": "text2vec-cohere",
"moduleConfig": {
"text2vec-cohere": {
"model": "embed-multilingual-v2.0"
}
},
"properties": [
{
"name": "title",
"dataType": ["text"]
},
{
"name": "body",
"dataType": ["text"]
}
]
}
示例配置:嵌套对象
v1.22 中添加此嵌套对象配置定义了以下内容
-
集合名称 (
Person) -
向量化器模块 (
text2vec-huggingface) -
一组属性 (
last_name,address)last_name具有text数据类型address具有object数据类型
-
address属性有两个嵌套属性 (street和city)
{
"class": "Person",
"vectorizer": "text2vec-huggingface",
"properties": [
{
"dataType": ["text"],
"name": "last_name"
},
{
"dataType": ["object"],
"name": "address",
"nestedProperties": [
{ "dataType": ["text"], "name": "street" },
{ "dataType": ["text"], "name": "city" }
]
}
]
}
示例配置:生成式搜索
此配置用于 检索增强生成,定义了以下内容
- 集合名称 (
Article) - 默认向量化器模块 (
text2vec-openai) - 生成模块 (
generative-openai) - 一组属性 (
title,chunk,chunk_no和url) url属性的分词选项url属性的向量化选项(跳过向量化)
{
"class": "Article",
"vectorizer": "text2vec-openai",
"vectorIndexConfig": {
"distance": "cosine"
},
"moduleConfig": {
"generative-openai": {}
},
"properties": [
{
"name": "title",
"dataType": ["text"]
},
{
"name": "chunk",
"dataType": ["text"]
},
{
"name": "chunk_no",
"dataType": ["int"]
},
{
"name": "url",
"dataType": ["text"],
"tokenization": "field",
"moduleConfig": {
"text2vec-openai": {
"skip": true
}
}
}
]
}
示例配置:图像
此图像搜索配置定义了以下内容
-
集合名称 (
Image) -
向量化器模块 (
img2vec-neural)image属性配置集合以存储图像数据。
-
向量索引距离度量 (
cosine) -
一组属性 (
image),其中image属性设置为blob。
有关图像搜索,请参阅 图像搜索。
{
"class": "Image",
"vectorizer": "img2vec-neural",
"vectorIndexConfig": {
"distance": "cosine"
},
"moduleConfig": {
"img2vec-neural": {
"imageFields": ["image"]
}
},
"properties": [
{
"name": "image",
"dataType": ["blob"]
}
]
}
读取所有集合定义
获取数据库模式以检索所有集合定义。
如果某个片段无法工作或您有任何反馈,请打开一个 GitHub issue。
response = client.collections.list_all(simple=False)
print(response)
更新集合定义
您可以更新集合定义以更改 可变集合设置。
如果某个片段无法工作或您有任何反馈,请打开一个 GitHub issue。
from weaviate.classes.config import (
Reconfigure,
VectorFilterStrategy,
ReplicationDeletionStrategy,
)
articles = client.collections.use("Article")
# Update the collection definition
articles.config.update(
description="An updated collection description.",
property_descriptions={
"title": "The updated title description for article",
}, # Available from Weaviate v1.31.0
inverted_index_config=Reconfigure.inverted_index(bm25_k1=1.5),
vector_config=Reconfigure.Vectors.update(
name="default",
vector_index_config=Reconfigure.VectorIndex.hnsw(
filter_strategy=VectorFilterStrategy.ACORN # Available from Weaviate v1.27.0
),
),
replication_config=Reconfigure.replication(
deletion_strategy=ReplicationDeletionStrategy.TIME_BASED_RESOLUTION # Available from Weaviate v1.28.0
),
)
articles = client.collections.use("Article")
article_shards = articles.config.update_shards(
status="READY",
shard_names=shard_names, # The names (List[str]) of the shard to update (or a shard name)
)
print(article_shards)
删除集合
您可以删除任何不需要的集合,以及它们包含的数据。
当您删除集合时,您会删除所有关联的对象!
在生产数据库和任何其他具有重要数据的地方,请非常小心删除操作。
此代码删除一个集合及其对象。
如果某个片段无法工作或您有任何反馈,请打开一个 GitHub issue。
# collection_name can be a string ("Article") or a list of strings (["Article", "Category"])
client.collections.delete(
collection_name
) # THIS WILL DELETE THE SPECIFIED COLLECTION(S) AND THEIR OBJECTS
# Note: you can also delete all collections in the Weaviate instance with:
# client.collections.delete_all()
添加属性
数据导入后的索引限制
在导入数据之前添加集合属性时,没有索引限制。
如果您在导入一些数据后添加新属性,则会对索引产生影响。
属性索引是在导入时构建的。如果您在导入一些数据后添加新属性,现有的对象索引不会自动更新以添加新属性。这意味着现有对象未添加到新的属性索引中。查询可能会返回意外结果,因为索引仅包含新对象。
要创建包含集合中所有对象的索引,请执行以下操作之一
- 新集合:在导入对象之前添加集合的所有属性。
- 现有集合:从集合中导出现有数据。使用新属性重新创建它。将数据导入到更新后的集合中。
我们正在开发一个重新索引 API,以便您可以在添加属性后重新索引数据。此功能将在未来的版本中提供。
如果某个片段无法工作或您有任何反馈,请打开一个 GitHub issue。
from weaviate.classes.config import Property, DataType
articles = client.collections.use("Article")
articles.config.add_property(Property(name="onHomepage", data_type=DataType.BOOL))
更多资源
问题和反馈
如果您有任何问题或反馈,请在 用户论坛 中告诉我们。
