Reputation: 2878
I have a query which supposes to join two results and give the union as output.
MATCH (:Some )-[r*0..1]-> (p:Address) WITH collect(p) AS rows1
MATCH (:Some2)-[r*0..1]-> (pp:Address)
WITH rows1+collect(pp) AS rows2
UNWIND rows2 AS row RETURN row
As you can see the selection has two parts. So it works fine if there is matching data for both queries but if the second part of the match does not return anything then it returns empty. Meaning MATCH (:Some2)-[r*0..1]-> (pp:Address)
returns empty then the whole union fails and returns null even if MATCH (:Some )-[r*0..1]-> (p:Address)
return values .
How to fix this? Is it a bug in neo4j?
Upvotes: 2
Views: 999
Reputation: 2878
Thanks for your inputs I used following version
MATCH (p:Address)
WHERE exist ((:Some )-[r*0..1]-> (p)) OR ((:Some2 )-[r*0..1]-> (p))
RETURN p;
Upvotes: 0
Reputation: 66947
This simple query should work:
MATCH (s)-[*0..1]->(p:Address)
WHERE s:Some OR s:Some2
RETURN p;
Upvotes: 1
Reputation: 2656
You have not actually asked a question (you have indeed just described the expected behaviour for pattern matching ... namely that if there is no match there is no result) ... but I assume you want a solution ?
MATCH (:Some )-[r*0..1]-> (p:Address)
RETURN p
UNION
MATCH (:Some2 )-[r*0..1]-> (p:Address)
RETURN p
should do the trick. Note that the only thing that matters is that the variables returned are exactly the same (what they contain doesn't actually matter).
Also ... you might want to have a look at what OPTIONAL MATCH does ...
Hope this helps.
Regards, Tom
Update
CALL apoc.cypher.run("
MATCH (:Some )-[r*0..1]-> (p:Address)
RETURN p
UNION
MATCH (:Some2 )-[r*0..1]-> (p:Address)
RETURN p",{}) YIELD value
RETURN value.p AS result
ORDER BY result;
Upvotes: 0