Pages

Sunday, August 14, 2016

Relationship Direction in Cypher is Important

The relationship direction between two nodes is required for Cypher write queries, but can be ignored on Cypher read queries. Cypher is a graph database declarative and textual query language by Neo4j, which supported by a larger group as OpenCypher to make Cypher the SQL for graph databases. Cypher looks a bit like ASCII art in it’s representation of graph-related traversal patterns, which makes it quite intuitive and fun to use in querying graphs.

Direction can be specified in Cypher via the use of ‘<' and '> as part of a relationship pattern. Here’s an example:
1
(jack)-[:KNOWS]->(jill)
1
(jack)<-[:KNOWS]-(jill)

Relationship Direction for Writes and Reads

When a relationship is being made in Neo4j 2.3.x utilizing the Cypher query language, it requires that the direction of the relationship to be specified. This means that whenever you’re creating data within a graph via a MERGE or CREATE statement, you must always specify a direction for such relationship because Neo4j always stores a relationship with a direction. It is also beneficial because it ensures a clean and consistent data layout in the graph for your read queries.
Querying data in the graph may seem flexible in that Cypher doesn’t require specifying a direction, but it should only be done through careful consideration to the density of OUTGOING and INCOMING relationships. The important part here is that when you’re not specifying a direction for the relationship you’re matching with the 

No comments:

Post a Comment