neo4j - Cypher - 按子节点类型的稀有度排序
问题描述
我有这个密码查询:
MATCH (Parent)-[R]-(Child) WHERE ID(Parent)=$parentId
CALL {
WITH Child
RETURN apoc.node.degree(Child) as ChildDegree
}
WITH Parent, Child, R, ChildDegree
RETURN Parent, Child, type(R), ChildDegree
ORDER BY R
LIMIT 35
它返回有限的数据(限制为 35)。这个限制让我很困扰。想象一下,父母有这个孩子:
- 40× A
- 3× B
- 2× C
在这种情况下,我的查询有时会返回(35 x A)。我想要实现的是按此父级的最稀有类型的子级进行此查询排序,并在此示例中返回此数据:
- 2× C
- 3× B
- 30× A
解决方案
我使用电影数据库测试了以下查询。
- 收集父、子、R 和子学位,并将所有子学位放在一个列表中(collect_nodes)
- 创建一个索引范围来累积子度数的总和(range_idx)
- 从0到行数,得到度数的运行总和
- 从每个父、子、R 和子学位,检查 sum_degree <= 35
- 返回父、子、R和子学位
您无法获得等于 35 的确切行数,因为您限制的是行数而不是子度数。另外,请向我们展示要处理的示例数据,以便我们为您提供最佳答案
MATCH (Parent)-[R]-(Child) WHERE ID(Parent)=$parentId
CALL {
WITH Child
RETURN apoc.node.degree(Child) as ChildDegree
}
WITH Parent, Child, type(R) as R, ChildDegree ORDER BY R, ChildDegree
WITH collect({p:Parent, c:Child, r: R, cd:ChildDegree }) as collect_nodes, collect(ChildDegree) as collect_degs
WITH collect_nodes, collect_degs, RANGE(0, SIZE(collect_degs)-1) AS range_idx
UNWIND range_idx as idx
WITH collect_nodes[idx] as nodes, REDUCE(acc = 0, value in (collect_degs[idx] + collect_degs[..idx]) | acc + value) AS sum_degree
UNWIND nodes as n_set
WITH n_set.p as Parent, n_set.c as Child, n_set.r as R, n_set.cd as ChildDegree WHERE sum_degree <= 35
RETURN Parent, Child, R, ChildDegree
样本结果:
╒═══════════════════════════╤══════════════════════════════════════════════════════════════════════╤══════════╤═════════════╕
│"Parent" │"Child" │"R" │"ChildDegree"│
╞═══════════════════════════╪══════════════════════════════════════════════════════════════════════╪══════════╪═════════════╡
│{"name":"Jessica Thompson"}│{"name":"Paul Blythe"} │"FOLLOWS" │2 │
├───────────────────────────┼──────────────────────────────────────────────────────────────────────┼──────────┼─────────────┤
│{"name":"Jessica Thompson"}│{"name":"James Thompson"} │"FOLLOWS" │3 │
├───────────────────────────┼──────────────────────────────────────────────────────────────────────┼──────────┼─────────────┤
│{"name":"Jessica Thompson"}│{"name":"Angela Scope"} │"FOLLOWS" │3 │
├───────────────────────────┼──────────────────────────────────────────────────────────────────────┼──────────┼─────────────┤
│{"name":"Jessica Thompson"}│{"tagline":"Come as you are","title":"The Birdcage","released":1996} │"REVIEWED"│5 │
├───────────────────────────┼──────────────────────────────────────────────────────────────────────┼──────────┼─────────────┤
│{"name":"Jessica Thompson"}│{"tagline":"It's a hell of a thing, killing a man","title":"Unforgiven│"REVIEWED"│5 │
│ │","released":1992} │ │ │
├───────────────────────────┼──────────────────────────────────────────────────────────────────────┼──────────┼─────────────┤
│{"name":"Jessica Thompson"}│{"tagline":"Break The Codes","title":"The Da Vinci Code","released":20│"REVIEWED"│7 │
│ │06} │ │ │
├───────────────────────────┼──────────────────────────────────────────────────────────────────────┼──────────┼─────────────┤
│{"name":"Jessica Thompson"}│{"tagline":"Pain heals, Chicks dig scars... Glory lasts forever","titl│"REVIEWED"│8 │
│ │e":"The Replacements","released":2000} │ │ │
└───────────────────────────┴──────────────────────────────────────────────────────────────────────┴──────────┴─────────────┘
推荐阅读
- javascript - Google 登录不仅适用于网络移动设备
- kubernetes - Nginx 入口控制器安装错误,“dial tcp 10.96.0.1:443: i/o timeout”
- javascript - 模态元素没有隐藏 [React & React - Bootstrap]
- android - 使用函数将一个活动移动到另一个活动
- python - Python:我如何抓取 ESPN 的比赛对决
- python - pyqtgraph.opengl VS OpenGL.GL - 如何在 PyQt5 OpenGL 嵌入式小部件上显示轴
- javascript - 试图用复选框过滤掉结果
- c++ - 找不到未解析的外部符号 _wWinMain@16 的解决方案
- php - 调用未定义函数 App\Transaksi()
- php - 如何在 chrome 开发工具中查看帖子正文数据?