Define ontology classes to categorize nodes with semantic meaning. Classes support inheritance hierarchies and can be marked as disjoint.
Define a new ontology class:
GQLCREATE CLASS @ex:Person
Create a class with description:
GQLCREATE CLASS @ex:Employee DESCRIPTION 'A person who works for an organization'
Create multiple related classes:
GQLCREATE CLASS @ex:Person CREATE CLASS @ex:Organization CREATE CLASS @ex:Location
Create subclasses using EXTENDS to define inheritance:
GQL// Person is a subclass of Agent CREATE CLASS @foaf:Agent CREATE CLASS @foaf:Person EXTENDS @foaf:Agent CREATE CLASS @foaf:Organization EXTENDS @foaf:Agent
GQL// Employee extends Person CREATE CLASS @ex:Person CREATE CLASS @ex:Employee EXTENDS @ex:Person CREATE CLASS @ex:Manager EXTENDS @ex:Employee
When you query for a superclass, nodes with subclass labels are automatically included:
GQL// Insert an Employee (which extends Person) INSERT (:@ex:Employee {name: 'Alice', role: 'Engineer'})
GQL// Query for Person - also returns Employee nodes MATCH (n@ex:Person) RETURN n.name, n.role
| n.name | n.role |
|---|---|
| Alice | Engineer |
GQL// Query specifically for Employee MATCH (n@ex:Employee) RETURN n.name, n.role
| n.name | n.role |
|---|---|
| Alice | Engineer |
Mark classes as mutually exclusive - a node cannot have labels from both:
GQLCREATE CLASS @ex:Cat CREATE CLASS @ex:Dog DISJOINT WITH @ex:Cat
Attempting to create a node with both labels will fail in STRICT enforcement mode:
GQL// This fails: Cannot be both Cat and Dog INSERT (:@ex:Cat&@ex:Dog {name: 'Mystery'}) // Error: Disjoint class violation
Multiple disjoint declarations:
GQLCREATE CLASS @ex:Mammal CREATE CLASS @ex:Bird DISJOINT WITH @ex:Mammal CREATE CLASS @ex:Fish DISJOINT WITH @ex:Mammal, @ex:Bird
List all defined classes:
GQLSHOW CLASSES
| class | superclass | disjoint_with | description |
|---|---|---|---|
| @ex:Person | |||
| @ex:Employee | @ex:Person | A person who works for an organization | |
| @ex:Cat | |||
| @ex:Dog | @ex:Cat |
Remove a class definition:
GQLDROP CLASS @ex:TempClass
Remove if exists (no error if class doesn't exist):
GQLDROP CLASS IF EXISTS @ex:TempClass