代码之家  ›  专栏  ›  技术社区  ›  Myoch

对列表中的节点计数时结果错误

  •  0
  • Myoch  · 技术社区  · 4 年前

    WITH ["a", "b", "c", "d"] as list
    MATCH (n:Node)-[]-(g:Group)-[]-(n2:Node) WHERE n.name IN list
    RETURN g, COUNT(n) AS intersection, COUNT(n)+COUNT(n2) AS groupSize
    

    基本上,我要计算组g中存在的所有元素,以及列表和组之间的交集大小。

    问题:结果是错误的

    intersection = {number of Nodes connected to Group g} - 1
    groupSize = 2 * ({number of Nodes connected to Group g} - 1)
    

    0 回复  |  直到 4 年前
        1
  •  0
  •   Myoch    4 年前

    我学会了如何连续多次过滤,从而得到想要的结果:

    WITH ["a", "b", "c", "d"] as list
    MATCH  (n:Node)--(g:Group)
    WHERE n.name IN list
    WITH g, COUNT(n) as intersectionSize
    MATCH (n2:Node)--(g)
    RETURN g, COUNT(n2) as groupSize, intersectionSize
    ORDER BY intersectionSize DESC
    

    我希望能更简洁些,但是。。。

        2
  •  0
  •   cybersam    4 年前

    [编辑]

    你不应该使用 MATCH Node 节点,因为这会创建每个节点的大量重复引用。同样,同一对将被发现两次(按相反的顺序)。

    此查询可能会执行您想要的操作:

    WITH ["a", "b", "c", "d"] as list
    MATCH (n:Node)--(g:Group)
    WITH list, g, COLLECT(n) AS ns
    WITH g, SIZE(ns) AS groupSize, SIZE([x IN ns WHERE x.name IN list]) AS intersection
    WHERE intersection > 0
    RETURN g, groupSize, intersection