如何删除两个节点间属性值相同的重复关系?
发布于 4 年前 作者 anyway 1891 次浏览 来自 问答

想请教下大佬,如何删除两个节点间的重复关系,重复的定义指的是,关系的属性不同 比如 (A)-[:Realation{name:‘属性1’]-(B) (A)-[:Realation{name:‘属性1’]-(B) (A)-[:Realation{name:‘属性2’]-(B) (A)-[:Realation{name:‘属性2’]-(B) (A)-[:Realation{name:‘属性2’]-(B) (A)-[:Realation{name:‘属性3’]-(B) 想把重复的部分去掉,就是变成 (A)-[:Realation{name:‘属性1’]-(B) (A)-[:Realation{name:‘属性2’]-(B) (A)-[:Realation{name:‘属性3’]-(B)

3 回复

我也想知道

关系重构

MATCH p=(A:Test {name:'A'})-[r]->(B:Test {name:'B'})
WITH ID(r) AS id,r.name AS name
WITH name,COLLECT(id) AS relIds
WITH name,relIds,SIZE(relIds) AS relIdsSize
WHERE relIdsSize>1
WITH name,apoc.coll.subtract(relIds, [relIds[0]]) AS deleteRelIds
WITH name,deleteRelIds
MATCH ()-[r]-() WHERE ID(r) IN deleteRelIds DELETE r
  • 更多条件分支操作可参考如下实现
CALL apoc.do.case([relationship=1,\'MATCH (from:Label {hcode:$fromHcode}),(to:Label {hcode:$toHcode}) MERGE (from)-[:NEXT]->(to)\',relationship=-1,\'MATCH (from:Label {hcode:$fromHcode}),(to:Label {hcode:$toHcode}) MERGE (from)<-[:NEXT]-(to)\'],\'\',{fromHcode:fromHcode,toHcode:toHcode}) YIELD value RETURN value

创建样例数据

MERGE (A:Test {name:'A'})
MERGE (B:Test {name:'B'})
CREATE (A)-[:Realation {name:'属性1'}]->(B)
CREATE (A)-[:Realation {name:'属性1'}]->(B)
CREATE (A)-[:Realation {name:'属性2'}]->(B)
CREATE (A)-[:Realation {name:'属性2'}]->(B)
CREATE (A)-[:Realation {name:'属性2'}]->(B)
CREATE (A)-[:Realation {name:'属性3'}]->(B)

查询刚才创建的测试数据

MATCH p=(A:Test {name:'A'})-->(B:Test {name:'B'}) RETURN p
回到顶部