If the first MATCH doesn't found anything.
The second MATCH doesn't work (but node exist in DB)
How could it be fixed?
Example
MATCH(h1:Human)-[r]->(d:DOG{name:"rome"})
WITH DISTINCT h1
MATCH(h2:Human{name:"Tom"})
RETURN h2
If the first MATCH doesn't found anything.
The second MATCH doesn't work (but node exist in DB)
How could it be fixed?
Example
MATCH(h1:Human)-[r]->(d:DOG{name:"rome"})
WITH DISTINCT h1
MATCH(h2:Human{name:"Tom"})
RETURN h2
The thing to understand is that Cypher operations execute per row. They are not independent of each other. If there is no row to execute upon, then it's a no-op (though there are some exceptions for aggregations, such as counting or collecting across 0 rows).
What's going on is the first MATCH fails to find anything, you drop down to 0 rows.
Now there are no rows to execute upon. The second MATCH doesn't even execute.
If you anticipate that certain match patterns might return no results, yet you want the execution to continue, use OPTIONAL MATCH, since that does not filter out rows when the match pattern isn't found (it emits nulls for newly-introduced variables in the pattern instead).
OPTIONAL MATCH (h1:Human)-[r]->(d:DOG{name:"rome"})
WITH DISTINCT h1
MATCH (h2:Human{name:"Tom"})
RETURN h2
In this case, the pattern will not be found, but the row is not filtered out, all variables are newly introduced, so they are all null
.
WITH DISTINCT h1
executes for that row, emitting a row where the variable h1
is null
for that row.
Then the MATCH executes on that row, finding the node, and returning it.