UltipaDocs
Try Playground
  • Introduction
  • GQL vs Other Languages
    • Overview
    • Node and Edge Patterns
    • Path Patterns
    • Quantified Paths
    • Shortest Paths
    • Graph Patterns
    • Overview
    • Closed Graph
    • Open Graph
    • Graph Sharding and Storage
    • Constraints
    • Unique Identifiers
    • INSERT
    • INSERT OVERWRITE
    • UPSERT
    • SET
    • REMOVE
    • DELETE
    • Query Composition
    • Result Table and Visualization
    • MATCH
    • OPTIONAL MATCH
    • FILTER
    • LET
    • FOR
    • ORDER BY
    • LIMIT
    • SKIP
    • CALL
    • RETURN
    • Composite Query
    • NEXT
    • All Functions
    • Scalar Functions
    • Path Functions
    • Aggregate Functions
    • Mathematical Functions
    • Trigonometric Functions
    • String Functions
    • List Functions
    • Datetime Functions
    • Spatial Functions
    • Type Conversion Functions
    • Table Functions
    • AI & Vector Functions
    • Database Functions
  • Operators
  • Predicates
  • Expressions
    • Index
    • Full-text Index
    • Vector Index
  • Transactions
  • Triggers
  • Query Management
  • Backup and Restore
    • Variables
    • Values and Types
    • Comments
    • Reserved Words
    • Syntactic Notation
  • GQL Conformance
  1. Docs
  2. /
  3. ISO GQL
  4. /
  5. Graph Pattern Matching

Path Patterns

Overview

A path pattern is to match paths in the graph. It is composed of three parts:

  • Path Variable Declaration (Optional)
  • Path Pattern Prefix (Optional)
  • Path Pattern Expression
Syntax
<path pattern> ::=
  [ <path variable declaration> ]
  [ <path pattern prefix> ]
  <path pattern expression>

Path Pattern Expression

A path pattern expression (or path pattern for short) defines the nodes and edges that make up a path. Essentially, it is a sequence of node and edge patterns concatenated according to the topological rules of a path - it must start and end with a node and alternate between nodes and edges.

Basic Paths

This path pattern starts from the node Brainy, connecting to a Club node through an outgoing Joins edge:

Path Pattern
(:User {name: 'Brainy'})-[:Joins]->(:Club)

You can keep on chaining edge and node patterns to create more complex path patterns. This path pattern describes that Brainy and mochaeach join the same club, with the Club node bound to the variable c:

Path Pattern
(:User {name: 'Brainy'})-[:Joins]->(c:Club)<-[:Joins]-(:User {name: 'mochaeach'})

This path pattern reuses the variable a to form a ring-like structure that starts and ends with the same Account node:

Path Pattern
(a:Account)-[:Owns]->(:Card)-[:Transfers]->(:Card)-[:Transfers]->(:Card)<-[:Owns]-(a)

Advanced Paths

GQL supports the following advanced path patterns:

  • Quantified Paths
  • Shortest Paths

Path Variable Declaration

A path variable is declared at the start of a path pattern with =, representing a path binding.

The variable p is bound to paths connecting any two nodes through an outgoing Follows edge:

GQL
MATCH p = ()-[:Follows]->()
RETURN p

Path Pattern Prefix

There are two types of path pattern prefixes:

  • Path Mode
  • Path Selector

Example Graph

CREATE GRAPH myGraph SHARDS [1]

Path Mode

Path modes control how paths are traversed and whether nodes or edges can be revisited. A path mode may be placed either at the head of a path pattern expression.

Path Mode
Description
TRAILThe default. Paths may repeat nodes but not edges.
ACYCLICNo repeated nodes are allowed in the paths.
SIMPLENo repeated nodes allowed in the paths unless it forms a cycle by starting and ending at the same node.
WALKNon-restrictive.

The following queries find 1- to 3-step outgoing paths from C1 using different path modes:

MATCH p = WALK ({_id: 'C1'})->{1,3}()
RETURN p

Result: p

Path Selector

A path selector is used to select a limited number of paths from each partition of the match results. When a path pattern matches multiple start and end nodes, the results are conceptually partitioned into distinct pairs of start node and end node. The path selection is performed within each partition, and the result is the union of all paths found for each partition.

Path SelectorDescription
ALLThe default. Non-selective.
ANYSelects any one path from each partition.
ANY kSelects any k (non-negative integer) paths from each partition. If a partition has fewer than k paths, all are retained.
ALL SHORTESTSee Shortest Paths.
ANY SHORTEST
SHORTEST k
SHORTEST k GROUP

The following queries find 1- to 3-step paths between C1 and target nodes C3 and C4, selecting ALL or ANY paths from each partition:

MATCH p = ({_id: 'C1'})-{1,3}(target WHERE target._id IN ['C3', 'C4'])
RETURN p

Result: p

Combined Usage

When a query includes both a path selector and a path mode, the path selector should precede the path mode.

To find any 1- to 3-step outgoing WALK paths from C1 (from each partition):

GQL
MATCH p = ANY WALK ({_id: 'C1'})->{1,3}()
RETURN p

Special Considerations

Edge Pattern Juxtaposition

Two consecutive edge patterns conceptually have an empty node pattern between them. For example,

Path Term
(:User)-[]->-[]->(u)

This path term implicitly extends to:

Path Term
(:User)-[]->()-[]->(u)
NOTE

A path term should not juxtapose a token that exposes a minus sign on the right (]-, <-, -) followed by a token that exposes a minus sign on the left (-[, ->, -), as this combination introduces the comment symbol --. See Comments.

Node Pattern Juxtaposition

Node pattern juxtaposition is only supported for quantified paths.