neo4jbidirectional-relation

Querying single relationship from a bidirectional relationship in Neo4j


Overlapping bidirectional relationship

Is it possible to show only one direction relationship from a bidirectional relationship?

(n)-[:EMAIL_LINK]->(m)

(n)<-[:EMAIL_LINK]-(m)


Solution

  • If the relationship type in question does not have directional semantics, it's best practice to have them only one time in the graph and omit the direction while querying, i.e. (a)-[:EMAIL_LINK]-(b) instead of (a)-[:EMAIL_LINK]->(b).

    To get rid of duplicated relationships in different directions, use:

    MATCH (a)-[r1:EMAIL_LINK]->(b)<-[r2:EMAIL_LINK]-(a)
    WHERE ID(a)<ID(b)
    DELETE r2
    

    if your graph is large you need to take care of having reasonable transaction sizes by adding a LIMIT and running the query multiple times until all have been processed.

    NB: the WHERE ID(a)<ID(b) is necessary. Otherwise a and b might change roles during in a later iteration. Consequently r1 and r2 would change roles as well and both get deleted.