Skip to content

Pattern Matching

Tutorial

Pattern Matching

Variable-length paths and complex patterns

35 min Intermediate
CypherPatternsPaths

What You'll Learn

  • Simple Patterns - Match node-relationship-node triples
  • WHERE Filters - Narrow results with conditions
  • Variable-Length Paths - Traverse multiple hops
  • Parameterized Queries - Pass values safely
Info:
Prerequisites
Complete 01_property_graphs before this tutorial.
# Cell 1 — Parameters
USERNAME = "_FILL_ME_IN_" # Set your email before running
# Cell 2 — Connect
from graph_olap import GraphOLAPClient
client = GraphOLAPClient(username=USERNAME)
# Cell 3 — Provision
from notebook_setup import provision
personas, conn = provision(USERNAME)
analyst = personas["analyst"]
admin = personas["admin"]
ops = personas["ops"]
client = analyst
print(f"Connected | {conn.query_scalar('MATCH (n) RETURN count(n)')} nodes")
1

Simple Patterns

Matching relationships between nodes

A pattern in Cypher describes the shape you want to find in the graph. The simplest relationship pattern connects two nodes:

(a:Customer)-[:SHARES_ACCOUNT]->(b:Customer)

This matches every directed SHARES_ACCOUNT edge from one customer to another.

# Find all SHARES_ACCOUNT relationships
result = conn.query("""
MATCH (a:Customer)-[:SHARES_ACCOUNT]->(b:Customer)
RETURN a.id AS from_customer, b.id AS to_customer
""")
result.show()
2

WHERE Filters

Narrowing results with conditions

The WHERE clause filters matched patterns by property values, string operations, or boolean logic. Only rows satisfying the condition are returned.

# Filter customers by sector
result = conn.query("""
MATCH (c:Customer)
WHERE c.bk_sectr = 'P'
RETURN c.id AS name
""")
result.show()
# String filtering with CONTAINS
result = conn.query("""
MATCH (c:Customer)
WHERE c.id CONTAINS 'XIAO'
RETURN c.id AS name
""")
result.show()
3

Variable-Length Paths

Traversing multiple hops

The *1..2 syntax on a relationship matches paths of length 1 or 2. This lets you discover indirect connections — customers linked through an intermediate shared account.

(a)-[:SHARES_ACCOUNT*1..2]->(b)

Use DISTINCT to de-duplicate results when multiple paths reach the same node.

# Variable-length path: find customers within 2 hops of MR LAU XIAOMING
result = conn.query("""
MATCH (a:Customer {id: 'MR LAU XIAOMING'})-[:SHARES_ACCOUNT*1..2]->(b:Customer)
RETURN DISTINCT b.id AS reachable
ORDER BY reachable
""")
result.show()
4

Parameterized Queries

Safe value passing

Hard-coding values in a Cypher string is a security risk (Cypher injection) and prevents query plan caching. Use the parameters argument instead:

conn.query("MATCH (c {id: $name}) RETURN c", parameters={"name": value})

The $name placeholder is replaced safely by the engine.

# Parameterized query -- safe and cache-friendly
result = conn.query(
"MATCH (c:Customer {id: $name}) RETURN c.id AS name, c.bk_sectr AS sector, c.acct_stus AS id_type",
parameters={"name": "MR LAU XIAOMING"}
)
result.show()

Key Takeaways

  • Patterns like (a)-[:REL]->(b) describe the graph shape to find
  • WHERE filters results by property values or string operations
  • *1..2 matches variable-length paths for multi-hop traversal
  • Always use parameters={} instead of string interpolation for safety