2011-06-12 123 views

回答

4

最簡單的方法是在FRIEND_OF關係中使用長度爲2的最短路徑算法和兩個用戶。這些是通過恰好一個朋友跳來連接兩個用戶的路徑。

PathFinder<Path> finder = GraphAlgoFactory.shortestPath(
     Traversal.expanderForTypes(FRIEND_OF), 2); 
Iterable<Path> paths = finder.findAllPaths(user1, user2); 
4

在使用暗號的情況下,以下查詢返回共同的朋友:

start a = node(1), b = node(4) match (a)--(x)--(b) return x; 

上面的示例返回節點1的共同的朋友和4

enter image description here

下面是複製查詢及其結果示例如下:

neo4j-sh (0)$ start a = node(1), b = node(4) match (a)--(x)--(b) return x; 
==> +--------------------+ 
==> | x     | 
==> +--------------------+ 
==> | Node[3]{Name->"C"} | 
==> +--------------------+ 
==> 1 row 
==> 9 ms 
==> 
neo4j-sh (0)$ start a = node(1), b = node(6) match (a)--(x)--(b) return x; 
==> +--------------------+ 
==> | x     | 
==> +--------------------+ 
==> | Node[5]{Name->"E"} | 
==> | Node[2]{Name->"B"} | 
==> +--------------------+ 
==> 2 rows 
==> 0 ms 
+0

如果我們在開始時不知道'b'節點,我們可以使用'START a = node(1)MATCH a - x - b WHERE a - b RETURN x' – ulkas 2014-07-24 08:10:57