8
votes

I'd like to combine two requests into one query and I'm not sure what happens when 2 match statements are used in a single cypher query.

say I have a list of friends and I'd like to see a list of my friends with each of their uncles and siblings listed in a collection. Can I have two match statements that would do the job? e.g.

match friends-[:childOf]->parents-[:brother]->uncles
    , friends-[:childOf]->parents<-[:childOf]-siblings
return friends, collect(siblings), collect(uncles)

However, if I do a query like this, it always returns no results.

2
Can you share an example graph via console.neo4j.org?Thomas Fenzl

2 Answers

8
votes

Since you have already chosen parents in your first match class, you can do like this -

match friends-[:childOf]->parents-[:brother]->uncles
with friends, parents, uncles
match parents<-[:childOf]-siblings
return friends, collect(siblings), collect(uncles)
1
votes

You may want to make some of those relationships optional. For example, if you find a sibling but you don't find any uncles, this query will return null because it didn't satisfy both match clauses. If you make the end relationships optional then you don't have to satisfy both clauses completely to return data. So:

match friends-[:childOf]->parents-[?:brother]->uncles
    , friends-[:childOf]->parents<-[?:childOf]-siblings
return friends, collect(siblings), collect(uncles)