Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Optional relationship with Cypher

Got a wall post type scenario where I'd like to spill out posts by you, your friends, and sort them chronologically with a limit

Is there a way to do this without using a union?

like, I'd almost like something like this (if you'll forgive the pseudo-query):

match (a:Account)-([:FRIEND]->(friend:Account)-)?[:POST]->(post:Post)
where id(a) = 0
return friend,post

I'm just wondering how to get your posts as well as your friends posts in one query

Right now I have (0 is the id of the logged in account right now):

match (a:Account)-[:FRIEND]->(friend:Account)-[:POST]->(post:Post)
where id(a) = 0
return friend,post
union
match (friend:Account)-[:POST]->(post:Post)
where id(friend) = 0
return friend,post
order by post.date_created DESC
limit 10

and while this works in acquiring all the desired posts I want (according to my tests), the order by only seems to organize based on the individual result sets and not as the whole unioned result set

basically this should not be my resulting json:

[{"id":4,"date_created":1438621410,"content":"This is a test post!","account":{"id":10,"username":"test","email":"[email protected]"}},{"id":5,"date_created":1438621422,"content":"about to try this thing","account":{"id":0,"username":"test2","email":"[email protected]"}}]

it should be

[{"id":5,"date_created":1438621422,"content":"about to try this thing","account":{"id":0,"username":"test2","email":"[email protected]"}}, {"id":4,"date_created":1438621410,"content":"This is a test post!","account":{"id":10,"username":"test","email":"[email protected]"}}]

any pointers?

like image 755
RedactedProfile Avatar asked Nov 01 '25 15:11

RedactedProfile


1 Answers

Brian Underwood's answer happened to lead me in the right direction but ultimately the solutions attempted yielded not quite right results

However after some tinkering I did manage to stumble upon this winning query:

MATCH (a:Account)-[:FRIEND*0..1]->(friend:Account)-[:POST]->(post:Post)
WHERE id(a) = 0
RETURN friend,post
ORDER BY post.date_created DESC
LIMIT 10

Hopefully this helps others using Neo for acquiring Facebook "Wall" like updates which include your own posts.

Any improvements to this query would be welcome however, I have no idea how efficient it really is, only that it does indeed sort properly and to my knowledge so far limits accordingly

like image 52
RedactedProfile Avatar answered Nov 04 '25 07:11

RedactedProfile



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!