Pattern Matching
Pattern Matching
Variable-length paths and complex patterns
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
# Cell 1 — ParametersUSERNAME = "_FILL_ME_IN_" # Set your email before running# Cell 2 — Connectfrom graph_olap import GraphOLAPClientclient = GraphOLAPClient(username=USERNAME)
# Cell 3 — Provisionfrom notebook_setup import provisionpersonas, 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")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 relationshipsresult = conn.query(""" MATCH (a:Customer)-[:SHARES_ACCOUNT]->(b:Customer) RETURN a.id AS from_customer, b.id AS to_customer""")
result.show()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 sectorresult = conn.query(""" MATCH (c:Customer) WHERE c.bk_sectr = 'P' RETURN c.id AS name""")
result.show()# String filtering with CONTAINSresult = conn.query(""" MATCH (c:Customer) WHERE c.id CONTAINS 'XIAO' RETURN c.id AS name""")
result.show()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 XIAOMINGresult = 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()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-friendlyresult = 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 WHEREfilters results by property values or string operations*1..2matches variable-length paths for multi-hop traversal- Always use
parameters={}instead of string interpolation for safety