AI

Update a Class in Weaviate

Weaviate is an open-source, decentralized knowledge graph system that enables the efficient semantic search and recommendation systems. It uses the graph databases and machine learning to organize and retrieve the 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 the data consistency and provides a blueprint for data modeling and indexing in Weaviate.

Each class in the schema defines the properties and relationships that are 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.

This tutorial teaches us how to update an existing Weaviate class using the Python Client.

Requirements:

In order to follow along with this post, ensure that you have the following requirements:

  1. A running Weaviate cluster
  2. Python 3.10 and above
  3. Installed Weaviate Python client

With the given conditions met, we can proceed to the installation process.

Create a Schema in Weaviate

The first step is to create a basic schema to store the information. For this, we create a basic schema to store the 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 the film information in the database. You can learn more about the Weaviate data types in the official docs.

Update the Class in Weaviate

Once you define a class, you can use the update_config() method to update various objects of a class as demonstrated in the following example code:

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 in the following:

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

You learned how to create a Weaviate class using the Python client. You also learned how to update a class configuration or add a new property to an existing class.

About the author

John Otieno

My name is John and am a fellow geek like you. I am passionate about all things computers from Hardware, Operating systems to Programming. My dream is to share my knowledge with the world and help out fellow geeks. Follow my content by subscribing to LinuxHint mailing list