Reputation: 5865
I have "users" who owns "items", users are also friends w/ each other. I am trying to construct a cypher query to return all items I own PLUS those my friends own in a single query. I can do them individually but can't figure out how to do it in a single query.
RELATIONSHIPS:
(u:user)-[:OWNS]-(i:items)
(u:user)-[:FRIEND]-(f:user)
Let's say I have just two users in my DB and 100 items. Out of 100, the first person owns (1-5) 5 items and the 2nd person owns another 5 items(6-10). These two users are also friends.
I get 5 items if I do:
MATCH (uer1)-[:OWNS]->(i:items) return i
I get another 5 items if I do:
MATCH (uer1)-[:FRIEND]->(f)-[:OWNS]->(i:items) return i
But I need to combine them both for a given user(user1) so I can return all 10 items in a single shot. How to do that?
Upvotes: 2
Views: 48
Reputation: 41676
You have two (or more options)
MATCH (user:User {name:"Raja"})-[:OWNS]->(i:Item) return i
UNION
MATCH (user:User {name:"Raja"})-[:FRIEND]->(f)-[:OWNS]->(i:Item) return i
MATCH (user:User {name:"Raja"})-[:FRIEND*0..1]->(f)-[:OWNS]->(i:Item) return i
in this case we look at friends of the distance 0 (the user itself) to one (first degree friends)
The first option might be faster The second is more versatile.
Upvotes: 2