Cypher聚合和收集

时间:2014-09-19 08:52:26

标签: neo4j cypher

在我的密码查询中给出以下结果,其中人是集合:

[
   {
      "people": [
         {
            "id": 24749,
            "matches": 1
         },
         {
            "id": 26026,
            "matches": 1
         },
         {
            "id": 26223,
            "matches": 1
         },
         {
            "id": 25121,
            "matches": 1
         },
         {
            "id": 24632,
            "matches": 1
         },
         {
            "id": 25708,
            "matches": 1
         },
         {
            "id": 25182,
            "matches": 1
         },
         {
            "id": 24826,
            "matches": 1
         },
         {
            "id": 26186,
            "matches": 1
         },
         {
            "id": 27001,
            "matches": 1
         },
         {
            "id": 24243,
            "matches": 1
         },
         {
            "id": 27255,
            "matches": 1
         },
         {
            "id": 27145,
            "matches": 1
         },
         {
            "id": 24126,
            "matches": 1
         },
         {
            "id": 27463,
            "matches": 1
         },
         {
            "id": 24069,
            "matches": 1
         },
         {
            "id": 25210,
            "matches": 1
         },
         {
            "id": 24994,
            "matches": 1
         },
         {
            "id": 27331,
            "matches": 1
         },
         {
            "id": 25793,
            "matches": 1
         },
         {
            "id": 27312,
            "matches": 1
         },
         {
            "id": 26206,
            "matches": 1
         },
         {
            "id": 24252,
            "matches": 1
         },
         {
            "id": 24714,
            "matches": 2
         },
         {
            "id": 24612,
            "matches": 1
         },
         {
            "id": 26964,
            "matches": 1
         },
         {
            "id": 27101,
            "matches": 1
         },
         {
            "id": 26730,
            "matches": 1
         },
         {
            "id": 27211,
            "matches": 1
         },
         {
            "id": 24783,
            "matches": 2
         },
         {
            "id": 25336,
            "matches": 1
         },
         {
            "id": 24128,
            "matches": 1
         },
         {
            "id": 26186,
            "matches": 1
         },
         {
            "id": 25125,
            "matches": 2
         },
         {
            "id": 24069,
            "matches": 3
         },
         {
            "id": 24607,
            "matches": 1
         },
         {
            "id": 27055,
            "matches": 1
         },
         {
            "id": 25336,
            "matches": 3
         },
         {
            "id": 24128,
            "matches": 2
         },
         {
            "id": 26716,
            "matches": 1
         },
         {
            "id": 27331,
            "matches": 1
         },
         {
            "id": 24069,
            "matches": 1
         }
      ]
   }
]

我如何(使用cypher)迭代人员集合并找到具有相同" id"的那些,总结"匹配"将项目放在一起,然后添加一个名为" duplicates"或类似的。

示例结果我试图获得:

[
  {
    "people": [
      {
        "id": 24069,
        "matches": 5,  // all the "matches" of the duplicate 24069's added together
        "duplicates": 3 // how may times the id 24069 was found in the collection called people
      },

      // etc...
    ]
  }
]

2 个答案:

答案 0 :(得分:0)

查询:

  

匹配(p:people)返回distinct(p.id),sum(p.matches)

应该可以提供你需要的东西

答案 1 :(得分:0)

这有点复杂,因为最简单的方法是撕开你的收藏品并重新组装。这是一个很好的步骤,但是Cypher就是这样做的,也是对每个步骤的评论。

// initial matching (whatever you match on to get your collection)
MATCH (n:People) WITH collect(n) as people

// Tear people collection apart for possessing
UNWIND people as p 

// Reduce on id
WITH COLLECT(DISTINCT p.id) as id, p.matches as m

// For each id, sum and count the matches
WITH {id:id, matches:SUM(m), duplicates:COUNT(m)} as people

// Recollect rows into collection
RETURN COLLECT(people) as people