Chapter 04 · Advanced Pattern Matching: Variable Length, OPTIONAL MATCH, Paths, Quantified Patterns, and Shortest Paths

Path Values, Nodes/Relationships Functions, Path Predicates, Uniqueness, and Cycle Awareness

Make the exact matched path observable, then reason precisely about cycles and which elements may repeat under each match/path mode.

Intermediate120–145 minutesPath values/cycle semantics labNeo4j 2026.07.1 Community · Cypher 25Last reviewed: September 2026

Learning outcomes

When a query returns a path, the path is a value—not just a visualization. AtlasMart can inspect its nodes, relationships, hop length and properties, then enforce path-level predicates. Those tools are also how you prove what “cycle” and “uniqueness” mean in the concrete fixture.

01

Bind a path variable and inspect nodes(), relationships(), length() and relationship properties.

02

Distinguish revisiting a node from reusing the same relationship.

03

Explain Cypher default relationship uniqueness and current explicit DIFFERENT RELATIONSHIPS semantics.

04

Use bounded REPEATABLE ELEMENTS/ACYCLIC examples only with the Cypher-version caveats they require.

05

Write path predicates that test business properties without hiding combinatorial cost.

Chapter 04 continuity contract

Continue Chapters 01–03 with Neo4j Community 2026.07.1, database neo4j, explicit CYPHER 25 in version-sensitive examples, local container atlasmart-neo4j, Bolt 127.0.0.1:7687, HTTP 127.0.0.1:7474, and constraint-backed AtlasMart domain identifiers. Chapter 04 adds a small synthetic operational handoff subgraph on existing Supplier/Category/Store concepts; it is intentionally isolated for path-mechanics exercises and does not replace the transactional relationships modeled earlier.

Version and execution note

Neo4j 2026.07.1 is the current 2026 release used by this course snapshot; 5.26.30 remains the current 5.26 LTS comparison line. The current manual covers Cypher 25; Cypher 5 is frozen. Quantified path patterns/relationships date from Neo4j 5.9, while explicit Cypher 25 path modes such as ACYCLIC arrived later and have version-sensitive combination rules. Commands here were checked against current documentation but could not be executed in this generation environment, so expected output is described by deterministic invariants rather than fabricated captures.

1. A path variable exposes the exact matched sequence

Cypher · idempotent cyclic AtlasMart handoff fixture
CYPHER 25CREATE CONSTRAINT supplier_id IF NOT EXISTS FOR (s:Supplier) REQUIRE s.supplierId IS UNIQUE;CREATE CONSTRAINT category_id IF NOT EXISTS FOR (c:Category) REQUIRE c.categoryId IS UNIQUE;CREATE CONSTRAINT store_id IF NOT EXISTS FOR (s:Store) REQUIRE s.storeId IS UNIQUE;CREATE CONSTRAINT ops_point_id IF NOT EXISTS FOR (n:OpsPoint) REQUIRE n.pointId IS UNIQUE;MERGE (s1:Supplier {supplierId:'SUP-3001'}) SET s1:OpsPoint, s1.pointId='OP-SUP-1', s1.name='Northwind Optics';MERGE (s2:Supplier {supplierId:'SUP-3002'}) SET s2:OpsPoint, s2.pointId='OP-SUP-2', s2.name='Audio Forge';MERGE (c1:Category {categoryId:'CAT-CAMERAS'}) SET c1:OpsPoint, c1.pointId='OP-CAT-CAM', c1.name='Cameras';MERGE (c2:Category {categoryId:'CAT-AUDIO'}) SET c2:OpsPoint, c2.pointId='OP-CAT-AUD', c2.name='Audio';MERGE (st1:Store {storeId:'ST-001'}) SET st1:OpsPoint, st1.pointId='OP-ST-1', st1.name='Central', st1.region='west';MERGE (st2:Store {storeId:'ST-002'}) SET st2:OpsPoint, st2.pointId='OP-ST-2', st2.name='Harbor', st2.region='east';MERGE (st3:Store {storeId:'ST-003'}) SET st3:OpsPoint, st3.pointId='OP-ST-3', st3.name='Airport', st3.region='north';MATCH (s1:OpsPoint {pointId:'OP-SUP-1'}), (s2:OpsPoint {pointId:'OP-SUP-2'}),      (c1:OpsPoint {pointId:'OP-CAT-CAM'}), (c2:OpsPoint {pointId:'OP-CAT-AUD'}),      (st1:OpsPoint {pointId:'OP-ST-1'}), (st2:OpsPoint {pointId:'OP-ST-2'}), (st3:OpsPoint {pointId:'OP-ST-3'})MERGE (s1)-[:HANDOFF_TO {routeId:'R01', minutes:30, active:true}]->(st1)MERGE (s1)-[:HANDOFF_TO {routeId:'R02', minutes:10, active:true}]->(c1)MERGE (c1)-[:HANDOFF_TO {routeId:'R03', minutes:8, active:true}]->(st1)MERGE (c1)-[:HANDOFF_TO {routeId:'R04', minutes:5, active:true}]->(c2)MERGE (st1)-[:HANDOFF_TO {routeId:'R05', minutes:12, active:true}]->(s2)MERGE (st1)-[:HANDOFF_TO {routeId:'R06', minutes:11, active:false}]->(c2)MERGE (s2)-[:HANDOFF_TO {routeId:'R07', minutes:9, active:true}]->(c2)MERGE (s2)-[:HANDOFF_TO {routeId:'R08', minutes:6, active:true}]->(st2)MERGE (c2)-[:HANDOFF_TO {routeId:'R09', minutes:7, active:true}]->(st2)MERGE (st2)-[:HANDOFF_TO {routeId:'R10', minutes:14, active:true}]->(s1)MERGE (st2)-[:HANDOFF_TO {routeId:'R11', minutes:4, active:true}]->(st3)MERGE (st3)-[:HANDOFF_TO {routeId:'R12', minutes:13, active:true}]->(c1);
Cypher · inspect one bounded family of paths
CYPHER 25MATCH p=(:OpsPoint {pointId:'OP-SUP-1'})-[:HANDOFF_TO]->{1,4}(:Store:OpsPoint {storeId:'ST-002'})RETURN length(p) AS hops,       [n IN nodes(p) | n.pointId] AS pointIds,       [r IN relationships(p) | r.routeId] AS routeIds,       reduce(total=0, r IN relationships(p) | total + r.minutes) AS minutesORDER BY hops, minutes, pointIds;

nodes(p) and relationships(p) preserve path order. length(p) counts relationships, not nodes. The reduction computes a path property for inspection; it does not make the pattern selector weighted.

2. Nodes may repeat under default matching; relationships may not

Cypher · make a cycle back to the starting supplier observable
CYPHER 25MATCH p=(s:OpsPoint {pointId:'OP-SUP-1'})-[:HANDOFF_TO]->{2,7}(s)RETURN length(p) AS hops,       [n IN nodes(p) | n.pointId] AS points,       [r IN relationships(p) | r.routeId] AS routesORDER BY hopsLIMIT 10;

The fixture contains a directed cycle back to OP-SUP-1. Seeing the start node again is legal. Under default match semantics, however, the same relationship is not traversed twice within one matched result. Current Cypher 25 can spell that default match mode explicitly as DIFFERENT RELATIONSHIPS.

Cypher 25 · explicit default match mode
CYPHER 25MATCH DIFFERENT RELATIONSHIPS p=(s:OpsPoint {pointId:'OP-SUP-1'})-[:HANDOFF_TO]->{1,6}(t:OpsPoint)RETURN count(p) AS boundedPaths;

3. REPEATABLE ELEMENTS changes the search space

Use only with a finite bound in the course lab.

REPEATABLE ELEMENTS is a Cypher 25 match mode that permits relationships to be revisited. On a cyclic graph, removing a finite bound can make the candidate space operationally dangerous.

Cypher 25 · controlled comparison
CYPHER 25MATCH REPEATABLE ELEMENTS p=(s:OpsPoint {pointId:'OP-SUP-1'})-[:HANDOFF_TO]->{1,6}(t:OpsPoint)RETURN count(p) AS repeatableBoundedPaths;

This is a semantic tool, not a performance trick. Use it only when repeated elements are part of the domain question, such as modeling walks rather than trails.

4. ACYCLIC is a current path mode, not timeless Cypher syntax

Cypher 25 added explicit path modes in the 2026 line. ACYCLIC prevents a node from repeating within the path. Its availability and combination with restrictive selectors changed across 2026 patches, so version-pin any lesson or production query that uses it.

Cypher 25 / Neo4j 2026.07.1 · acyclic bounded path
CYPHER 25MATCH p=ACYCLIC (:OpsPoint {pointId:'OP-SUP-1'})-[:HANDOFF_TO]->{1,6}(t:Store:OpsPoint)RETURN t.storeId, length(p) AS hops,       [n IN nodes(p) | n.pointId] AS pointsORDER BY hops, t.storeId;

Do not backport this syntax mentally to Cypher 5. The stable concept is the requirement “no repeated nodes”; the exact syntax is release-sensitive.

5. Path predicates express domain rules, but they do not erase path enumeration

Cypher · accept only active paths with a latency budget
CYPHER 25MATCH p=(:OpsPoint {pointId:$source})-[:HANDOFF_TO]->{1,5}(target:Store:OpsPoint)WHERE all(r IN relationships(p) WHERE r.active=true)  AND reduce(total=0, r IN relationships(p) | total+r.minutes) <= $budgetMinutesRETURN target.storeId,       length(p) AS hops,       [r IN relationships(p) | r.routeId] AS routesORDER BY hops, target.storeId;

For large search spaces, prefer predicates that can prune during the quantified pattern rather than only a final path-level predicate. Production judgment means separating semantic correctness from search strategy. The next lesson uses that distinction to show why hop-shortest and cost-shortest are different questions.

Check your understanding

  1. What does length(p) count?
  2. Can a node repeat in a default matched path?
  3. Can the same relationship repeat under the default match behavior?
  4. What does REPEATABLE ELEMENTS change?
  5. Why is ACYCLIC treated as version-sensitive in this course?
Review the answers

1. Relationships/hops, not nodes.

2. Yes, a node may be revisited.

3. No, not within a default matched result.

4. It allows elements, including relationships, to be revisited, so the path search space changes materially.

5. It is a newer Cypher 25 path mode whose availability/combination rules evolved across 2026 releases.

Summary and next step

Path Values, Nodes/Relationships Functions, Path Predicates, Uniqueness, and Cycle Awareness is useful only when its assumptions and observed evidence stay attached to the decision. The examples above establish a reproducible mechanism and boundary; they do not turn one lab result into a universal production rule.

Next, continue to Shortest-Path Queries, Weighted vs Unweighted Reasoning, and When to Use Graph Data Science Instead. Carry forward the verified assumptions, fixture state, version/edition boundaries, and measurements from this lesson instead of treating the next topic as an isolated recipe.

Authoritative references

Keep knowledge open

Help the academy stay free and grow.

If these tutorials save you time, a small donation supports new lessons, technical review, diagrams, examples, and long-term maintenance.

ETHEthereum / ERC-20 only
0x716c4Ab160C4B66F31a28AE2448BfF68fc3a2ef0

Send only Ethereum or ERC-20 compatible assets to this address.