Database Reference
In-Depth Information
The traversal has three phases:
1
. Find a starting node by ID.
2
. Traverse the graph from the starting node by following the
HAS_SEEN
relation-
ships.
3
. Return the nodes that are at the end of the
HAS_SEEN
relationships.
Now let's see how you could write the same query using Cypher, following the same three
phases of traversal:
JustasintheJavacode,youfirstneedtofindthestartingnodebyusingthenodeID,which
can be achieved in Cypher using the
start
and
node
keywords . Note the name of
the starting node—
user
—so that you can refer to it later in the same Cypher query.
Next, you set the pattern you need your result to match. The pattern is a description of all
subgraphs you want to inspect within your data, and it consists of a number of nodes con-
nected with relationships. Typical graph patterns consist of two nodes connected by a rela-
tionship, which is represented in the format ()-[]-(). Nodes are specified using parentheses,
()
, and relationships are specified using square brackets,
[]
. Nodes and relationships are
linked using hyphens. The node in the
start
clause (
user
in the preceding example) is
usedtofixtheleftsideofthepattern.Thewholepatternhastwoknownelements (left-side
node and the relationship), which you use to find all nodes that match the right side (nodes
named
movie
in our example).
Finally, you're interested in all movies that match, so you use the
return
keyword to re-
turnthem(again,referencingthenodebythenameintroducedbefore).Asemicolonisused
to mark the end of the Cypher query.
When you run this Cypher query, you'll see exactly the same results as you did with the
corresponding Java API solution, but with much simpler query syntax. Cypher is much