Limit is not working as expected


Happy Monday. I hope you all are doing well.

Can anyone help me with a query and a scenario I am stuck with?

Use case:
I am working on a social network platform where we have posts shared by users and each post can have likes.


MATCH (p:Post)
WITH collect(DISTINCT p) as nodes, count(DISTINCT p) as count, count(l) as likes
OPTIONAL MATCH (u2:User)-[:LIKED]->(p)
WITH nodes, count, likes, u2 as likedBy
UNWIND nodes as results
RETURN results, count, likes, collect(likedBy) as likedByUsers```

In the query above, what I am trying to do is, fetch the **posts** along with the **users** who have liked the **post**. Here I am doing an **OPTIONAL MATCH** as there might be any **post** not liked by any **user**.

In the first OPTIONAL MATCH, I am counting the total number of likes, and in the second OPTIONAL MATCH I am trying to get the list of users who have liked the post. Now here I only want to get a limited number of users i.e. 5 or 10 and implement the pagination later on.

So when I add the LIMIT constraint, this limit is also applied to the posts as well.


Consider I have two posts A & B. Post A have 2 likes by different users. In this case:

1. When I try to limit it to 1 then I get only 1 post & 1 likedByUsers and when I increase this limit to let's say 3 I get 2 posts (which is fine) & two likedByUsers for post-A (which is also fine) but I am also getting 1 likedByUser for post B as well
2. Also according to this query I am getting likedByUsers list in the case of post B as well which shouldn't be the case as post B has no likes at this point in time.

I am new to RedisGraph and I'll appreciate it if anyone can help me out.

Hammad Rasheed.

HI @hammad.rasheed

The following will aggregate all users who’ve liked a post, I’ve used LIMIT to limit the number of posts taken into consideration, result-set includes the post(s) aggregated in addition to a list of users liked the post, it’s assumed a user can only like a given post only once.

MATCH (p:Post)
WITH p, collect(u) AS likedByUsers
RETURN p, likedByUsers, size(likedByUsers) AS likes
1 Like

Hi @SWilly22,

Thanks for your reply. I really appreciate that and apologies for my late response.

I’ll check it out and get back to you.

Thanks & Best Regards,
Hammad Rasheed