2

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?

Gökhan Çoban
  • 600
  • 8
  • 17

2 Answers2

2

Why don't you use a Cypher query? The library has this possibility. Just use the cypher query function (see their doc for the exact name, I don't remember) and then use this query:

START n=node(_id1), m=node(_id2) MATCH n-[rel:RELATIONSHIP_TYPE]-m RETURN DISTINCT rel;

where _id1 and _id2 are the internal ids of the nodes in Neo4J

If you use Neo4J 2.0 (highly recommended), your query would look something like:

MATCH (n{id:"_id1"}), (m{id:"_id2"}), n-[rel:RELATIONSHIP_TYPE]-m RETURN DISTINCT rel;

in that latter case you could actually use any property in the place of id - for example, your own unique id for the node, or their names, etc.

In the end Neo4J is explicitly saying they're moving away from REST API towards Cypher, so it makes sense to use Cypher where possible and it's also more precise what you get like that.

Aerodynamika
  • 7,883
  • 16
  • 78
  • 137
  • I am using Neo4j embedded in Java due to performance issues by the help of Core API or Traversal API. – Gökhan Çoban Feb 16 '14 at 21:47
  • @GökhanÇoban which makes it impossible to use Cypher? :) – Aerodynamika Feb 16 '14 at 21:54
  • 1
    @GökhanÇoban - You should note that the article you referenced is over a year old, and Neo4j v2 has many performance improvements, including Cypher improvements. You may want to do some benchmarking of your own, along with updated v2 Cypher constructs (such as labels) to see if you get decent performance w/Cypher. – David Makogon Feb 17 '14 at 14:02
1

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;
}
Michael Hunger
  • 41,339
  • 3
  • 57
  • 80