2013-07-29 73 views
0

我希望我的朋友與友誼狀態的相互數。我爲每個用戶創建了節點,並創建了它們之間屬性的關係。根據以下查詢,我找到了我想要的結果。在這個測試用例中,我的登錄用戶ID = 1,我想搜索那些從字母'dh'開始的用戶。所以,我的查詢如下。加入結果集

1st Query : which is returned all users with specific given keyword. 
-------------------------------------------------------------------- 
START other=node(*) 
WHERE other.FirstName =~ "(?i)dh.*" AND other.UserID <> 1 
WITH other, me=node:node_auto_index(UserID = '1') 
RETURN other.UserID, other.UserName, other.FirstName, other.LastName, other.ImagePath 
LIMIT 100; 

該查詢返回了我所有的用戶開始使用「衛生署」 現在,我想我的登錄用戶之間友誼的地位和本搜索用戶。所以,我做這個如下:

2nd Query : which is returned approvalstatus between user1 and other 
-------------------------------------------------------------------- 
START me=node:node_auto_index(UserID = '1') 
MATCH me-[myR:friends]-(x) 
RETURN x.UserID, myR.ApprovalStatus 
ORDER BY x.UserID 

最後,我需要按照下面的查詢用戶1和其他人之間的共同好友數。

3rd Query : which is returned mutual count between user1 and other 
------------------------------------------------------------------ 
START me=node:node_auto_index(UserID = '1'), other=node(*) 
MATCH pMutualFriends=me-[r:friends]-mf-[r1:friends]-other 
WHERE r.ApprovalStatus = 1 
AND r1.ApprovalStatus = 1 
AND other.FirstName =~ "(?i)dh.*" AND other.UserID <> 1 
RETURN other.UserID, COUNT(pMutualFriends) AS mutualCount 
ORDER BY other.UserID 

現在我想加入所有這個查詢,就像我們在RDBMS中一樣。表示結果集1st應該返回所有記錄,並加入第二個結果集&。

我該怎麼做?

回答

2

當你查詢一個圖形數據庫時,你應該首先從那裏知道並從圖上向外工作的一段特定數據。使用START n=node(*) ...非常昂貴:您將整個列表返回到整個用戶圖表。但是,這不是您想要的,因爲您只需要那些與UserID = 1相連的用戶。

START me=node:node_auto_index(UserID = '1') 
MATCH me-[r:FRIENDS]-friend-[r1:FRIENDS]-other 
WHERE other.FirstName =~ "(?i)dh.*" AND other.UserID <> 1 
    AND r.ApprovalStatus = 1 AND r1.ApprovalStatus = 1 
    AND NOT (me-[:FRIENDS]-> other) 
RETURN other.UserID, other.FirstName, COUNT(friend) AS MutualCount 

此發現的朋友(other)誰擁有與dh開始遞增計數,他們與me分享共同的朋友的數量名字的所有朋友。

我還添加了條款AND NOT (me-[:FRIENDS]-> other)刪除other的情況也是me的朋友。

+0

謝謝@愛德華,您的查詢看起來對我非常有用。但有一件事是不存在的。我需要以'dh'開頭的所有節點。這裏在你的查詢中只返回連接到用戶1的那些節點,而我也想從關係中獲得「ApprovalStatus」屬性。你能幫我取得理想的結果嗎? –

+0

嗨@edward,我們可以用userid加入3個結果集嗎?像RDBMS一樣加入。所以,所有節點都來自結果1,結果2有'ApprovalStatus',結果3來自'MutualCount'。你明白我的意思了嗎?等待你的回覆。謝謝。 –

+2

我正在努力理解您所要求的數據問題。它是「查找所有名稱以'dh'開頭的用戶,並將他們與朋友分享的每個人的共同朋友計數返回給他們。」? – Edward