AI

Add a Property in Weaviate

Weaviate is a powerful free and open-source, graph-based, and real-time vector search engine that enables us to search through the large amounts of data using Natural Language Processing.

Weaviate uses data objects to structure and store the information about various objects. Each object in Weaviate is classified by a specific class which includes the properties of that object.

We can add a new property to an existing class or update an already existing property. This makes it flexible which allows us to make changes without re-creating the entire schema.

This tutorial teaches us how to create a new property to an existing object in Weaviate using the API endpoints and the Python client.

What Is a Property in Weaviate?

Let us start at the basics and define what property in Weaviate is. In the simplest terms, a Weaviate property specifies the data structure associated with a given class.

For example, suppose you have a class that stores the “Person” information. The properties of that class could be things like their name, age, email address, and more.

As you can guess, you often encounter scenarios where you need to add a property to an existing class after creation. This is where the add property feature in Weaviate comes into play.

Let us explore how to add a property to a class after creation.

Before starting, you should install Python on your system and have a basic knowledge of working with Python.

You can install the Weaviate client with the following command:

pip install weaviate

Initialize the Connection to Weaviate

Once installed, create a file to store the source code and add the line as shown in the following to connect to the Weaviate instance.

import weaviate
client = weaviate.Client("http://localhost:8080")

If your Weaviate instance is running on a different address, replace the http://localhost:8080 with your Weaviate URL.

Create a New Class

Before attempting to add a new property, we need to ensure that the class that we target already exists on the server.

Hence, let us start by creating a new class that stores the “Person” information as follows:

client.schema.create_class(
    {
        "class": "Person",
        "description": "A class to represent a person",
        "properties": [
            {
                "name": "name",
                "description": "The name of the person",
                "dataType": ["string"]
            },
            {
                "name": "age",
                "description": "The age of the person",
                "dataType": ["int"]
            }
        ]
    }
)

This should create a new “Person” class with the name and age properties. The “name” property stores the person’s name as a string while the “age” property stores the person’s age as an integer.

You can verify that the class is created using the code as follows:

person_class = client.schema.get('Person')

print(person_class)

This should return a detailed information about the “Person” class.

Add a Property to a Class in Weaviate

Once we ensured that the class exists, we can add a new property. We can accomplish this using the schema.property.create() method and passing the target class and the property that we wish to add.

add_prop = {
"dataType": [
"date"
],
"name": "DOB"
}

client.schema.property.create("Person", add_prop)

The previous code adds the DOB property to the “Person” class with the date data type.

Using the API Endpoint

You can also make a POST request to the v1/schema/{class_name}/properties endpoint to add a new property to the class.

You can pass the property information as a JSON body as shown in the following example:

$ curl \
-X POST \
-H "Content-Type: application/json" \
-d '{
"dataType": [
"string"
],
"name": "email"
}'
\
http://localhost:8080/v1/schema/Person/properties

In this case, the previous command should add the email property to the “Person” class.

Conclusion

This tutorial taught us how to add a new property to an existing class in the Weaviate database using the Python client and the API endpoint.

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