cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Combine the result of match and use for further processing

Dowanna
Node Link

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.

  1. I want a comment by andy
  2. 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?

3 REPLIES 3

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?

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!

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.