Patrick
Patrick

Reputation: 478

Subqueries in neo4j 3.5 (for Pagination)

I am trying to implement a pagination using neo4j 3.5 and I realized that subqueries (such as Call {}) is not supported in this version. The common approach for pagination in this particular version is (cypher pagination total result count):

// -------------------------------------
// FIRST QUERY
// -------------------------------------
MATCH (x:Brand)
WITH count(*) as total

// -------------------------------------
// SECOND QUERY
// -------------------------------------
MATCH (o:Brand)
WITH total, o
ORDER BY o.name SKIP 5 LIMIT 5
WITH total, collect({uuid:o.uuid, name:o.name}) AS brands
RETURN {total:total, brands:brands}

This works if the block where we get the count is just a single type. I have a complex query that has nested matches like:

MATCH (px:Type1)-[:Relationship1]->(pvx:Type2 { prop:'somevalue'})-[:Relationship2]->(bx:Type3)                     
     MATCH (px)-[:Rel3]->(ptx:Type4)
// and so on

To get the value of this complex query, I had to enclose the query inside "apoc.cypher.run", which I was able to successfully get with this:

CALL apoc.cypher.run("MATCH (x:Type1) return count(*) as total", {})
YIELD value
return value.total

Is there a way to pass the result of this apoc.cypher.run to the second query, so I may be able to return the total number of records, as a variable in the second query?

Upvotes: 0

Views: 321

Answers (1)

Charlotte Skardon
Charlotte Skardon

Reputation: 6270

Something like this?

CALL apoc.cypher.run("MATCH (x:Type1) return count(*) as total", {})
YIELD value AS total
WITH total
MATCH (o:Brand)
WITH total, o
ORDER BY o.name SKIP 5 LIMIT 5
WITH total, collect({uuid:o.uuid, name:o.name}) AS brands
RETURN {total:total, brands:brands}

Upvotes: 1

Related Questions