首页 > 解决方案 > 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)。这个限制让我很困扰。想象一下,父母有这个孩子

在这种情况下,我的查询有时会返回(35 x A)。我想要实现的是按此父级的最稀有类型的子级进行此查询排序,并在此示例中返回此数据:

标签: neo4jcypher

解决方案


我使用电影数据库测试了以下查询。

  • 收集父、子、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 

样本结果:

╒═══════════════════════════╤══════════════════════════════════════════════════════════════════════╤══════════╤═════════════╕
│&quot;Parent"                   │&quot;Child"                                                               │&quot;R"       │&quot;ChildDegree"│
╞═══════════════════════════╪══════════════════════════════════════════════════════════════════════╪══════════╪═════════════╡
│{"name":"Jessica Thompson"}│{"name":"Paul Blythe"}                                                │&quot;FOLLOWS" │2            │
├───────────────────────────┼──────────────────────────────────────────────────────────────────────┼──────────┼─────────────┤
│{"name":"Jessica Thompson"}│{"name":"James Thompson"}                                             │&quot;FOLLOWS" │3            │
├───────────────────────────┼──────────────────────────────────────────────────────────────────────┼──────────┼─────────────┤
│{"name":"Jessica Thompson"}│{"name":"Angela Scope"}                                               │&quot;FOLLOWS" │3            │
├───────────────────────────┼──────────────────────────────────────────────────────────────────────┼──────────┼─────────────┤
│{"name":"Jessica Thompson"}│{"tagline":"Come as you are","title":"The Birdcage","released":1996}  │&quot;REVIEWED"│5            │
├───────────────────────────┼──────────────────────────────────────────────────────────────────────┼──────────┼─────────────┤
│{"name":"Jessica Thompson"}│{"tagline":"It's a hell of a thing, killing a man","title":"Unforgiven│&quot;REVIEWED"│5            │
│                           │&quot;,"released":1992}                                                    │          │             │
├───────────────────────────┼──────────────────────────────────────────────────────────────────────┼──────────┼─────────────┤
│{"name":"Jessica Thompson"}│{"tagline":"Break The Codes","title":"The Da Vinci Code","released":20│&quot;REVIEWED"│7            │
│                           │06}                                                                   │          │             │
├───────────────────────────┼──────────────────────────────────────────────────────────────────────┼──────────┼─────────────┤
│{"name":"Jessica Thompson"}│{"tagline":"Pain heals, Chicks dig scars... Glory lasts forever","titl│&quot;REVIEWED"│8            │
│                           │e":"The Replacements","released":2000}                                │          │             │
└───────────────────────────┴──────────────────────────────────────────────────────────────────────┴──────────┴─────────────┘

推荐阅读