Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
To fully utilize the power of a graph database, we also need to express the relationships between our nodes.
Relationships are represented in Cypher using an arrow -->
or <--
between two nodes.
Notice how the syntax looks like the arrows and lines connecting our nodes in the visual representation.
Additional information, such as how nodes are connected (relationship type) and any properties pertaining to the relationship, can be placed in square brackets inside of the arrow.
In our example, the lines with LIKES
, IS_FRIENDS_WITH
, and WORKS_FOR
between nodes are our relationships.
Undirected relationships are represented with no arrow and just two dashes --
.
This means that the relationship can be traversed in either direction.
While a direction must be inserted to the database, it can be matched with an undirected relationship where Cypher ignores any particular direction and retrieves the relationship and connected nodes, no matter what the physical direction is.
This allows the queries to be flexible and not force the user to know the physical direction of the relationship stored in the database.
Relationship types categorize and add meaning to a relationship, similar to how labels group nodes. In our property graph data model, relationships show how nodes are connected and related to each other. You can usually identify relationships in your data model by looking for actions or verbs.
You can specify any type of relationship you want between nodes, but we recommend good naming conventions using verbs and actions. Poor relationship type names make it more difficult to both read and write Cypher (remember, it should sound like English!).
For example, let us look at the relationship types from our example graph.
[:LIKES]
- makes sense when we put nodes on either side of the relationship (Jennifer LIKES Graphs)
[:IS_FRIENDS_WITH]
- makes sense when we put nodes with it (Jennifer IS_FRIENDS_WITH Michael)
[:WORKS_FOR]
- makes sense with nodes (Jennifer WORKS_FOR Neo4j)
Just as we did with nodes, if we want to refer to a relationship later in a query, we can give it a variable like [r]
or [rel]
.
We can also use longer, more expressive variable names like [likes]
or [knows]
.
If you do not need to reference the relationship later, you can specify an anonymous relationship using two dashes --+, +-->+, +<--
.
As an example, you could use either -[rel]->
or -[rel:LIKES]->
and call the rel
variable later in your query to reference the relationship and its details.