cypher limit in neo4j including duplicate rows

173 Views Asked by At

Using Neo4J with Cypher

I am trying to aquire what is essentially a users "wall". In this wall is posts by you, your friends, and the posts comments.

This is my query so far:

match (a:Account)-[:FRIEND*0..1]->(friend:Account)-[:POST]->(post:Post)
where a.uid = "u132984734sas"
optional match (post)<-[:COMMENT*0..1]-(comment:Comment)<-[:AUTHOR]-(commentAuthor:Account)
return friend,post,comment,commentAuthor
ORDER BY post.date_created DESC
LIMIT 10

Note the LIMIT 10

Well I half expected this, but much like Doctrine in PHP when fetching nested arrays of things, here a row is returned featuring the top level post and a single comment for each comment in a post.

What this means is, if there are 20 posts that match this criteria WITHOUT fetching comments, it would return back 10 actual posts thanks to the LIMIT criteria
Lets say a single post within the first 10 results has 2 comments in it, now the resultSet is only going to feature 9 posts instead of 10. Because the first comment is always returned in the first row if available, it doesn't detract form the number returned back by the resultSet, but every additional comment per post, does.

I'd like to get a reliable 10 results here, but I'm unsure of what to do here.

For sake of brevity, I am using Everyman\Neo4JPHP

I've even contemplated querying for 50 posts and pruning through only picking the top 10 results but this obviously doesn't seem right haha

1

There are 1 best solutions below

0
On

Here is a slight variation of what @FrobberOfBits proposed in his comment to the question.

When using aggregation, instead of creating two collections (one for comment and another for commentAuthor), you can create a single collection whose elements are comment and commentAuthor pairs.

MATCH (a:Account)-[:FRIEND*0..1]->(friend:Account)-[:POST]->(post:Post)
WHERE a.uid = "u132984734sas"
OPTIONAL MATCH (post)<-[:COMMENT*0..1]-(comment:Comment)<-[:AUTHOR]-(commentAuthor:Account)
RETURN friend,post, COLLECT({comment: comment, commentAuthor: commentAuthor}) AS comments
ORDER BY post.date_created DESC 
LIMIT 10