创建对象
此页面上的示例演示了如何在 Weaviate 中创建单个对象。
对于一次创建多个对象,请参阅 操作指南:批量导入。
创建对象
此示例在 JeopardyQuestion 集合中创建对象。
如果某个片段无法工作或您有任何反馈,请打开一个 GitHub issue。
jeopardy = client.collections.use("JeopardyQuestion")
uuid = jeopardy.data.insert({
"question": "This vector DB is OSS & supports automatic property type inference on import",
# "answer": "Weaviate", # properties can be omitted
"newProperty": 123, # will be automatically added as a number property
})
print(uuid) # the return value is the object's UUID
使用指定的向量创建对象
创建对象时,您可以提供一个向量。(对于指定多个命名向量,请参阅 下方。)
如果某个片段无法工作或您有任何反馈,请打开一个 GitHub issue。
jeopardy = client.collections.use("JeopardyQuestion")
uuid = jeopardy.data.insert(
properties={
"question": "This vector DB is OSS and supports automatic property type inference on import",
"answer": "Weaviate",
},
vector=[0.12345] * 1536
)
print(uuid) # the return value is the object's UUID
使用命名向量创建对象
v1.24创建对象时,您可以指定命名向量(如果 在您的集合中配置)。
如果某个片段无法工作或您有任何反馈,请打开一个 GitHub issue。
reviews = client.collections.use("WineReviewNV") # This collection must have named vectors configured
uuid = reviews.data.insert(
properties={
"title": "A delicious Riesling",
"review_body": "This wine is a delicious Riesling which pairs well with seafood.",
"country": "Germany",
},
# Specify the named vectors, following the collection definition
vector={
"title": [0.12345] * 1536,
"review_body": [0.31313] * 1536,
"title_country": [0.05050] * 1536,
}
)
print(uuid) # the return value is the object's UUID
使用指定的 ID 创建对象
创建对象时,您可以指定一个 ID。
如果未提供 ID,Weaviate 将生成一个随机 UUID。
如果某个片段无法工作或您有任何反馈,请打开一个 GitHub issue。
properties = {
"question": "This vector DB is OSS and supports automatic property type inference on import",
"answer": "Weaviate",
}
jeopardy = client.collections.use("JeopardyQuestion")
uuid = jeopardy.data.insert(
properties=properties,
uuid="12345678-e64f-5d94-90db-c8cfa3fc1234"
)
print(uuid) # the return value is the object's UUID
生成确定性 ID
您可以根据您的数据对象生成一个 ID。
对象 ID 不是随机生成的。相同的值始终生成相同的 ID。
如果您提供重复的 ID,Weaviate 会抛出错误。使用确定性 ID 以避免插入重复对象。
如果某个片段无法工作或您有任何反馈,请打开一个 GitHub issue。
from weaviate.util import generate_uuid5 # Generate a deterministic ID
data_object = {
"question": "This vector DB is OSS and supports automatic property type inference on import",
"answer": "Weaviate",
}
jeopardy = client.collections.use("JeopardyQuestion")
uuid = jeopardy.data.insert(
properties=data_object,
uuid=generate_uuid5(data_object),
)
其他信息
要生成确定性 ID,请使用以下方法之一
generate_uuid5(Python)generateUuid5(TypeScript)
使用交叉引用创建对象
涉及交叉引用的查询可能比不涉及交叉引用的查询慢,尤其是在大规模情况下,例如对于多个对象或复杂查询。
首先,我们强烈建议您考虑是否可以避免在数据模式中使用交叉引用。作为可扩展的 AI 数据库,Weaviate 擅长使用向量、关键词和混合搜索进行复杂查询,包括过滤器。您可以通过重新思考数据模式来避免交叉引用,从而受益匪浅。
例如,与其创建带有交叉引用的单独的“作者”和“书籍”集合,不如考虑将作者信息直接嵌入到书籍对象中,并使用搜索和过滤器按作者特征查找书籍。
您可以创建具有指向其他对象的交叉引用的对象。
如果某个片段无法工作或您有任何反馈,请打开一个 GitHub issue。
questions = client.collections.use("JeopardyQuestion")
questions.data.insert(
properties=properties, # A dictionary with the properties of the object
uuid=obj_uuid, # The UUID of the object
references={"hasCategory": category_uuid}, # e.g. {"hasCategory": "583876f3-e293-5b5b-9839-03f455f14575"}
)
有关使用交叉引用的更多信息,请参阅 操作指南:交叉引用。
使用 geoCoordinates 创建对象
当前,地理坐标过滤仅限于从源位置的最近 800 个结果,这些结果将进一步减少任何其他过滤器条件和搜索参数。
如果您计划使用密集数据集,请考虑使用另一种策略,例如将地理哈希转换为 text 数据类型,并进一步过滤,例如使用 ContainsAny 过滤器。
如果您想提供一个 geoCoordinates 属性,您需要指定 latitude 和 longitude 作为浮点十进制度数
如果某个片段无法工作或您有任何反馈,请打开一个 GitHub issue。
publications = client.collections.use("Publication")
publications.data.insert(
properties={
"headquartersGeoLocation": {
"latitude": 52.3932696,
"longitude": 4.8374263
}
},
)
在创建之前验证对象
在创建对象之前,您可以根据集合定义对其进行 验证。
如果某个片段无法工作或您有任何反馈,请打开一个 GitHub issue。
# Validate is currently not supported with the Weaviate Python client v4
多个向量嵌入(命名向量)
Weaviate 集合支持多个命名向量。
集合可以有多个 命名向量。
集合中的向量可以有自己的配置。每个向量空间可以设置自己的索引、自己的压缩算法和自己的向量化器。这意味着您可以对同一个对象使用不同的向量化模型,并应用不同的距离度量。
要使用命名向量,请调整您的查询以指定 向量搜索 或 混合搜索 查询的目标向量。
相关页面
问题和反馈
如果您有任何问题或反馈,请在 用户论坛 中告诉我们。
