This will tell you if there is a connection:
Node from_node = index.get("guid", "user_a").getSingle(); Node to_node = index.get("guid", "user_b").getSingle(); if(from_node != null && to_node != null) { RelationshipExpander expander = Traversal.expanderForAllTypes(Direction.BOTH); PathFinder<Path> finder = GraphAlgoFactory.shortestPath(expander, 2); if(finder.findSinglePath(from_node, to_node) != null) { //Connected by at least 1 common friend } else { //Too far apart or not connected at all } }
This will tell you who are mutual friends:
Node from_node = index.get("guid", "user_a").getSingle(); Node to_node = index.get("guid", "user_b").getSingle(); if(from_node != null && to_node != null) { RelationshipExpander expander = Traversal.expanderForAllTypes(Direction.BOTH); PathFinder<Path> finder = GraphAlgoFactory.shortestPath(expander, 2); Iterable<Path> paths = finder.findAllPaths(from_node, to_node); if(paths != null) { for(Path path : paths) { Relationship relationship = path.relationships().iterator().next(); Node friend_of_friend = relationship.getEndNode(); } } else {
This code is a little rough and much easier to express in Cypher (taken from the Cheet Sheet in the Neo4J server console (a great way to play with Neo4J after filling the database):
START a = (user, name, "user_a") MATCH (a)-[:FRIEND]->(friend)-[:FRIEND]->(friend_of_friend) RETURN friend_of_friend
This will give you a list of nodes shared between other disconnected nodes. You can pass this request to the embedded server by counting the CypherParser class.
source share