Search code examples
neo4jcypherneo4j-apoc

Neo4J apoc.create.vRelationship unique relationsip


A few months back I asked neo4j return a relationship that isn't defined and the answer was perfect.

MATCH (p:part)<-[:SUPPLIES]-(:supplier)-[:LOCATED_IN]->(c:country)
RETURN p, c, apoc.create.vRelationship(p, 'IS_IN', {}, c) as rel

The problem I am having with the above solution is I now have two nodes with lots of the exact same relationship type between them. Is there a way to return a distinct relationship based on it's type?


Solution

  • You can use WITH DISTINCT p, c to filter out duplicate p and c pairs, so that there will only be a single virtual relationship between each pair:

    MATCH (p:part)<-[:SUPPLIES]-(:supplier)-[:LOCATED_IN]->(c:country)
    WITH DISTINCT p, c
    RETURN p, c, apoc.create.vRelationship(p, 'IS_IN', {}, c) as rel