Search code examples
neo4jcypherneo4j-apoc

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


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


Solution

  • 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.