The Cypher language interacts with data organized in a graph structure, unlike tabular or document-based models. This graph-based model in Cypher consists of Nodes, Relationships, Properties, Labels, and Paths. Understanding these elements is fundamental to working with Cypher in Polypheny.
Nodes
Nodes are the primary entities in the graph. They are the “things” or objects that data is stored about. For example, in a social network, each person could be represented by a node. Nodes are usually represented in Cypher syntax as follows:
(node)
Relationships
Relationships connect nodes and represent how they are associated with each other. For instance, in a social network, a ‘FRIEND_OF’ relationship might exist between two person nodes. Relationships are always directional and always have a start node and an end node. However, you can ignore the direction if it’s not relevant to your query. In Cypher, a relationship is represented as:
(node1)-[relationship]->(node2)
Properties
Both nodes and relationships can have properties. Properties are named values that add details or attributes to nodes and relationships. For example, a ‘Person’ node might have ‘name’ and ‘age’ properties. A ‘FRIEND_OF’ relationship might have a ‘since’ property indicating when the friendship started. In Cypher, properties are contained within curly brackets {}
:
(person:Person {name: 'John', age: 30})
Labels
Labels are used to categorize nodes into different groups. A node can have zero or more labels. For instance, a ‘Person’ label might be applied to all nodes representing people. Labels can also be used to create indexes and constraints. Labels are prefixed by a colon :
:
(person:Person)
Paths
Paths are sequences of nodes and relationships. Paths can be of any length and are particularly useful when making complex queries. For example, you might want to find a path of ‘FRIEND_OF’ relationships leading from one person to another. In Cypher, a path can be represented as follows:
(startNode)-[relationship*]->(endNode)
In the above, the *
indicates that any number of the ‘relationship’ can exist between the startNode and endNode.
Understanding these five key aspects of the Cypher data model is crucial to working with Cypher queries effectively. They form the building blocks for modeling and querying your data in a graph database in Polypheny using the Cypher language.