Search code examples
neo4jnode-neo4j

Recommended way of getting single relationship of specific type between two nodes


Although title is clear,

I need to remove a relationship between two nodes of a specific relationship type. Neither getSingleRelationship function of Node nor overloaded versions of getRelationships have second node parameter.

Should I get all relationships and iterate over it to find relationship? Is there any constant time way?

What is the recommended way in Core API or Traversal API?


Solution

  • Yes you would iterate over the relationships and check the end-node:

    public Relationship getRelationshipBetween(Node start, Node end, Direction direction, RelationshipType type) {
        for (Relationship r: start.getRelationships(direction,type)) {
           if (r.getOtherNode(start).equals(end)) return r;
        }
        return null;
    }