Vector Databases: Pinecone, Weaviate & pgvector · บทเรียน

คำค้น GraphQL ใน Weaviate

เชี่ยวชาญการค้นข้อมูล Weaviate ด้วยเอพีไอ GraphQL ที่ทรงพลัง เพื่อการค้นหาเชิงความหมายและการค้นคืนข้อมูล

บทเรียน 3 จาก 411 ขั้นตอน

คำค้น GraphQL ใน Weaviate เป็นบทเรียน Vector Databases: Pinecone, Weaviate & pgvector ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน 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 _and or _or clauses.

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 Get operation retrieves data objects and their properties.
  • The where filter allows you to apply precise conditions to your searches.
  • You can explicitly fetch an object's vector embedding using _additional { vector }.
  • The nearText operator powers semantic search, finding items based on meaning.

These tools enable you to efficiently retrieve and explore your vector data in Weaviate!

เริ่มต้นได้ฟรี

เรียนรู้ Vector Databases: Pinecone, Weaviate & pgvector ด้วย AI tutor — ฟรี

เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป

คอร์ส
12
บทเรียน
48

คำถามที่พบบ่อย

บทเรียน “คำค้น GraphQL ใน Weaviate” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “คำค้น GraphQL ใน Weaviate” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Vector Databases: Pinecone, Weaviate & pgvector ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Vector Databases: Pinecone, Weaviate & pgvector มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “คำค้น GraphQL ใน Weaviate”

เชี่ยวชาญการค้นข้อมูล Weaviate ด้วยเอพีไอ GraphQL ที่ทรงพลัง เพื่อการค้นหาเชิงความหมายและการค้นคืนข้อมูล คุณปฏิบัติ Vector Databases: Pinecone, Weaviate & pgvector ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Vector Databases: Pinecone, Weaviate & pgvector หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน Vector Databases: Pinecone, Weaviate & pgvector บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน

บทเรียน “คำค้น GraphQL ใน Weaviate” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน Vector Databases: Pinecone, Weaviate & pgvector นี้ได้ไหม

ได้ บทเรียน Vector Databases: Pinecone, Weaviate & pgvector ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. การกำหนดสคีมา Weaviate
  2. การนำเข้าออบเจ็กต์ข้อมูล
  3. คำค้น GraphQL ใน Weaviate
  4. โมดูลสร้างเวกเตอร์และการสร้างเวกเตอร์ฝังอัตโนมัติ
← กลับไปที่ Vector Databases: Pinecone, Weaviate & pgvector