Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
11-02-2019 06:27 PM
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.
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?
11-04-2019 05:16 AM
You could try collecting on a map that contains the user and the relationship/comment perhaps. So something like this:
match (u1:User{name:'andy'})-[created:CREATED]->(c1:Comment)
match (:User{name:'me'})-[:FRIENDS_WITH]->(u2:User)-[created2:CREATED]->(c2:Comment)
WITH collect(u1) + collect(u2) as u, collect(c1) + collect(c2) as c
WITH collect({user: u1, relType: created, comment: c1}) +
collect({user: u2, relType: created2, comment: c2})
Not sure if that's what you want?
11-11-2019 12:58 AM
Thank you mark!
I'm not sure how that query works. In the second WITH you are using u1, but the first WITH only passes u and c, so it doesn't work on my local environment...!
Also wouldn't two consecutive MATCH mean BOTH conditions must be met?
I apologize my description wasn't detailed enough, but I need EITHER conditions met!
11-04-2019 11:43 AM
Hello, here's an article on different ways to do post-UNION processing, that should have some workarounds you can use.
And just to note there are some features coming in 4.0 that will make this much easier to tackle.
All the sessions of the conference are now available online