Neo4j重复关系

时间:2016-05-08 07:04:37

标签: neo4j cypher

我在节点之间有重复的关系,例如:

(Author)-[:CONNECTED_TO {weight: 1}]->(Coauthor)

(Author)-[:CONNECTED_TO {weight: 1}]->(Coauthor)

(Author)-[:CONNECTED_TO {weight: 1}]->(Coauthor)

我希望将这些关系合并为一个形式的关系:A-> {weight:3} B表示我的整个图形。

我尝试过类似下面的内容; (我正在读取csv文件中的数据)

MATCH (a:Author {authorid: csvLine.author_id}),(b:Coauthor { coauthorid: csvLine.coauthor_id})

CREATE UNIQUE (a)-[r:CONNECTED_TO]-(b)

SET r.weight = coalesce(r.weight, 0) + 1

但是当我开始这个查询时,createst会创建重复的coauthor节点。重量会更新。看起来像这样:

(Author)-[r:CONNECTED_TO]->(Coauthor)

(它为作者创建3个相同的coauthor节点)

1 个答案:

答案 0 :(得分:1)

如果您需要在事后修复它,您可以聚合所有关系以及每组适用节点之间的权重。然后使用新的聚合数字更新第一个关系。然后用关系集合删除第二个到最后一个。仅在存在多个关系的情况下执行更新。像这样......

match (a:Author {name: 'A'})-[r:CONNECTED_TO]->(b:CoAuthor {name: 'B'})

// aggregate the relationships and limit it to those with more than 1
with a, b, collect(r) as rels, sum(r.weight) as new_weight
where size(rels) > 1

// update the first relationship with the new total weight
set (rels[0]).weight = new_weight

// bring the aggregated data forward
with a, b, rels, new_weight

// delete the relationships 1..n
unwind range(1,size(rels)-1) as idx
delete rels[idx]

如果您正在为整个图表执行此操作并且图表是扩展的,您可能希望使用限制或其他控制机制批量更新它。

match (a:Author)-[r:CONNECTED_TO]->(b:CoAuthor)
with a, b, collect(r) as rels, sum(r.weight) as new_weight
limit 100
where size(rels) > 1
set (rels[0]).weight = new_weight
with a, b, rels, new_weight
unwind range(1,size(rels)-1) as idx
delete rels[idx]

如果你想在加载时消除这个问题......

MATCH (a:Author {authorid: csvLine.author_id}),(b:Coauthor { coauthorid: csvLine.coauthor_id})
MERGE (a)-[r:CONNECTED_TO]->(b)
  ON CREATE SET r.weight = 1
  ON MATCH SET r.weight = coalesce(r.weight, 0) + 1

旁注:并不真正了解您的数据模型,我会考虑将CoAuthor建模为Author,因为它们本身可能就是作者。可能只有在特定项目的背景下,他们才会被视为共同作者。

相关问题