Neo4j - How to build proper multi-dimensional query for my graph -
i have simple social-networking graph w/ users, friends, comments, likes etc. users can "own" items, comment on "items", "items". trying write cypher query returns "items" along w/ information display them in stream.
i have tried using optional match , collect , stuff, there part of result doesn't work.
specifically, given user(say user1), want return "items" that:
- a specific user + friends own
- show number of likes,
- also show number of comments,
- know if item owned me (so can hide "own" button in ui)
- if item owned friends, want show name, image of 2 friends (but not more 2 friends if, say, 5 friends own item)
you can copy-paste below graph
// 3 users create (u1:user{name:"user1", image: "image1"}) create (u2:user{name:"user2", image: "image2"}) create (u3:user{name:"user3", image: "image3"}) //3 items create (i1:item{name:"item1"}) create (i2:item{name:"item2"}) create (i3:item{name:"item3"}) // ownership .. //user1 owns 2 items create (u1)-[:owns]->(i1) create (u1)-[:owns]->(i2) // user2 owns i2 , i3 create (u2)-[:owns]->(i2) create (u2)-[:owns]->(i3) // user3 owns i2 , i3 (so i2 owned u1, u2 , u3; , i3 owned u2 , u3) create (u3)-[:owns]->(i2) create (u3)-[:owns]->(i3) // friendship.. // user1 friend of both user2 , user3 create (u1)-[:friend_of]->(u2) create (u1)-[:friend_of]->(u3) // comments .. //user1 has commented on items owns create (u1i1:comment{text:"user1 comment on item1"}) create (u1)-[:commented]->(u1i1)-[:comment_for]->(i1) create (u1i2:comment{text:"user1 comment on item2"}) create (u1)-[:commented]->(u1i2)-[:comment_for]->(i2) //user 2 has commented on owns create (u2i2:comment{text:"user2 comment on item2"}) create (u2)-[:commented]->(u2i2)-[:comment_for]->(i2) create (u2i3:comment{text:"user2 comment on item3"}) create (u2)-[:commented]->(u2i3)-[:comment_for]->(i3) // likes .. //user1 has liked user2's , user3's items create (u1)-[:liked]->(i2) create (u1)-[:liked]->(i3) //user2 has liked user1's items create (u2)-[:liked]->(i1)
let's build query step step:
specifically, given user(say user1), want return "items" that: specific user + friends own
match (u:user {name: "user1"})-[:friend_of*0..1]-(friend:user)-[:owns]-(i:item) u,i, // know if item owned me (so can hide "own" button in ui) sum(size((u)-[:owns]->(i))) > 0 user_owns, // if item owned friends, want show name, image of 2 friends collect({name:friend.name, image:friend.image})[0..2] friends return u,i, user_owns, friends // show number of likes, sum(size(()-[:liked]->(i))) like, // show number of comments, sum(size(()-[:comment_for]->(i))) comments
actually because such question, sat down , created graphgist documenting each step here.
Comments
Post a Comment