Convert multiple relationships between 2 nodes to a single one with weight

淺唱寂寞╮ 提交于 2019-12-11 23:24:12

问题


I have the following graph, describing co-occurrence of car brands in documents:

CREATE 
  (`0` :Car {value:"Ford"})
, (`1` :Car {value:"Subaru"})
, (`2` :Car {value:"VW"})
, (`0`)-[:`DOCUMENT` {value:"DOC-1"}]->(`1`)
, (`0`)-[:`DOCUMENT` {value:"DOC-2"}]->(`1`)
, (`1`)-[:`DOCUMENT` {value:"DOC-3"}]->(`2`);

If there are many relationships between two nodes - for the purpose of visualization - I want to replace it with a single one and calculate the weight:

VW ---1--- Subaru ---2--- Ford

How can this be achieved?

I tried the following query:

MATCH (n1)-[r1:DOCUMENT]-(n2)
RETURN n1, n2, apoc.create.vRelationship(n1, 'WEIGHT', {weight:count(r1)}, n2);

but this is not the expected result:


回答1:


If the relationship direction is not specified in MATCH (n1)-[r1:DOCUMENT]-(n2) RETURN *;, every relationship is returned twice:

╒══════════════════╤══════════════════╤═════════════════╕
│"n1"              │"n2"              │"r1"             │
╞══════════════════╪══════════════════╪═════════════════╡
│{"value":"Ford"}  │{"value":"Subaru"}│{"value":"DOC-1"}│
├──────────────────┼──────────────────┼─────────────────┤
│{"value":"Ford"}  │{"value":"Subaru"}│{"value":"DOC-2"}│
├──────────────────┼──────────────────┼─────────────────┤
│{"value":"Subaru"}│{"value":"Ford"}  │{"value":"DOC-1"}│
├──────────────────┼──────────────────┼─────────────────┤
│{"value":"Subaru"}│{"value":"Ford"}  │{"value":"DOC-2"}│
├──────────────────┼──────────────────┼─────────────────┤
│{"value":"Subaru"}│{"value":"VW"}    │{"value":"DOC-3"}│
├──────────────────┼──────────────────┼─────────────────┤
│{"value":"VW"}    │{"value":"Subaru"}│{"value":"DOC-3"}│
└──────────────────┴──────────────────┴─────────────────┘

Specify the direction by changing the MATCH clause to either MATCH (n1)-[r1:DOCUMENT]->(n2) or MATCH (n1)<-[r1:DOCUMENT]-(n2). The remaining part of the query is correct.



来源:https://stackoverflow.com/questions/51713072/convert-multiple-relationships-between-2-nodes-to-a-single-one-with-weight

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!