I'm developing an SNS with neo4j, and would like to know how to achieve the case described below.
Please note that the use-case may not sound practical. It's because I'm trying to simplify the case. The actual conditions are much complicated, so I didn't want to write everything here.
Let's say I want to fetch all comments and its author by two conditions.
- I want a comment by andy
- I want a comment by my friends
To MATCH with multiple conditions, I have the following:
match (u1:User{name:'andy'})-[:CREATED]->(c1:Comment)
match (:User{name:'me'})-[:FRIENDS_WITH]->(u2:User)-[:CREATED]->(c2:Comment)
return u1,u2,c1,c2
However, to further process the User and Comment nodes, I want to pass these nodes with WITH, whilst combining them. Something like this would be ideal:
match (u1:User{name:'andy'})-[:CREATED]->(c1:Comment)
match (:User{name:'me'})-[:FRIENDS_WITH]->(u2:User)-[:CREATED]->(c2:Comment)
WITH (u1,u2) as u, (c1,c2) as c
...
Since that's not possible, I made several different attempts:
match (u1:User{name:'andy'})-[:CREATED]->(c1:Comment)
match (:User{name:'me'})-[:FRIENDS_WITH]->(u2:User)-[:CREATED]->(c2:Comment)
WITH collect(u1) + collect(u2) as u, collect(c1) + collect(c2) as c
UNWIND u
UNWIND c
...
But this would destroy the relation who created the comment, so doesn't work.
match p1=(u1:User{name:'andy'})-[:CREATED]->(c1:Comment)
match p2=(:User{name:'me'})-[:FRIENDS_WITH]->(u2:User)-[:CREATED]->(c2:Comment)
WITH p1+p2 as p
...
This doesn't work because + requires a list as argument.
UNION does not work because it must be followed by RETURN, but there's a ton of post-processing to do after this query.
How can I achieve such query?