# Neo4j Features and Practical Usage
🕸️ "If this server goes down, what chain of things stops with it?" Cypher's variable-length patterns answer that by traversing relationships across any number of hops.
🏷️ Title: Variable-length patterns (`*1..5`) / SHORTEST / Quantified Path Patterns
🔗 URL:
📘 Overview
Variable-length patterns let you express paths whose hop count is not fixed, all in a single declarative pattern. Questions like "trace suppliers up to 5 levels deep" or "find one shortest path" become natural one-liners. Shortest-path finding and Quantified Path Patterns (QPP) belong to the same family.
⚙️ How It Works
・Quantified relationships (legacy syntax): `[:REL*1..5]` matches 1 to 5 repetitions. `*2` is exactly 2, `*3..` is 3 or more, `*..10` is up to 10, `+` is one or more, and `*` is zero or more.
・Quantified Path Patterns (QPP): wrap a whole pattern in parentheses and quantify it, e.g. `((a)-[r:NEXT]->(b)){1,3}`. It is GQL-conformant and supports inline `WHERE` predicates inside the repetition.
・Group variables: variables declared inside a QPP become lists when referenced outside (e.g. `r` becomes an array of relationships). Combine with `reduce()` to compute, say, total distance along a path.
・Shortest paths: `SHORTEST k` returns k shortest paths, `ALL SHORTEST` returns every tied-shortest path, and `ANY` returns any one. The legacy `shortestPath()` / `allShortestPaths()` functions still work but the keyword syntax is faster.
🛠️ Practical Usage
Trace a part's suppliers up to 5 levels deep:
```cypher
MATCH (p:Part {sku: $sku})-[:SUPPLIED_BY*1..5]->(supplier:Company)
RETURN DISTINCT
```
Find the single shortest introduction chain between two people:
```cypher
MATCH path = SHORTEST 1 (a:Person {id:$a})-[:KNOWS]-+(b:Person {id:$b})
RETURN [n IN nodes(path) | AS intro_chain
```
💡 Use Cases
・Impact analysis: how far a server failure cascades.
・Money-laundering detection: tracing fund-movement paths between accounts.
・Org charts and social graphs: boss-of-boss lookups, friend-of-friend shortest reach.
⚠️ Caveats
・Unbounded `*` or `[:REL*]` causes path explosion and can stall on millions of matches. Always set an upper bound such as `*1..5`.
・Pruning early with labels, relationship types, and inline predicates makes queries dramatically faster. QPP is built for this kind of pruning.
・By default Cypher does not allow re-traversing the same relationship; be aware that this behavior can be modified.
#
Neo4j# #
Cypher#