Convert multiple relationships between 2 nodes to

2019-09-04 03:42发布

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`);

source graph

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:

query graph - wrong result

1条回答
老娘就宠你
2楼-- · 2019-09-04 04:15

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.

查看更多
登录 后发表回答