Neo4j Cypher 在同一查询中返回计数和实际数据进行分页

问题描述 投票:0回答:2

是否可以避免两次调用几乎相同的查询 - 一次用于分页计数,然后用于实际数据,并将它们组合到单个 Cypher 调用中?

neo4j cypher
2个回答
3
投票

我在单个查询中执行此操作是一个要求,您可以按照这些方式执行某些操作,假设您有这些参数:

{
  "page": 0,
  "pageSize": 10
}

此查询

MATCH (n:Movie) 
WITH COLLECT({title:n.title}) AS allResults
WITH allResults[($page * $pageSize)..($page+1)*$pageSize] AS pageResults,
     toInt(size(allResults)/$pageSize) + 1 AS numPages,
     size(allResults) AS totalCount
RETURN totalCount,$page, numPages, pageResults

退货

╒════════════╤═══════╤══════════╤══════════════════════════════════════════════════════════════════════╕
│"totalCount"│"$page"│"numPages"│"pageResults"                                                         │
╞════════════╪═══════╪══════════╪══════════════════════════════════════════════════════════════════════╡
│46          │0      │5         │[{"title":"Forrest Gump"},{"title":"Appolo 13"},{"title":"The Matrix"}│
│            │       │          │,{"title":"The Matrix Reloaded"},{"title":"The Matrix Revolutions"},{"│
│            │       │          │title":"The Devil's Advocate"},{"title":"A Few Good Men"},{"title":"To│
│            │       │          │p Gun"},{"title":"Jerry Maguire"},{"title":"Stand By Me"}]            │
└────────────┴───────┴──────────┴──────────────────────────────────────────────────────────────────────┘

0
投票

此解决方案将工作分为两部分:首先获取完整的行列表,执行WITH对它们进行计数并将它们收集到列表中,然后使用collect()执行分页的子查询。如果您需要引入有关结果中行的附加信息,请将其放在“WITH item”和“RETURNcollect(item)”之间,以便仅针对您要返回的页面上的项目完成此操作。

MATCH (n:MyLabel)
WITH collect(n) as collection,count(n) as cnt 
CALL{
    WITH collection
    UNWIND collection as item
    WITH item ORDER BY item.id DESC SKIP 10 LIMIT 5
    RETURN collect(item) as items
}
RETURN cnt, items
© www.soinside.com 2019 - 2024. All rights reserved.