4

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":"test@test.com"}},{"id":5,"date_created":1438621422,"content":"about to try this thing","account":{"id":0,"username":"test2","email":"test2@gmail.com"}}]

it should be

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

any pointers?

RedactedProfile
  • 2,748
  • 6
  • 32
  • 51

2 Answers2

6

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

RedactedProfile
  • 2,748
  • 6
  • 32
  • 51
  • 1
    This is awesome and solved my problem. The only sad thing is neo4j browser says: "This feature is deprecated and will be removed in future versions." – GreenAsJade May 03 '19 at 03:11
  • @GreenAsJade glad it helped you, but specifically which feature is deprecated now? – RedactedProfile May 03 '19 at 21:05
  • I don't actually understand the error message, honestly. It says "Binding relationships to a list in a variable length pattern is deprecated. (Binding a variable length relationship pattern to a variable ('c') is deprecated and will be unsupported in a future version. The recommended way is to bind the whole path to a variable, then extract the relationships: MATCH p = (...)-[...]-(...) WITH *, relationships(p) AS c)". I wonder if you would be kind enough to look at https://stackoverflow.com/q/55964038/554807 – GreenAsJade May 04 '19 at 01:42
1

I think that you could do this in one of two ways:

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

I think that friend would be NULL in the cases where it's a post belonging to the first account. This also should work:

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

Do either of those get you what you want?

Brian Underwood
  • 10,746
  • 1
  • 22
  • 34
  • I appreciate your response, I will try these at my earliest possibility – RedactedProfile Aug 04 '15 at 05:06
  • The second item is very close, but it seems to duplicate one of the posts and attribute it to a different account.. this returns three post results in total instead of just the two in the database lol – RedactedProfile Aug 05 '15 at 05:36
  • Ah, right. Well you could add a `DISTINCT` (like `RETURN DISTINCT friend, post`), but it might be useful to do `RETURN post, collect(friend)` which gives you a collection of all of the friends who have shared that post. You can even do `collect(DISTINCT friend)` if that's helpful. – Brian Underwood Aug 05 '15 at 11:27