shortestPath
Generic Description
Path-pattern helper that asks for one shortest path.
Simple example:
MATCH p = shortestPath((a:Page)-[:LINK*]->(b:Page)) RETURN length(p) AS hop_count
Consumer-Level Explanation
Use shortestPath when the query must path-pattern helper that asks for one shortest path and the planner needs to see that operation as part of the Cypher row pipeline. Keep the clause explicit because it controls row grain, variable scope, and what later clauses are allowed to reference.
More Detailed Explanation
shortestPath is a pattern-level tool, not a general scalar function. It is for graph queries where route shape matters but you want the concise declarative form rather than a procedure call.
What this clause is really for:
- it defines one concrete stage in the Cypher row pipeline, so variables available before and after
shortestPathmust be clear - it should make graph structure, temporal filters, document payload shaping, or procedure output explicit instead of relying on client-side interpretation
- planner tooling depends on this clause boundary to know row grain, variable scope, and whether later expressions are reads, writes, schema operations, or projections
Advanced Example
This example keeps shortestPath inside a complete query pipeline so the clause boundary, visible variables, and returned row shape are clear to planner tooling.
MATCH p = shortestPath((a:Page)-[:LINK*]->(b:Page))
RETURN length(p) AS hop_count, size(nodes(p)) AS node_count, size(relationships(p)) AS relationship_count
Real Use Cases
- single best-route discovery in graph UIs
- dependency trace minimization
- link-analysis path inspection
Real Limitations And Tradeoffs
- meaning depends on the available path pattern and graph shape
- one shortest path is not the same as all meaningful alternatives
- debugging large path searches still benefits from careful graph scoping