Cool, Mattias.  I just ran into that requirement again today.

This is also another good case for the cached/persisted "number of
relationships" counter on a node that has also been discussed. It would
enable the optimization to occur automatically.

Another API function that would be useful (though it is also easy to
implement) is one that automatically deletes all relationships for a node,
then deletes the node, since that is the required path of action anyway to
delete a node.



-----Original Message-----
From: user-boun...@lists.neo4j.org [mailto:user-boun...@lists.neo4j.org] On
Behalf Of Mattias Persson
Sent: Sunday, April 25, 2010 3:52 PM
To: Neo4j user discussions
Subject: [Neo] Checking if relationship exists between two nodes

As you all (probably) know the Neo4j API has no method
Node#hasRelationshipTo(Node otherNode) and to find out such a thing could
potentially be slow if both the nodes has many relationships (of the
type/direction you want to check).

I just created a utility for that which starts to iterate from the node (of
the two) that the developer thinks/knows has the least relationships. If a
certain amount of relationships have been iterated and not yet found a new
thread is spawned which starts to iterate from the other node in the
opposite direction. The method will return as soon as the first thread finds
a match. I'd guess that's the fastest way currently to do that. The next
level of this would be to add indexing for relationships (which is an
upcoming feature) and go via that instead.

So the code may look like:

(it's in the neo4j-utils component)

  GraphDatabaseUtil graphDbUtil = new GraphDatabaseUtil( graphDb );
  boolean exists = graphDbUtil.relationshipExistsBetween( node1, node2,
      MyRelTypes.MY_TYPE, Direction.OUTGOING );

-- 
Mattias Persson, [matt...@neotechnology.com]
Hacker, Neo Technology
www.neotechnology.com
_______________________________________________
Neo mailing list
User@lists.neo4j.org
https://lists.neo4j.org/mailman/listinfo/user

_______________________________________________
Neo mailing list
User@lists.neo4j.org
https://lists.neo4j.org/mailman/listinfo/user

Reply via email to