Match modes

Match modes determine whether relationships can appear more than once in a graph pattern match (nodes can appear multiple times regardless of the match mode). Cypher® supports two match modes:

  • DIFFERENT RELATIONSHIPS: a relationship can only be traversed once in a given match for a graph pattern. The same restriction does not hold for nodes, which may be re-traversed any number of times in a matched path. This is Cypher’s default match mode, applicable to most use cases.

  • REPEATABLE ELEMENTS: no restrictions on how often nodes and relationships can be traversed in a match for a graph pattern. This match mode is required in order for paths to be able to traverse a relationship more than once. Introduced in Neo4j 2025.06

Example graph

To explain match modes, this page will use the scenario of the Seven bridges of Königsberg, a problem studied by Leonhard Euler in 1736 to determine if one could cross all seven bridges of Königsberg exactly once in a single walk.

To recreate the graph, run the following query against an empty Neo4j database:

CREATE (kneiphof:Location {name: "Kneiphof"}),
       (northBank:Location {name: "North Bank"}),
       (southBank:Location {name: "South Bank"}),
       (lomse:Location {name: "Lomse"}),

       (kneiphof)-[:BRIDGE {id: 1}]->(northBank),
       (kneiphof)-[:BRIDGE {id: 6}]->(southBank),
       (kneiphof)-[:BRIDGE {id: 7}]->(lomse),
       (northBank)-[:BRIDGE {id: 5}]->(kneiphof),
       (northBank)-[:BRIDGE {id: 2}]->(lomse),
       (southBank)-[:BRIDGE {id: 4}]->(kneiphof),
       (southBank)-[:BRIDGE {id: 3}]->(lomse)

DIFFERENT RELATIONSHIPS

The DIFFERENT RELATIONSHIPS match mode ensures that a relationship can only occur once for a given MATCH result, regardless of the direction it is traversed in. The same restriction does not apply to nodes, which may be re-traversed any number of times in a match. As such, DIFFERENT RELATIONSHIPS is ideal to showcase the Königsberg bridges problem: in the graph model, not traversing a relationship means not traversing a bridge, the problem’s key constraint.

DIFFERENT RELATIONSHIPS is Cypher’s default match mode. That is, if no match mode is specified, Cypher will default to solving graph patterns according to the restrictions imposed by DIFFERENT RELATIONSHIPS.

The DIFFERENT RELATIONSHIPS keyword was released in Neo4j 2025.06 and can be used to explicitly specify Cypher’s default match mode after MATCH. For example, MATCH DIFFERENT RELATIONSHIPS p = (start)--{,2}(end). This is functionally equivalent to not specifying a match mode.

To show the behavior of DIFFERENT RELATIONSHIPS, consider the following query, which uses a quantified relationship to search for paths exactly 5 hops away from the start Location node, Kneiphof. The relationships used in this query are directional, meaning paths are traversed following specific directions for each bridge (-[:BRIDGE]->).

Find paths with a length of 5 relationships from a start node
MATCH p = (:Location {name: 'Kneiphof'})-[:BRIDGE]->{5}()
RETURN [n IN nodes(p) | n.name] AS locations,
       [r IN relationships(p) | r.id] AS crossedBridges (1)
1 The list comprehensions iterate over nodes and relationships in a path and return specific properties (the name property from nodes, and the id property from relationships).

As the results show, a node may be traversed several times in the same path, but a relationship (bridge) is never re-traversed (for example, the path [6, 4, 1, 5, 6], which traverses the same bridge more than once, cannot be matched using the DIFFERENT RELATIONSHIPS match mode).

Result
locations crossedBridges

["Kneiphof", "North Bank", "Kneiphof", "South Bank", "Kneiphof", "Lomse"]

[1, 5, 6, 4, 7]

["Kneiphof", "South Bank", "Kneiphof", "North Bank", "Kneiphof", "Lomse"]

[6, 4, 1, 5, 7]

Rows: 2

In the next example, the direction is removed from the pattern, and the path length is increased to 6. It shows that there are 48 valid paths with this length, where no relationships (bridges) are re-traversed. The query returns one of those paths as a samplePath, showing both the nodes and relationships traversed therein.

Find paths with a length of 6 relationships from a start node
MATCH p = (:Location {name: 'Kneiphof'})-[:BRIDGE]-{6}()
WITH count(p) AS pathCount, collect(p)[0] AS samplePath (1)
ORDER BY pathCount
RETURN pathCount,
       [n IN nodes(samplePath) | n.name] AS samplePathLocations,
       [r IN relationships(samplePath) | r.id] AS samplePathBridges
1 The collect() function collects all paths and [0] takes the first entry as the samplePath.
Result
pathCount samplePathLocations samplePathBridges

48

["Kneiphof", "North Bank", "Kneiphof", "South Bank", "Kneiphof", "Lomse", "North Bank"]

[1, 5, 6, 4, 7, 2]

Rows: 1

In the samplePath with a length of 6 returned by DIFFERENT RELATIONSHIPS, all bridges are traversed except for bridge 3.

However, if the relationship count is increased to 7, 0 paths are returned.

Find paths with a length of 7 relationships
MATCH p = (:Location {name: 'Kneiphof'})--{7}()
RETURN count(p) AS pathCount
Result
pathCount

0

Rows: 1

In the DIFFERENT RELATIONSHIPS match mode, each step must use a unique bridge. After the first step, one bridge is used, leaving six remaining bridges to be used in the subsequent steps. After the sixth bridge is traversed, a seventh step would require re-traversing a bridge, which is not allowed.

This reflects the conclusion of Euler’s Seven bridges of Königsberg problem: the impossibility of crossing each bridge exactly once in a single walk.

For more information about this match mode, see Syntax & semantics → DIFFERENT RELATIONSHIPS.

REPEATABLE ELEMENTS

The REPEATABLE ELEMENTS match mode ensures that there are no restrictions on how many times a node or relationship can occur for a given MATCH result. In so doing, the REPEATABLE ELEMENTS match mode does not solve the impossibility of crossing every bridge in Königsberg exactly once in a single walk. However, the ability to re-traverse relationships does allow Cypher to return paths when testing Euler’s hypothesis.

Queries utilizing this match mode must specify the REPEATABLE ELEMENTS keyword after MATCH.

The following query matches the graph for paths with a length of 7 relationships using REPEATABLE ELEMENTS and returns a sample path.

Find a path with a length of 7 relationships using REPEATABLE ELEMENTS
MATCH REPEATABLE ELEMENTS p = (:Location {name: 'Kneiphof'})-[:BRIDGE]-{7}()
WITH collect(p)[0] AS samplePath
RETURN [n IN nodes(samplePath) | n.name] AS samplePathLocations,
       [r IN relationships(samplePath) | r.id] AS samplePathBridges
Result
samplePathLocations samplePathBridges

["Kneiphof", "North Bank", "Kneiphof", "North Bank", "Kneiphof", "North Bank", "Kneiphof", "North Bank"]

[1, 5, 1, 5, 1, 5, 1]

Rows: 1

While no paths with a length of 7 can be returned using DIFFERENT RELATIONSHIPS, the query now returns a samplePath that shows the same two bridges (1 and 5) being repeatedly re-traversed until the maximum path length is reached.

The next example matches paths starting and ending at the same Location (Kneiphof), where each bridge is crossed at least once, using the REPEATABLE ELEMENTS match mode:

Find a path traversing all bridges at least once before returning to the starting Location using REPEATABLE ELEMENTS
MATCH REPEATABLE ELEMENTS p = (start:Location {name: 'Kneiphof'})-[:BRIDGE]-{,9}(start) (1)
WHERE all(bridge IN range(1,7) WHERE bridge IN [r IN relationships(p) | r.id]) (2)
RETURN [n IN nodes(p) | n.name] AS visitedLocations,
       [r IN relationships(p) | r.id] AS crossedBridges
ORDER BY length(p), crossedBridges
LIMIT 1
1 Paths with a length of less than 9 relationships return no results because they do not allow enough moves to traverse all seven bridges at least once while forming a cycle back to Kneiphof.
2 The all() function ensures that each bridge must be traversed once in the path.
Result
visitedLocations crossedBridges

["Kneiphof", "North Bank", "Kneiphof", "South Bank", "Lomse", "North Bank", "Kneiphof", "South Bank", "Lomse", "Kneiphof"]

[1, 1, 4, 3, 2, 5, 6, 3, 7]

Rows: 1

The order of the bridges traversed in the path returned demonstrates that bridges 1 and 3 were crossed twice in order to return to Kneiphof.

Sequence of bridge traversals when using REPEATABLE ELEMENTS.

For more information about this match mode, see Syntax and semantics → REPEATABLE ELEMENTS.

Bounded path length

When using DIFFERENT RELATIONSHIPS, the number of returned paths is limited because relationships cannot be re-traversed. However, because REPEATABLE ELEMENTS allows for re-traversing relationships, it enables full cycle re-traversals and paths that can double back on relationships. As a result, REPEATABLE ELEMENTS can generate a very large, or even infinite, number of valid paths.

The below query shows that there are 10583 valid paths with a length of 7 relationships starting from Kneiphof using REPEATABLE ELEMENTS:

Count the paths traversing all 7 bridges at least once using REPEATABLE ELEMENTS
MATCH REPEATABLE ELEMENTS p = (:Location {name: 'Kneiphof'})-[:BRIDGE]-{7}()
RETURN count(p) AS pathCount
Result
pathCount

10583

Rows: 1

If the path length is increased to exactly 10, a total of 490047 paths are returned, 11 hops returns 1818159 paths, 12 hops returns 6498397 paths, and so on, without limit.

To ensure that a MATCH will return a finite number of solutions in a finite amount of time, unbounded quantifiers that do not impose an upper bound on a pattern, such as *, +, or {1,} are not allowed in combination with REPEATABLE ELEMENTS. Also, users should take into account that the number of results returned for a high number of repetitions can significantly impact both memory usage and the speed of the query.

Not allowed: find paths of an unbounded length using REPEATABLE ELEMENTS
MATCH REPEATABLE ELEMENTS p = (start:Location {name: 'Kneiphof'})-[:BRIDGE]-+(start)
WHERE all(bridge IN range(1,7) WHERE bridge IN [r IN relationships(p) | r.id])
RETURN count(p) AS pathCount
Error message
The quantified path pattern may yield an infinite number of rows under match mode 'REPEATABLE ELEMENTS'. Add an upper bound to the quantified path pattern.