Weaviate is an open-source, decentralized knowledge graph system that enables efficient semantic search and recommendation systems. It uses graph databases and machine learning to organize and retrieve complex data structures.
In Weaviate, the schema or class defines the structure and organization of data. It specifies the entities, their properties, and the relationships between them. The schema ensures data consistency and provides a blueprint for data modeling and indexing in Weaviate.
Each class in the schema defines the properties and relationships associated with that particular entity type. The role of classes is to allow for efficient organization and retrieval of data based on their shared characteristics and relationships.
Requirements
In order to follow along with this post:
- A running Weaviate cluster
- Python 3.10 and above.
- Weaviate Python client installed
With the above conditions met, we can proceed.
Weaviate Create Schema
The first step is to create a basic schema to store information. For this, we will create a basic schema to store film information.
import weaviate
import json
client = weaviate.Client("http://localhost:8080")
class_obj = {
"classes": [
{
"class": "Film",
"description": "A class representing a film.",
"properties": [
{
"name": "title",
"dataType": ["string"],
"description": "The title of the film."
},
{
"name": "director",
"dataType": ["string"],
"description": "The director of the film."
},
{
"name": "year",
"dataType": ["int"],
"description": "The year the film was released."
},
{
"name": "genre",
"dataType": ["string"],
"description": "The genre of the film."
},
{
"name": "actors",
"dataType": ["string"],
"description": "The actors in the film."
}
]
}
]
}
client.schema.create_class(class_obj)
schema = client.schema.get()
print(json.dumps(schema))
This should configure a basic class that allows us to store film information in the database. You can learn more about Weaviate data types in the official docs.
Weaviate Update Class
Once you have defined a class, you can use the update_config() method to update various objects of a class, as demonstrated in the example code below:
class_obj = {
'invertedIndexConfig': {
'stopwords': {
'preset': 'en',
},
},
}
client.schema.update_config('Film', class_obj)
This should update the configuration of the Film class with the defined parameters.
You can also use the add_property() method to add a new property to an existing class as demonstrated below:
add_prop = {
'name': 'meta',
'dataType': ['text'],
}
client.schema.property.create('Film', add_prop)
This should add a meta property to the class of type text.
Conclusion
In this tutorial, you learned how to create a Weaviate class using Python Client. You also learned how to update a class's configuration or add a new property to an existing class.