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;

这个查询返回我所有以'dh'开头的用户现在,我想要我的登录用户和这个搜索用户之间的友谊状态。所以,我这样做了如下:

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 应该返回所有记录,加入 2nd & 3rd 结果集。

我该怎么做呢?

4

1 回答 1

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.

于 2013-07-29T20:00:29.180 回答