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.
It also discusses Cypher’s default behavior of not allowing relationships to be traversed in a match pattern, and how to alter this default, either by allowing the re-traversal of relationships using REPEATABLE ELEMENTS or by disallowing nodes to be repeated in a path with the ACYCLIC keyword.
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 and the
DIFFERENT RELATIONSHIPSkeyword. -
Paths with repeated nodes and relationships: information about how to enable re-traversal of relationships in graph pattern matches using
REPEATABLE ELEMENTS. Introduced in Neo4j 2025.06 Cypher 25 only -
Acyclic paths: information about
ACYCLICpaths. Introduced in Neo4j 2026.03 Cypher 25 only -
Syntax and semantics - a reference for looking up the syntax and semantics of graph pattern matching.
The model data in the examples used in this chapter are based on the UK national rail network, using publicly available datasets.