How to define nestedProperties in v4 python client?

It is not clear to me how I can use the v4 api to define nested properties. Can we get an example?

Thanks,
Robert

Hi @rhuang !

This is indeed not documented :thinking:

Here is a simple example:

import weaviate
from weaviate import classes as wvc

client = weaviate.connect_to_local()

client.collections.delete("NestedPropertyCollection")
nested = client.collections.create(
    name="NestedPropertyCollection",
    properties=[
        wvc.config.Property(
            name="nested_property",
            data_type=wvc.config.DataType.OBJECT,
            nested_properties=[
                wvc.config.Property(
                    name="sub_property1",
                    data_type=wvc.config.DataType.TEXT
                ),
                wvc.config.Property(
                    name="sub_property2",
                    data_type=wvc.config.DataType.INT
                )
            ]
        )
    ]
)

and this is how you can add some data:

nested.data.insert({
    "nested_property": {"sub_property1": "somedata", "sub_property2": 123}
})

Let me know if this helps :slight_smile:

1 Like

Thank you for your reply @DudaNogueira .

Can we have more than one level of nested properties?

Sure!

from weaviate import classes as wvc
client.collections.delete("NestedNestedProperty")
nested = client.collections.create(
    name="NestedNestedProperty",
    properties=[
        wvc.config.Property(
            name="nested_property",
            data_type=wvc.config.DataType.OBJECT,
            nested_properties=[
                wvc.config.Property(
                    name="sub_property1",
                    data_type=wvc.config.DataType.TEXT
                ),
                wvc.config.Property(
                    name="sub_nested",
                    data_type=wvc.config.DataType.OBJECT,
                    nested_properties=wvc.config.Property(name="inside", data_type=wvc.config.DataType.TEXT)
                )
            ]
        )
    ]
)
nested.data.insert({
    "nested_property": {"sub_property1": "somedata", "sub_nested": {"inside": "value"}}
})

now when you fetch those objects:

nested.query.fetch_objects().objects[0].properties

you get this:

{'nested_property': {'sub_property1': 'somedata',
  'sub_nested': {'inside': 'value'}}}