搜索模式和基础
使用 Weaviate,您可以使用 向量相似度搜索、关键词搜索,或两者结合使用 混合搜索 来查询您的数据。您可以控制要返回的对象 属性 和 元数据。
此页面提供基本的搜索语法,帮助您入门。
查询代理将简单的英文问题自动转换为优化的 Weaviate 查询 - 无需手动构建查询。
列出对象
您可以获取对象,而无需指定任何参数。这将以升序 UUID 顺序返回对象。
如果某个片段无法工作或您有任何反馈,请打开一个 GitHub issue。
jeopardy = client.collections.use("JeopardyQuestion")
response = jeopardy.query.fetch_objects()
for o in response.objects:
print(o.properties)
示例响应
输出如下所示
{
"data": {
"Get": {
"JeopardyQuestion": [
{
"question": "This prophet passed the time he spent inside a fish offering up prayers"
},
// shortened for brevity
]
}
}
}
其他信息
指定您希望查询返回的信息。您可以返回对象属性、对象 ID 和对象元数据。
limit 返回的对象
使用 limit 设置要返回的对象的固定最大数量。
如果某个片段无法工作或您有任何反馈,请打开一个 GitHub issue。
jeopardy = client.collections.use("JeopardyQuestion")
response = jeopardy.query.fetch_objects(
limit=1
)
for o in response.objects:
print(o.properties)
示例响应
输出如下所示
{
"data": {
"Get": {
"JeopardyQuestion": [
{
"question": "This prophet passed the time he spent inside a fish offering up prayers"
},
// Note this will only have one result as we limited it to 1
]
}
}
}
使用 limit 和 offset 进行分页
要在结果集的中间开始,请定义一个 offset。设置一个 limit 以从偏移量处返回对象。
如果某个片段无法工作或您有任何反馈,请打开一个 GitHub issue。
jeopardy = client.collections.use("JeopardyQuestion")
response = jeopardy.query.fetch_objects(
limit=1,
offset=1
)
for o in response.objects:
print(o.properties)
示例响应
输出如下所示
{
"data": {
"Get": {
"JeopardyQuestion": [
{
"question": "Pythons are oviparous, meaning they do this"
}
]
}
}
}
要分页浏览整个数据库,请使用 游标 代替 offset 和 limit。
检索对象 属性
您可以指定要返回哪些对象属性。默认情况下,将返回所有属性和对象 UUID。除非另有说明,否则将排除 Blob 和引用属性(这不适用于 Go 和 Java v5 客户端库)。
如果某个片段无法工作或您有任何反馈,请打开一个 GitHub issue。
jeopardy = client.collections.use("JeopardyQuestion")
response = jeopardy.query.fetch_objects(
limit=1,
return_properties=["question", "answer", "points"]
)
for o in response.objects:
print(o.properties)
示例响应
输出如下所示
{
"data": {
"Get": {
"JeopardyQuestion": [
{
"answer": "Jonah",
"points": 100,
"question": "This prophet passed the time he spent inside a fish offering up prayers"
},
]
}
}
}
检索对象 向量
您可以检索对象向量。(同样适用于使用 命名向量 的情况。)
如果某个片段无法工作或您有任何反馈,请打开一个 GitHub issue。
jeopardy = client.collections.use("JeopardyQuestion")
response = jeopardy.query.fetch_objects(
include_vector=True,
limit=1
)
print(response.objects[0].vector["default"])
示例响应
输出如下所示
{
"data": {
"Get": {
"JeopardyQuestion": [
{
"_additional": {
"vector": [
0.0065065133,
-0.017786196,
0.005879146,
0.006707012,
... // shortened for brevity
]
}
},
]
}
}
}
检索对象 id
您可以检索对象 id (uuid)。
如果某个片段无法工作或您有任何反馈,请打开一个 GitHub issue。
jeopardy = client.collections.use("JeopardyQuestion")
response = jeopardy.query.fetch_objects(
# Object IDs are included by default with the `v4` client! :)
limit=1
)
for o in response.objects:
print(o.uuid)
示例响应
输出如下所示
{
"data": {
"Get": {
"JeopardyQuestion": [
{
"_additional": {
"id": "0002bf92-80c8-5d94-af34-0d6c5fea1aaf"
}
},
// shortened for brevity
]
}
}
}
检索交叉引用属性
涉及交叉引用的查询可能比不涉及交叉引用的查询慢,尤其是在大规模情况下,例如对于多个对象或复杂查询。
首先,我们强烈建议您考虑是否可以避免在数据模式中使用交叉引用。作为可扩展的 AI 数据库,Weaviate 擅长使用向量、关键词和混合搜索进行复杂查询,包括过滤器。您可以通过重新思考数据模式来避免交叉引用,从而受益匪浅。
例如,与其创建带有交叉引用的单独的“作者”和“书籍”集合,不如考虑将作者信息直接嵌入到书籍对象中,并使用搜索和过滤器按作者特征查找书籍。
要检索交叉引用对象的属性,请指定
- 交叉引用属性
- 目标交叉引用集合
- 要检索的属性
如果某个片段无法工作或您有任何反馈,请打开一个 GitHub issue。
from weaviate.classes.query import QueryReference
jeopardy = client.collections.use("JeopardyQuestion")
response = jeopardy.query.fetch_objects(
return_references=[
QueryReference(
link_on="hasCategory",
return_properties=["title"]
),
],
limit=2
)
for o in response.objects:
print(o.properties["question"])
# print referenced objects
for ref_obj in o.references["hasCategory"].objects:
print(ref_obj.properties)
示例响应
输出如下所示
{
"data": {
"Get": {
"JeopardyQuestion": [
{
"hasCategory": [{"title": "THE BIBLE"}],
"question": "This prophet passed the time he spent inside a fish offering up prayers",
},
{
"hasCategory": [{"title": "ANIMALS"}],
"question": "Pythons are oviparous, meaning they do this",
},
]
}
}
}
检索元数据值
您可以指定要返回的元数据字段。
如果某个片段无法工作或您有任何反馈,请打开一个 GitHub issue。
from weaviate.classes.query import MetadataQuery
jeopardy = client.collections.use("JeopardyQuestion")
response = jeopardy.query.fetch_objects(
limit=1,
return_metadata=MetadataQuery(creation_time=True)
)
for o in response.objects:
print(o.properties) # View the returned properties
print(o.metadata.creation_time) # View the returned creation time
有关元数据字段的完整列表,请参阅 GraphQL:附加属性。
多租户
如果 多租户 已启用,请在每个查询中指定 tenant 参数。
如果某个片段无法工作或您有任何反馈,请打开一个 GitHub issue。
# Connect to the collection
mt_collection = client.collections.use("WineReviewMT")
# Get the specific tenant's version of the collection
collection_tenant_a = mt_collection.with_tenant("tenantA")
# Query tenantA's version
response = collection_tenant_a.query.fetch_objects(
return_properties=["review_body", "title"],
limit=1,
)
print(response.objects[0].properties)
复制
对于启用了复制的集合,您可以在查询中指定一致性级别。这适用于 CRUD 查询以及搜索。
如果某个片段无法工作或您有任何反馈,请打开一个 GitHub issue。
from weaviate.classes.config import ConsistencyLevel
questions = client.collections.use(collection_name).with_consistency_level(
consistency_level=ConsistencyLevel.QUORUM
)
response = collection.query.fetch_object_by_id("36ddd591-2dee-4e7e-a3cc-eb86d30a4303")
# The parameter passed to `withConsistencyLevel` can be one of:
# * 'ALL',
# * 'QUORUM' (default), or
# * 'ONE'.
#
# It determines how many replicas must acknowledge a request
# before it is considered successful.
for o in response.objects:
print(o.properties) # Inspect returned objects
相关页面
- 连接到 Weaviate
- API 参考:GraphQL:Get
- 有关教程,请参阅 查询
- 有关使用 GraphQL API 进行搜索,请参阅 GraphQL API
问题和反馈
如果您有任何问题或反馈,请在 用户论坛 中告诉我们。
