Weaviate GraphQL 查询
掌握使用 Weaviate 强大的 GraphQL 应用程序接口进行语义搜索和数据检索。
Weaviate GraphQL 查询 是 CoddyKit 上的免费 Vector Databases: Pinecone, Weaviate & pgvector 课时。 这是第 3 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Vector Databases: Pinecone, Weaviate & pgvector 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Vector Databases: Pinecone, Weaviate & pgvector 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Weaviate's GraphQL Power
Welcome! In this lesson, you'll master querying your Weaviate data. Weaviate uses GraphQL, a powerful query language, for flexible and efficient data retrieval.
GraphQL allows you to request exactly the data you need, nothing more, nothing less. This is especially useful for complex searches, including vector similarity.
Basic Data Retrieval: Get
The foundation of querying in Weaviate is the Get operation. It allows you to fetch data objects from a specific class defined in your schema.
- Specify the class name (e.g.,
Article). - Select the properties you want to retrieve (e.g.,
title,content). - Weaviate will return all objects of that class with the specified properties.
Get All Objects Example
Let's see a basic Get query in action. This Python code connects to your Weaviate instance and fetches the title and content of all Article objects.
(Ensure you have a Weaviate instance running locally at http://localhost:8080 and an 'Article' class with some data.)
import weaviate
# Connect to your Weaviate instance
# For local: http://localhost:8080
client = weaviate.Client(url="http://localhost:8080")
# Perform a basic Get query for 'Article' objects
try:
response = client.query.get("Article", ["title", "content"]).do()
print("--- Retrieved Articles ---")
for article in response["data"]["Get"]["Article"]:
print(f"Title: {article['title']}")
except Exception as e:
print(f"Error during query: {e}")Filtering Data with 'where'
Often, you don't want all objects, but specific ones. The where filter allows you to add conditions to your queries, narrowing down the results.
- You can filter by property values (text, number, boolean, date).
- Use operators like
Equal,Like,GreaterThan, etc. - Combine multiple conditions with
_andor_orclauses.
Filter by Property Value
This example shows how to use the where filter to find articles written by a specific author. We'll look for articles where the author property matches 'Jane Doe'.
import weaviate
client = weaviate.Client(url="http://localhost:8080")
# Define the 'where' filter
where_filter = {
"path": ["author"],
"operator": "Equal",
"valueText": "Jane Doe"
}
# Perform the Get query with the filter
try:
response = client.query.get("Article", ["title", "author"])
.with_where(where_filter).do()
print("--- Articles by Jane Doe ---")
for article in response["data"]["Get"]["Article"]:
print(f"Title: {article['title']}, Author: {article['author']}")
except Exception as e:
print(f"Error during query: {e}")Retrieving Vector Embeddings
Weaviate stores a vector embedding for each object, representing its semantic meaning. You can retrieve this vector directly as part of your GraphQL query.
To do this, you use the _additional { vector } clause. This is useful for debugging, understanding your data, or performing custom operations outside Weaviate.
Get Vector Embedding
Here's how to fetch the vector embedding along with other properties. We'll get the title and the vector for the first article found.
import weaviate
client = weaviate.Client(url="http://localhost:8080")
# Query for a title and its vector
try:
response = client.query.get("Article", ["title"])
.with_additional("vector")
.with_limit(1).do()
print("--- Article Title and Vector ---")
if response["data"]["Get"]["Article"]:
article = response["data"]["Get"]["Article"][0]
print(f"Title: {article['title']}")
print(f"Vector (first 5 elements): {article['_additional']['vector'][:5]}...")
else:
print("No articles found.")
except Exception as e:
print(f"Error during query: {e}")Powerful Semantic Search
One of Weaviate's core strengths is semantic search. Instead of keyword matching, it finds data objects based on their meaning, even if the exact words aren't present.
This is achieved using the nearText operator. You provide 'concepts' (text) and Weaviate uses them to find the most semantically similar objects in your database.
Find Similar Articles
Let's perform a semantic search to find articles related to 'machine learning applications'. Notice how we use with_near_text and provide our query concepts.
import weaviate
client = weaviate.Client(url="http://localhost:8080")
# Define the concepts for semantic search
concepts = ["machine learning applications"]
# Perform a nearText query
try:
response = client.query.get("Article", ["title", "content"])
.with_near_text({"concepts": concepts})
.with_limit(3).do()
print("--- Articles similar to 'machine learning applications' ---")
for article in response["data"]["Get"]["Article"]:
print(f"Title: {article['title']}")
except Exception as e:
print(f"Error during query: {e}")GraphQL Query Challenge
You've learned about various ways to query data in Weaviate using GraphQL.
Which of the following GraphQL query operations is specifically used to find data objects that are semantically similar to a given text concept?
Weaviate Queries Recap
Great job! You've mastered the essentials of Weaviate's powerful GraphQL API:
- The
Getoperation retrieves data objects and their properties. - The
wherefilter allows you to apply precise conditions to your searches. - You can explicitly fetch an object's vector embedding using
_additional { vector }. - The
nearTextoperator powers semantic search, finding items based on meaning.
These tools enable you to efficiently retrieve and explore your vector data in Weaviate!
常见问题解答
「Weaviate GraphQL 查询」课时是免费的吗?
是的 — 「Weaviate GraphQL 查询」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Vector Databases: Pinecone, Weaviate & pgvector 课程的其余内容,请升级到 CoddyKit PRO。 Vector Databases: Pinecone, Weaviate & pgvector 课程共包含 4 节课。
「Weaviate GraphQL 查询」这节课中我会学到什么?
掌握使用 Weaviate 强大的 GraphQL 应用程序接口进行语义搜索和数据检索。 你通过在浏览器中直接运行的动手代码来练习 Vector Databases: Pinecone, Weaviate & pgvector,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Vector Databases: Pinecone, Weaviate & pgvector 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Vector Databases: Pinecone, Weaviate & pgvector 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 3 节课,共 4 节。
「Weaviate GraphQL 查询」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Vector Databases: Pinecone, Weaviate & pgvector 课中编写并运行代码吗?
能。每节 Vector Databases: Pinecone, Weaviate & pgvector 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 定义 Weaviate 模式
- 导入数据对象
- Weaviate GraphQL 查询
- 向量化模块与自动嵌入