cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

How to paginate intermediate nodes

Dowanna
Node Link

Hi, thanks in advance for all the help offered at this community, it's really helpful.

I couldn't quite understand what's happening with the situation below.
Say there's a test data like this. Users can 'like' a certain project.

CREATE
(a:User),(b:User),(c:User),(p1:Project{id:1}),(p2:Project{id:2}),(p3:Project{id:3}),
(a)-[:LIKED]->(p3),(b)-[:LIKED]->(p3),(c)-[:LIKED]->(p3),
(a)-[:LIKED]->(p2),(b)-[:LIKED]->(p2)

I would like to get projects in order of liked counts. This is the query I wrote:

MATCH (p:Project)
OPTIONAL MATCH (p)-[l:LIKED]-(:User)
WITH count(l) as likedCount, p ORDER BY likedCount DESC
// perform some expensive task on p
RETURN p.id, likedCount

Now, since I am performing some expensive task with p,
I want to limit the number of projects to work on, with something like this.

MATCH (p:Project)
OPTIONAL MATCH (p)-[l:LIKED]-(:User)
WITH count(l) as likedCount, p ORDER BY likedCount DESC
WITH collect(p)[0..3] as projects, likedCount
UNWIND projects as p
// perform expensive task, but only to limited projects
RETURN p.id, likedCount

The result:

p.id	likedCount
3	3
2	2
1	0

To paginate, for example excluding the most liked project, I would change [0..3] to [1..3] as such:

MATCH (p:Project)
OPTIONAL MATCH (p)-[l:LIKED]-(:User)
WITH count(l) as likedCount, p ORDER BY likedCount DESC
WITH collect(p)[1..3] as projects, likedCount
UNWIND projects as p
RETURN p.id, likedCount

Now I get this:

(no changes, no records)

Instead of getting 2 project rows, why did I get 0?

1 REPLY 1

Hi Dowanna,

count() aggregation function will be automatically grouped.
So Please change "(p)[1..3]" to "SKIP 1 LIMIT 3"

MATCH (p:Project)
OPTIONAL MATCH (p)-[l:LIKED]-(:User)
WITH count(l) as likedCount, p
  ORDER BY likedCount DESC SKIP 1 LIMIT 3
RETURN p.id, likedCount