การกำหนดสคีมา Weaviate
ออกแบบและจัดการสคีมาข้อมูลใน Weaviate โดยกำหนดคลาสและคุณสมบัติสำหรับออบเจ็กต์เวกเตอร์
การกำหนดสคีมา Weaviate เป็นบทเรียน Vector Databases: Pinecone, Weaviate & pgvector ฟรีบน CoddyKit นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน Vector Databases: Pinecone, Weaviate & pgvector และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส Vector Databases: Pinecone, Weaviate & pgvector มีบทเรียนทั้งหมด 4 บทเรียน
บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ
Weaviate Schema: Your Data Map
Welcome to Weaviate! To store and search data effectively, Weaviate needs to understand its structure. This is where the schema comes in.
Think of a schema as a blueprint for your data. It defines the types of data you'll store and how they relate.
Why Schema Matters
A well-defined schema is crucial for:
- Data Consistency: Ensuring all objects conform to expected types.
- Efficient Indexing: Weaviate uses the schema to build efficient search indexes.
- Vectorization: Guiding how Weaviate generates and stores vector embeddings for your data.
- Querying: Enabling powerful semantic and filtered searches.
Classes: Main Data Types
In Weaviate, the primary building blocks of your schema are classes. A class is like a table in a relational database or a collection in a NoSQL database.
Each class represents a distinct type of object you want to store, such as an "Article", "Product", or "User".
Your First Weaviate Class
Let's define a simple class named Article. This class will represent articles in our database.
In Weaviate, a class definition includes its name and an optional description. We'll add properties later.
import weaviate
if __name__ == "__main__":
# Define a basic class schema
article_class_schema = {
"class": "Article",
"description": "A class for storing articles"
}
print("--- Defined Article Class Schema ---")
print(f"Class Name: {article_class_schema['class']}")
print(f"Description: {article_class_schema['description']}")
# In a real app, you'd add this to Weaviate:
# client.schema.create_class(article_class_schema)Properties: Data Fields
Within each class, you define properties. These are the individual fields or attributes that describe an object of that class.
For an Article class, properties might include title, content, author, or publicationDate.
Essential Property Types
Weaviate supports various data types for properties. Some common ones include:
text: For strings of text (e.g., names, descriptions).int: For whole numbers.number: For floating-point numbers.boolean: For true/false values.date: For date and time values.text[]: For arrays of text strings.
Each property must have a defined data type.
Class with Properties
Now, let's enhance our Article class by adding some properties. We'll include title, content, and wordCount.
Notice how each property has a name and a dataType.
import weaviate
if __name__ == "__main__":
# Define a class schema with properties
article_class_with_props = {
"class": "Article",
"description": "A class for storing articles with properties",
"properties": [
{
"name": "title",
"dataType": ["text"],
"description": "The title of the article"
},
{
"name": "content",
"dataType": ["text"],
"description": "The main content of the article"
},
{
"name": "wordCount",
"dataType": ["int"],
"description": "The number of words in the article"
}
]
}
print("--- Defined Article Class with Properties ---")
print(f"Class Name: {article_class_with_props['class']}")
for prop in article_class_with_props['properties']:
print(f" Property: {prop['name']}, Type: {prop['dataType'][0]}")
# client.schema.create_class(article_class_with_props)Vectorization & Indexing
Weaviate automatically handles vectorizing your data and building a vector index. By default, it uses a text2vec model for text properties.
You can configure the vectorizer (e.g., which model to use) and the vector index (e.g., HNSW parameters) directly within your class definition.
import weaviate
if __name__ == "__main__":
# Example of a class with vectorizer configuration
product_class_schema = {
"class": "Product",
"description": "A class for e-commerce products",
"vectorizer": "text2vec-openai", # Or "text2vec-huggingface", etc.
"vectorIndexConfig": {
"efConstruction": 128,
"maxConnections": 64,
"ef": -1 # default for queries
},
"properties": [
{"name": "name", "dataType": ["text"]},
{"name": "description", "dataType": ["text"]}
]
}
print("--- Defined Product Class with Vector Config ---")
print(f"Class Name: {product_class_schema['class']}")
print(f"Vectorizer: {product_class_schema['vectorizer']}")
print(f"Vector Index Config (efConstruction): {product_class_schema['vectorIndexConfig']['efConstruction']}")
# client.schema.create_class(product_class_schema)Adding a New Property
What if you need to add a new property to an existing class? Weaviate allows you to update your schema by adding new properties.
You cannot change existing property types or delete properties directly without re-creating the class (and potentially losing data).
import weaviate
if __name__ == "__main__":
# Imagine 'Article' class already exists
# This defines the NEW property to add
new_property = {
"name": "category",
"dataType": ["text"],
"description": "The category of the article"
}
print("--- New Property Definition ---")
print(f"Name: {new_property['name']}, Type: {new_property['dataType'][0]}")
# In a real app, you'd add this to Weaviate:
# client.schema.property.create(
# class_name="Article",
# property=new_property
# )
print("\nTo add this, you'd call client.schema.property.create(class_name, property_object)")Schema Quick Check
Which of the following statements about Weaviate schema definition are TRUE?
Recap: Schema Essentials
You've learned the fundamentals of Weaviate schema definition!
- Classes are your main data types.
- Properties define the attributes within each class.
- Weaviate uses the schema for indexing and vectorization.
- You can add new properties to existing classes to evolve your schema.
A well-structured schema is key to powerful semantic search in Weaviate.
คำถามที่พบบ่อย
บทเรียน “การกำหนดสคีมา Weaviate” ฟรีหรือไม่
ใช่ — ข้อความเต็มของ “การกำหนดสคีมา Weaviate” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส Vector Databases: Pinecone, Weaviate & pgvector ให้อัปเกรดเป็น CoddyKit PRO คอร์ส Vector Databases: Pinecone, Weaviate & pgvector มีบทเรียนทั้งหมด 4 บทเรียน
คุณจะเรียนรู้อะไรในบทเรียน “การกำหนดสคีมา Weaviate”
ออกแบบและจัดการสคีมาข้อมูลใน Weaviate โดยกำหนดคลาสและคุณสมบัติสำหรับออบเจ็กต์เวกเตอร์ คุณปฏิบัติ Vector Databases: Pinecone, Weaviate & pgvector ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน
คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน Vector Databases: Pinecone, Weaviate & pgvector หรือไม่
ไม่จำเป็นต้องมีประสบการณ์มาก่อน Vector Databases: Pinecone, Weaviate & pgvector บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 1 จากทั้งหมด 4 บทเรียน
บทเรียน “การกำหนดสคีมา Weaviate” ใช้เวลานานแค่ไหน
บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย
ฉันเขียนและรันโค้ดในบทเรียน Vector Databases: Pinecone, Weaviate & pgvector นี้ได้ไหม
ได้ บทเรียน Vector Databases: Pinecone, Weaviate & pgvector ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ
บทเรียนทั้งหมดในหลักสูตรนี้
- การกำหนดสคีมา Weaviate
- การนำเข้าออบเจ็กต์ข้อมูล
- คำค้น GraphQL ใน Weaviate
- โมดูลสร้างเวกเตอร์และการสร้างเวกเตอร์ฝังอัตโนมัติ