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
    • Variables
    • Values and Types
    • Comments
    • Reserved Words
    • Syntactic Notation
  • GQL Conformance
  1. Docs
  2. /
  3. ISO GQL
  4. /
  5. Graph Pattern Matching

Quantified Paths

Overview

A quantified path is a variable-length path where the complete path or a part of it is repeated a specified number of times.

Quantified paths are useful when you:

  • Don’t know the exact number of hops required between nodes.
  • Need to capture relationships at varying depths.
  • Want to simplify queries by compressing repetitive pattens.

Quantifiers

A quantifier is written as a postfix to either an edge pattern or a parenthesized path pattern to specify how many times the pattern should repeat.

Quantifier
Description
{m,n}Between m and n repetitions.
{m}Exactly m repetitions.
{m,}m or more repetitions.
{,n}Between 0 and n repetitions.
*Between 0 and more repetitions.
+Between 1 and more repetitions.

Example Graph

CREATE GRAPH myGraph { 
  NODE User ({name string}),
  NODE Device (),
  EDGE Owns ()-[{}]->(),
  EDGE Flows ()-[{packets int32}]->()
} SHARDS [1]

Building Quantified Paths

When writing a quantified path to its full form:

  • Two consecutive node patterns are merged into a single node pattern with their filtering conditions combined using logical AND.
  • Two consecutive edge patterns are implicitly connected by an empty node pattern.

Quantified Edge

Edge patterns can be directly followed by a quantifier, and both the full and abbreviated edge patterns are supported.

Quantified Entire Path

You can enclose the entire path pattern in parentheses () and append a quantifier.

When a quantifier is applied to an entire path pattern, a step count of 0 produces no result.

Quantified Partial Path

You can enclose part of a path pattern in parentheses () and append a quantifier for it.

Another example:

Examples

Lowerbound and Upperbound

GQL
MATCH p = ({name: 'Jack'})->()-[f:Flows WHERE f.packets > 15]->{1,3}()<-({name: 'Mike'})
RETURN p

Result: p

Fixed Length

GQL
MATCH p = ((:Device)->(:Device)){2}
RETURN p

Result: p

Fixed Lowerbound

GQL
MATCH ({_id: 'Comp1'})->{2,}(n)
RETURN COLLECT_LIST(n._id)

Result:

COLLECT_LIST(n._id)
["Comp4","Comp3","Comp4"]
GQL
MATCH p = ({_id: 'Comp1'})-[f:Flows WHERE f.packets > 20]->*()
RETURN p

Result: p

GQL
MATCH p = ({_id: 'Comp1'})-[f:Flows WHERE f.packets > 20]->+()
RETURN p

Result: p

Fixed Upperbound

GQL
MATCH p = ({name: 'Jack'})->(()-[f:Flows WHERE f.packets > 15]->()){,2}<-({name: 'Mike'})
RETURN p

Result: p

Group Variables

Element variables declared within the repeatable part of a quantified path are bound to a list of nodes or edges, known as group variables or group list.

Example Graph

CREATE GRAPH myGraph { 
  NODE User ({name string, age uint32}),
  EDGE Follows ()-[{score uint32}]->()
} SHARDS [1]

Referencing Outside the Quantified Segment

Whenever a group variable is referenced outside the repeatable part of the quantified path where it is declared, it refers to a list of nodes or edges.

In this query, variables a and b represent lists of nodes encountered along the matched paths, rather than individual nodes:

GQL
MATCH p = ((a)-[]->(b)){1,2}
RETURN p, a, b

Result:

pab
[(:User {_id:"U2", name:"Quasar92", age:29})][(:User {_id:"U3", name:"claire", age:35})]
[(:User {_id:"U1", name:"rowlock", age:24})][(:User {_id:"U2", name:"Quasar92", age:29})]
[(:User {_id:"U1", name:"rowlock", age:24}), (:User {_id:"U2", name:"Quasar92", age:29})][(:User {_id:"U2", name:"Quasar92", age:29}), (:User {_id:"U3", name:"claire", age:35})]

To aggregate the group variables, use the FOR statement to expand it into individual records:

GQL
MATCH path = ()-[edges]->{1,2}()
CALL (path, edges) {
  FOR edge IN edges
  RETURN sum(edge.score) AS scores 
}
FILTER scores > 2
RETURN path, scores

Result:

pscores
3
5

The following query throws syntax error since a and b are lists:

GQL - Syntax Error
MATCH p = ((a)-[]->(b)){1,2}
WHERE a.age < b.age
RETURN p

Referencing Inside the Quantified Segment

A group variable has a singleton reference only when it is referenced within the repeatable part of the quantified path where it is declared.

In this query, a and b are treated as singletons that represent individual nodes. The condition a.age < b.age is evaluated for each pair of nodes a and b as the path is matched:

GQL
MATCH p = ((a)-[]->(b) WHERE a.age < b.age){1,2}
RETURN p

Result: p