I have a simple social-networking like graph w/ users, friends, comments, likes etc. Users can "own" items, comment on "items", like "items". I am trying to write a cypher query that returns "items" along w/ extra information to display them in my stream.
I have tried using optional match and collect and stuff, but there is always some part of the result that doesn't work.
Specifically, for a given user(say user1), I want to return "items" that:
- a specific user + his friends own
- show number of likes,
- also show number of comments,
- Know if the item is already owned by me (so I can hide "own" button in the UI)
- If the item is owned by friends, I want to show name, image of up to 2 friends (but not more than 2 friends if, say, 5 friends own that item)
You can copy-paste below to get the 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 and i3
CREATE (u2)-[:OWNS]->(i2)
CREATE (u2)-[:OWNS]->(i3)
// user3 also owns i2 and i3 (so i2 is owned by u1, u2 and u3; and i3 is owned by u2 and u3)
CREATE (u3)-[:OWNS]->(i2)
CREATE (u3)-[:OWNS]->(i3)
// FRIENDSHIP..
// user1 is friend of both user2 and user3
CREATE (u1)-[:FRIEND_OF]->(u2)
CREATE (u1)-[:FRIEND_OF]->(u3)
// COMMENTS ..
//user1 has commented on all those items he 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 also commented on all those he 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 and user3's items
CREATE (u1)-[:LIKED]->(i2)
CREATE (u1)-[:LIKED]->(i3)
//user2 has liked user1's items
CREATE (u2)-[:LIKED]->(i1)