Pattern-Matching Queries for AI: Building Context

Pattern-Matching Queries for AI: Building Context

Shift your Cypher focus from data reporting to context assembly. Learn how to write queries that return rich, human-readable subgraphs specifically designed for LLM consumption.

Pattern-Matching Queries for AI: Building Context

A data analyst uses Cypher to generate Reports (e.g., "What is the average revenue?"). A Graph RAG engineer uses Cypher to generate Context (e.g., "Tell me the story of how this project evolved"). These are two different missions.

In this lesson, we will focus on AI-Centric Pattern Matching. We will learn how to write queries that don't just return IDs, but return "Narrative Subgraphs." We will explore Variable-Length Paths, Shortest Path queries, and the importance of Node and Edge Metadata in the final result set.


1. The Context-First Return Statement

In a traditional query, you return id, name, budget. In an AI query, you return Relationships as Sentences.

The Pattern: MATCH (p:Person)-[r:LEADS]->(proj:Project) RETURN p.name + ' leads project ' + proj.title + ' which has a status of ' + proj.status as fact

The Result: "Sudeep leads project Titan which has a status of Green."

By performing this "String Assembly" inside the database, you save the LLM from having to "Figure out" what the relationship means. You provide it with a ready-to-consume Wall of Facts.


2. Exploring the "Cloud of Influence" (Optional Hops)

A user might ask about a project, but the "Why" behind the project might be 2 hops away.

Query Strategy: Use OPTIONAL MATCH. MATCH (p:Project {id: 'Titan'}) OPTIONAL MATCH (p)-[:DEPENDS_ON]->(dep:Project) OPTIONAL MATCH (p)<-[:FUNDED_BY]-(dept:Department) RETURN p, collect(dep.name) as dependencies, dept.name as funder

This query ensures that even if there are no dependencies, you still get the project info. It provides a Variable Context Window.


3. Shortest Path: The "Bridge" Context

This is the most "Intelligent" query an agent can run.

  • Query: "Find the shortest connection between Sudeep and the Tokyo server."
  • Cypher: MATCH p = shortestPath((s:Person {name: 'Sudeep'})-[*..6]-(srv:Server {location: 'Tokyo'})) RETURN p

The AI Value: The LLM receives a sequence of nodes. It can then explain to the user: "You are related to that server because you wrote the code (Node 2) for the VPN (Node 3) that that server uses (Node 4)."

graph LR
    A[Sudeep] --> B[Code Repository]
    B --> C[Deployment Pipeline]
    C --> D[Tokyo Server]
    
    subgraph "The Shortest Path Result"
    A --- B --- C --- D
    end
    
    style A fill:#4285F4,color:#fff
    style D fill:#34A853,color:#fff

4. Implementation: Building a Rich Subgraph in Python

Let's look at how we combine multiple patterns into a single "Context Block" for our agent.

# A complex Cypher query optimized for RAG
CYPHER_QUERY = """
MATCH (n:Person {id: $userId})
// Find direct contacts
MATCH (n)-[:WORKS_WITH]-(colleague:Person)
// Find their shared projects
MATCH (n)-[:CONTRIBUTES_TO]->(p:Project)<-[:CONTRIBUTES_TO]-(colleague)
// Aggregate into a JSON-like structure
RETURN n.name as user, 
       collect({colleague: colleague.name, project: p.title}) as network
"""

# The output gives the LLM a structured map of the user's professional world.

5. Summary and Exercises

AI-centric querying is about Assembly, not Counting.

  • Assemble facts into strings directly in Cypher.
  • Optional Match expands the context without breaking the query if data is missing.
  • ShortestPath provides the logical bridge between distant concepts.
  • Metadata (like lastModified) helps the LLM decide which fact is "Freshest."

Exercises

  1. Fact Assembler: Take the relationship (Book)-[:WRITTEN_BY]->(Author). Write a Cypher RETURN statement that generates the sentence: "The book [Title] was written by [Author Name] in [Year]."
  2. Optional Choice: Why is OPTIONAL MATCH safer for an AI agent than a standard MATCH? What happens if a standard MATCH fails to find a relationship? (The whole query returns 0 rows).
  3. Bridge Builder: Write a query to find the connection between any two nodes named A and B with a maximum of 4 hops.

In the next lesson, we will look at the ultimate hybrid: Combining Vector Search with Graph Queries.

Subscribe to our newsletter

Get the latest posts delivered right to your inbox.

Subscribe on LinkedIn