Categories
Uncategorized

Cypher gotchas: multiple-match vs comma operator

Even as a long term Neo4j user with a 10y+ experience I’ve stumbled over something being new to me. Therefore I thought it’s worth sharing.

In Cypher there’s the comma operator to describe a graph pattern consisting of multiple parts, e.g.

MATCH (a:Person{name:'a'})-[:KNOWS]->(b:Person),
 (b)-[:KNOWS]->(c:Person)
RETURN a, b, c

In the example above I could have written the pattern in one single MATCH as well:

MATCH (a:Person{name:'a'})-[:KNOWS]->(b:Person)-[:KNOWS]->(c:Person)
RETURN a, b, c

However there are patterns that you cannot encode in a linear way:

MATCH (a:Person{name:'a'})-[:KNOWS]->(b)-[:KNOWS]->(c),
   (b)-[:OWNS]-(i:Item)
RETURN a, b, c,i

When teaching Cypher classes I always tell people that with a MATCH you basically “draw” nodes and relationships. The comma operator instructs to move the pen up and continue drawing at an already known node – b in the example above. So you create a composite pattern with the comma operator.

In a lot of cases using multiple-match or comma doesn’t make a difference. To prove differently consider this example:

Now let’s write a quey using comma operator:

MATCH (a:Person{name:'a'})-[:KNOWS]->(b),
 (b)-[:KNOWS]->(a),
 (a)-[:KNOWS]->(b)
RETURN *

This will return an empty result set. With multiple-match instead:

MATCH (a:Person{name:'a'})-[:KNOWS]->(b)
MATCH (b)-[:KNOWS]->(a)
MATCH (a)-[:KNOWS]->(b)
RETURN *

we’re getting a result back.

The nifty difference is that the comma variant considers one pattern whereas the second considers multiple separate patterns.

One important property of a pattern is that the very same relationship is never used more than once. So in the comma variant the first and last KNOWS relationships need to be different whereas in the multiple MATCH variant they could be the very same relationship.

So it’s about relationship uniqueness along a pattern. I hope this clarifies the difference between multiple matches and usage of the comma operator.

Leave a Reply

Your email address will not be published. Required fields are marked *