Patterns
Graph pattern matching sits at the very core of Cypher®.
It is the mechanism used to navigate, describe and extract data from a graph by applying a declarative pattern.
Inside a MATCH clause, you specify graph patterns to search for, and return the paths that match them.
Graph pattern matching can also be used without a MATCH clause, in the subqueries EXISTS, COUNT, and COLLECT.
|
A graph pattern describes data using a syntax that is similar to how the nodes and relationships of a property graph are drawn on a whiteboard. On a whiteboard, nodes are drawn as circles and relationships are drawn as arrows. Cypher represents the circles as a pair of parentheses, and the arrows as dashes and greater-than or less-than symbols:
()-->()<--()
These simple patterns for nodes and relationships form the building blocks of path patterns that can match paths of a fixed length. As well as discussing simple fixed-length patterns, this chapter covers more complex patterns, showing how to match patterns of a variable or unknown length, find the shortest paths between a given set of nodes, add inline filters for improved query performance, and add cycles and non-linear shapes to path patterns.
This chapter includes the following sections:
-
Primer - a short primer on how to get started with using graph pattern matching in Cypher.
-
Fixed-length paths - information about node, relationship, and path patterns.
-
Variable-length paths - information about quantified path patterns, quantified relationships, and group variables.
-
Non-linear paths - information about equijoins and graph patterns (combined path patterns).
-
Shortest paths - information about finding the
SHORTESTpath patterns. -
Paths with unique relationships - information about Cypher’s default behavior of not re-traversing relationships in graph pattern matches.
The model data in the examples used in this chapter are based on the UK national rail network, using publicly available datasets.