Monday, 24 April 2017

How to limit only a specific node in neo4j?

I am creating a social media-like platform that has a home feed. The home feed contains, posts, which can have any number of comments and likes on it. Comments can have any number of likes as well.

When the user first loads the home feed, I want to only fetch a certain number of posts at first (being 10). The query I am currently running is the following:

MATCH (p:Post)<-[:POSTED]-(u1:User), (u2: User {id: {id}})
WHERE u1.id = {id} OR (u1)-[:FRIENDS_WITH]-(u2)  
OPTIONAL MATCH (u4:User)-[:LIKES]->(p)
OPTIONAL MATCH (u3:User)-[:COMMENTED]->(c:Comment)<-[:HAS_COMMENT]-(p)
OPTIONAL MATCH (u5:User)-[:LIKES]->(c)
RETURN p, u1, u3, c, u4, u5 ORDER BY p.timestamp DESC LIMIT 10

The problem here is that it will only fetch 10 results, not necessarily 10 posts. I would like a way to collect 10 posts specifically and then fetch all comments/likes/comment likes on each of those posts. Is this possible in a single query?



via singwukgwu

No comments:

Post a Comment