UltipaDocs
Try Playground
  • Introduction
    • Overview
    • Node and Edge Patterns
    • Path Patterns
    • Quantified Paths
    • Shortest Paths
    • Graph Patterns
    • Overview
    • Typed 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
    • Table Functions
  • Operators
  • Predicates
  • Expressions
    • Index
    • Full-text Index
    • Vector Index
    • Managing HDC Graphs
    • HDC Graph Queries
  • Transaction
  • Trigger
    • Process
    • Job
    • Execution Plan
    • Variables
    • Values and Types
    • Comments
    • Reserved Words
    • Syntactic Notation
  • Access Control
  • GQL Conformance
  1. Docs
  2. /
  3. ISO GQL
  4. /
  5. Graph Pattern Matching

Graph Patterns

Overview

A graph pattern is composed of three parts:

  • Match Mode (Optional)
  • Path Pattern List
  • WHERE Clause (Optional)
Syntax
<graph pattern> ::= 
  [ <match mode> ] <path pattern list> [ <where clause> ]

<path pattern list> ::= 
  <path pattern> [ { "," <path pattern> }... ]

The path pattern list consists of one or multiple path patterns. Each path pattern is independently matched against the graph, producing a result set. These sets are either equi-joined on the common variables (if any exist) or combined using the Cartesian product.

Example Graph

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

Connected Paths

To get common followers of Brainy and mochaeach who live in New York:

GQL
MATCH ({name: 'Brainy'})<-[:Follows]-(u:User)-[:Follows]->({name: 'mochaeach'}), 
      (u)-[:LivesIn]->({name: 'New York'})
RETURN u.name

The two path patterns in MATCH declare a common variable u. An equi-join is performed, joining rows where the values of u are equal and discarding the rest.

Result:

u.name
rowlock
purplechalk

Disconnected Paths

Consider this query:

GQL
MATCH (u1:User)-[:Follows]->({name: 'Brainy'}),
      (u2:User)-[:LivesIn]->({name: 'New York'})
RETURN u1.name, u2.name

The two path patterns in MATCH declare no common variables, resulting in a Cartesian product. Each row from the first result set is combined with every row from the second.

Result:

u1.nameu2.name
rowlockrowlock
rowlockpurplechalk
purplechalkrowlock
purplechalkpurplechalk
QuickFoxrowlock
QuickFoxpurplechalk

Please note that Cartesian products often lead to unintended results and can significantly increase query overhead when dealing with large datasets. Therefore, unless explicitly required, it's best to avoid Cartesian products when writing queries.

Match Mode

A graph pattern can specify a match mode that applies to all contained path patterns. There are two match modes:

Match Mode
Description
DIFFERENT EDGESThe default. Repeated edges are not permitted in a record. There are no restrictions on nodes.
REPEATABLE ELEMENTSIt is non-restrictive.
NOTE

The REPEATABLE ELEMENTS is not supported yet.

Example Graph

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

DIFFERENT EDGES

This query finds nodes connected to QuickFox, and also have other different connections:

GQL
MATCH DIFFERENT EDGES ({name: "QuickFox"})-[e1]-(n), (n)-[e2]-(m)
RETURN collect_list(n._id)

Result:

n._id
["U03","U03","C01"]

REPEATABLE ELEMENTS

Compare the result of the query with the REPEATABLE ELEMENTS match mode:

GQL
MATCH REPEATABLE ELEMENTS ({name: "QuickFox"})-[e1]-(n), (n)-[e2]-(m)
RETURN collect_list(n._id)

Result:

n._id
["U02","U03","U03","U03","C01","C01"]

Ensuring Unique Edge Bindings in DIFFERENT EDGES

Only records where edges are uniquely bound to different variables are retained in the DIFFERENT EDGES match mode. Therefore, if an edge variable is reused in a graph pattern, no results will be returned. See the following examples:

GQL
MATCH DIFFERENT EDGES ()-[e]->(), ()-[e]->()
RETURN e
GQL
MATCH DIFFERENT EDGES ()-[e]->()<-[e]-()
RETURN e