Complex Pattern Matching
Introduction
Welcome to the final lesson of Advanced Queries! You've completed an impressive journey through aggregations, pattern matching, multi-hop traversals, and shortest paths. Each lesson has built upon the last, expanding your ability to extract insights from connected data.
Throughout this course, we've learned powerful techniques for querying graphs, but we've primarily focused on one pattern at a time. However, real-world problems often require combining multiple patterns to capture complex relationships. For instance, we might want to recommend movies based on both similar viewing history and shared genre preferences, or find friends who not only share connections but also have common interests.
In this lesson, we'll discover how to combine multiple patterns within a single query to perform sophisticated analysis. We'll learn how to match independent patterns simultaneously, filter across these patterns to refine results, and understand how Neo4j executes these queries efficiently. These techniques are essential for building recommendation systems, analyzing multi-faceted relationships, and extracting nuanced insights from graph data.
Combining Multiple Patterns
When analyzing graphs, we often need to consider several types of relationships at once. A single pattern tells us about one aspect of the data, but combining patterns reveals richer insights. For example, knowing that two users are friends is useful, but knowing they're friends who also share interests is far more meaningful.
Neo4j allows us to specify multiple patterns in a single query using two approaches. First, we can include several comma-separated patterns within one MATCH clause. Second, we can use consecutive MATCH clauses to build up complexity gradually. Both approaches enable us to express sophisticated queries that consider multiple relationship types and node properties simultaneously.
The key advantage of combining patterns is that we can express conditions that span different parts of the graph. We might match one pattern to find potential recommendations and another pattern to verify shared characteristics. This multidimensional matching is what makes graph databases exceptionally powerful for complex analysis.
Finding Friends with Shared Interests
Let's start with a practical example: finding which of Alice's friends share common interests with her. This requires matching two distinct patterns simultaneously:
This query combines two patterns separated by a comma. The first pattern (me)-[:FRIENDS_WITH]->(friend) finds all of Alice's friends. The second pattern (me)-[:LIKES]->(interest)<-[:LIKES]-(friend) finds interests that both Alice and her friend like. Notice how both patterns reference the same me and friend variables, creating a connection between them.
The structure here is important: we're not just finding friends, and we're not just finding interests; we're finding friends who share specific interests with Alice. The comma separating the patterns tells Neo4j to match both conditions simultaneously. The collect() function then aggregates all shared interests for each friend into a convenient list.
Visualizing Pattern Overlap
To see how these patterns connect, let's visualize the graph structure:

In this visualization, you can clearly see how the two patterns overlap through shared nodes. Alice connects to both her friends (Bob and David) through FRIENDS_WITH relationships, and the LIKES relationships converge on the shared Interest nodes. This visual representation makes it immediately clear how the variable connections work—me and friend appear in both patterns, creating the join condition that finds shared interests.

