ONgDB重复关系重构

 

Here’s the table of contents:

  1. 创建样例数据
  2. 重复数据定义
  3. 查询刚才创建的测试数据
  4. 关系重构

创建样例数据

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)

重复数据定义

(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)
    

查询刚才创建的测试数据

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

关系重构

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