Reputation: 1448
Assume that my project is Facebook. I want to display a feed which consists of my status updates and my friends' status updates both.
Here are the relations;
This is how I get my friends status updates;
START me = node(1) MATCH me-[:KNOWS]-()-[:UPDATES_STATUS]->friendsStatusUpdates RETURN friendsStatusUpdates
And this is how I get my own status updates;
START me = node(1) MATCH me-[:UPDATES_STATUS]->myStatusUpdates RETURN myStatusUpdates
Both queries work fine but I need a single query that combines these two.
Upvotes: 1
Views: 476
Reputation: 1448
Here is the answer I got from Google Groups;
START me = node(1) MATCH me-[:KNOWS*0..1]-()-[:UPDATES_STATUS]->statusUpdate RETURN DISTINCT statusUpdate
Only thing I had to do was adding *0..1
depth indicator to the relation in order to get both 0 or 1 level depth results.
Edit: I had to add DISTINCT
because without it query includes 0 level nodes 2 times which results in duplicates.
Alternative query which returns same results using WITH
statement;
START me = node(1)
MATCH me-[:KNOWS*0..1]-friend
WITH DISTINCT friend
MATCH friend-[:UPDATES_STATUS]->statusUpdate
RETURN DISTINCT statusUpdate
Upvotes: 2
Reputation: 5918
http://docs.neo4j.org/chunked/milestone/introduction-pattern.html
START me = node(1)
MATCH me-[:UPDATES_STATUS*1..2|KNOWS]-myStatusUpdates
RETURN myStatusUpdates
in case the *1..2
wont work with |
command, do this:
START me = node(1)
MATCH friendsStatusUpdates2-[?:UPDATES_STATUS]-me-[:KNOWS]-()-[:UPDATES_STATUS]->friendsStatusUpdates
RETURN distinct myStatusUpdates,friendsStatusUpdates2
just edit the RETURN
statement with some aggregation function so you will get one status per row
Upvotes: 0
Reputation: 1
Crate a himself relationship between user node to itself than query is START me = node(1) MATCH me-[:KNOWS|HIMSELF]-()-[:UPDATES_STATUS]->friendsStatusUpdates RETURN friendsStatusUpdates
Upvotes: 0