weaviate.schema¶
Module used to manipulate schemas.
- class weaviate.schema.Schema(connection: weaviate.connect.connection.Connection)¶
Bases:
object
Schema class used to interact and manipulate schemas or classes.
- property¶
A Property object to create new schema property/ies.
Initialize a Schema class instance.
- Parameters
connection (weaviate.connect.Connection) – Connection object to an active and running weaviate instance.
- contains(schema: Optional[Union[dict, str]] = None) bool ¶
Check if weaviate already contains a schema.
- Parameters
schema (dict or str, optional) – Schema as a python dict, or the path to a json file or a url of a json file. If a schema is given it is checked if this specific schema is already loaded. It will test only this schema. If the given schema is a subset of the loaded schema it will still return true, by default None.
Examples
>>> schema = client.schema.get() >>> client.schema.contains(schema) True >>> schema = client.schema.get() >>> schema['classes'].append( { "class": "Animal", "description": "An Animal", "properties": [ { "name": "type", "dataType": ["string"], "description": "The animal type", } ] } ) >>> client.schema.contains(schema) False
- Returns
True if a schema is present, False otherwise.
- Return type
bool
- create(schema: Union[dict, str]) None ¶
Create the schema at the weaviate instance.
- Parameters
schema (dict or str) – Schema as a python dict, or the path to a json file or a url of a json file.
Examples
>>> author_class_schema = { ... "class": "Author", ... "description": "An Author class to store the author information", ... "properties": [ ... { ... "name": "name", ... "dataType": ["string"], ... "description": "The name of the author", ... }, ... { ... "name": "wroteArticles", ... "dataType": ["Article"], ... "description": "The articles of the author", ... } ... ] ... } >>> client.schema.create(author_class_schema)
If you have your schema saved in the ‘./schema/my_schema.json’ you can create it directly from the file.
>>> client.schema.create('./schema/my_schema.json')
- Raises
TypeError – If the ‘schema’ is neither a string nor a dict.
ValueError – If ‘schema’ can not be converted into a weaviate schema.
requests.ConnectionError – If the network connection to weaviate fails.
weaviate.UnexpectedStatusCodeException – If weaviate reports a none OK status.
weaviate.SchemaValidationException – If the ‘schema’ could not be validated against the standard format.
- create_class(schema_class: Union[dict, str]) None ¶
Create a single class as part of the schema in weaviate.
- Parameters
schema_class (dict or str) – Class as a python dict, or the path to a json file or a url of a json file.
Examples
>>> author_class_schema = { ... "class": "Author", ... "description": "An Author class to store the author information", ... "properties": [ ... { ... "name": "name", ... "dataType": ["string"], ... "description": "The name of the author", ... }, ... { ... "name": "wroteArticles", ... "dataType": ["Article"], ... "description": "The articles of the author", ... } ... ] ... } >>> client.schema.create_class(author_class_schema)
If you have your class schema saved in the ‘./schema/my_schema.json’ you can create it directly from the file.
>>> client.schema.create_class('./schema/my_schema.json')
- Raises
TypeError – If the ‘schema_class’ is neither a string nor a dict.
ValueError – If ‘schema_class’ can not be converted into a weaviate schema.
requests.ConnectionError – If the network connection to weaviate fails.
weaviate.UnexpectedStatusCodeException – If weaviate reports a none OK status.
weaviate.SchemaValidationException – If the ‘schema_class’ could not be validated against the standard format.
- delete_all() None ¶
Remove the entire schema from the Weaviate instance and all data associated with it.
Examples
>>> client.schema.delete_all()
- delete_class(class_name: str) None ¶
Delete a schema class from weaviate. This deletes all associated data.
- Parameters
class_name (str) – The class that should be deleted from weaviate.
Examples
>>> client.schema.delete_class('Author')
- Raises
TypeError – If ‘class_name’ argument not of type str.
requests.ConnectionError – If the network connection to weaviate fails.
weaviate.UnexpectedStatusCodeException – If weaviate reports a none OK status.
- get(class_name: Optional[str] = None) dict ¶
Get the schema from weaviate.
- Parameters
class_name (str, optional) – The class for which to return the schema. If NOT provided the whole schema is returned, otherwise only the schema of this class is returned. By default None.
- Returns
A dict containing the schema. The schema may be empty. To see if a schema has already been loaded use contains method.
- Return type
dict
Examples
No schema present in client
>>> client.schema.get() {'classes': []}
Schema present in client
>>> client.schema.get() { "classes": [ { "class": "Animal", "description": "An Animal", "invertedIndexConfig": { "cleanupIntervalSeconds": 60 }, "properties": [ { "dataType": [ "string" ], "description": "The animal type", "name": "type" } ], "vectorIndexConfig": { "cleanupIntervalSeconds": 300, "maxConnections": 64, "efConstruction": 128, "vectorCacheMaxObjects": 500000 }, "vectorIndexType": "hnsw", "vectorizer": "text2vec-contextionary" } ] }
>>> client.schema.get('Animal') { "class": "Animal", "description": "An Animal", "invertedIndexConfig": { "cleanupIntervalSeconds": 60 }, "properties": [ { "dataType": [ "string" ], "description": "The animal type", "name": "type" } ], "vectorIndexConfig": { "cleanupIntervalSeconds": 300, "maxConnections": 64, "efConstruction": 128, "vectorCacheMaxObjects": 500000 }, "vectorIndexType": "hnsw", "vectorizer": "text2vec-contextionary" }
- Raises
requests.ConnectionError – If the network connection to weaviate fails.
weaviate.UnexpectedStatusCodeException – If weaviate reports a none OK status.
- get_class_shards(class_name: str) list ¶
Get the status of all shards in an index.
- Parameters
class_name (str) – The class for which to return the status of all shards in an index.
- Returns
The list of shards configuration.
- Return type
list
Examples
Schema contains a single class: Article
>>> client.schema.get_class_shards('Article') [{'name': '2rPgsA2yngW3', 'status': 'READY'}]
- Raises
requests.ConnectionError – If the network connection to weaviate fails.
weaviate.UnexpectedStatusCodeException – If weaviate reports a none OK status.
- update_class_shard(class_name: str, status: str, shard_name: Optional[str] = None) list ¶
Get the status of all shards in an index.
- Parameters
class_name (str) – The class for which to update the status of all shards in an index.
status (str) – The new status of the shard. The available options are: ‘READY’ and ‘READONLY’.
shard_name (str or None, optional) – The shard name for which to update the status of the class of the shard. If None then all the shards are going to be updated to the ‘status’. By default None.
- Returns
The updated statuses.
- Return type
list
Examples
Schema contains a single class: Article
>>> client.schema.get_class_shards('Article') [{'name': 'node1', 'status': 'READY'}, {'name': 'node2', 'status': 'READY'}]
For a specific shard:
>>> client.schema.update_class_shard('Article', 'READONLY', 'node2') {'status': 'READONLY'} >>> client.schema.get_class_shards('Article') [{'name': 'node1', 'status': 'READY'}, {'name': 'node2', 'status': 'READONLY'}]
For all shards of the class:
>>> client.schema.update_class_shard('Article', 'READONLY') [{'status': 'READONLY'},{'status': 'READONLY'}] >>> client.schema.get_class_shards('Article') [{'name': 'node1', 'status': 'READONLY'}, {'name': 'node2', 'status': 'READONLY'}]
- Raises
requests.ConnectionError – If the network connection to weaviate fails.
weaviate.UnexpectedStatusCodeException – If weaviate reports a none OK status.
- update_config(class_name: str, config: dict) None ¶
Update a schema configuration for a specific class.
- Parameters
class_name (str) – The class for which to update the schema configuration.
config (dict) – The configurations to update (MUST follow schema format).
- Raises
requests.ConnectionError – If the network connection to weaviate fails.
weaviate.UnexpectedStatusCodeException – If weaviate reports a none OK status.
weaviate.schema.properties¶
weaviate.schema.validate_schema¶
Schema validation module.
- weaviate.schema.validate_schema.check_class(class_definition: dict) None ¶
Validate a class against the standard class format.
- Parameters
class_definition (dict) – The definition of the class to be validated.
- Raises
weaviate.SchemaValidationException – If the class could not be validated against the standard class format.
- weaviate.schema.validate_schema.check_property(class_property: dict) None ¶
Validate a class property against the standard class property.
- Parameters
class_property (dict) – The class property to be validated.
- Raises
weaviate.SchemaValidationException – If the class property could not be validated against the standard class property format.
- weaviate.schema.validate_schema.validate_schema(schema: dict) None ¶
Validate schema.
- Parameters
schema (dict) – Schema to be validated.
- Raises
weaviate.SchemaValidationException – If the schema could not be validated against the standard format.