Trying a single Neo4j Cypher query for my graph -
i have "users" owns "items", users friends w/ each other. trying construct cypher query return items own plus friends own in single query. can them individually can't figure out how in single query.
relationships:
(u:user)-[:owns]-(i:items) (u:user)-[:friend]-(f:user)
let's have 2 users in db , 100 items. out of 100, first person owns (1-5) 5 items , 2nd person owns 5 items(6-10). these 2 users friends.
i 5 items if do:
match (uer1)-[:owns]->(i:items) return
i 5 items if do:
match (uer1)-[:friend]->(f)-[:owns]->(i:items) return
but need combine them both given user(user1) can return 10 items in single shot. how that?
you have 2 (or more options)
union
match (user:user {name:"raja"})-[:owns]->(i:item) return union match (user:user {name:"raja"})-[:friend]->(f)-[:owns]->(i:item) return
variable length paths
match (user:user {name:"raja"})-[:friend*0..1]->(f)-[:owns]->(i:item) return
in case @ friends of distance 0 (the user itself) 1 (first degree friends)
the first option might faster second more versatile.
Comments
Post a Comment