Neo4j - 相互计数的搜索列表 [英] Neo4j - Search list with mutual count
问题描述
我在neo4j中创建了一些节点和关系,想用cypher查询.我将在下面详细解释.
I have created some nodes and relations in neo4j and want to query with cypher. I am explaining more about it as below.
UserID UserName
------ --------
1 UserA
2 UserB
3 UserC
4 UserD
5 UserE
6 UserF
和节点之间的关系如下:
and relationship between nodes are as follows :
UserID FriendID ApprovalStatus (1.Request Accepted, 2.Request Pending)
------ -------- ------------------------------------------------------
1 2 1
1 3 2
1 6 2
2 3 1
2 4 1
2 5 2
3 6 1
3 5 2
我的登录用户是节点 1(例如 UserA),并尝试从节点进行搜索.我期待着 Neo4j 的这个结果.
My Login User is node 1 (eg. UserA), and trying to search from node. and I am expecting this result from neo4j.
Record # UserID UserName MutualCount ApprovalStatus
-------- ------ -------- --------------- --------------
1 2 UserB 1 (eg. node 3) 1
2 3 Userc 0 2
3 4 UserD 0 null
4 5 UserE 0 null
5 6 UserF 0 2
检查以下几点:记录#1:Node3 (UserC) 在 Node1 & 之间是相互的.Node2 与因为它有 ApprovalStatus=1 与两个节点.
check the following points : Record # 1 : Node3 (UserC) is mutual between Node1 & Node2 with because it has ApprovalStatus=1 with both nodes.
记录#2:
node1 & 之间没有相互关系.node3,并且 ApprovalStatus = 2 因为 Node1 已经向 node3 发送了请求,但它尚未处理.
Record # 2 :
There is no mutual between node1 & node3, and ApprovalStatus = 2 because Node1 has sent request to node3, but it is pending yet.
记录#3:
与记录#2 中提到的情况相同
Record # 3 :
Same situation as mentioned in Record # 2
记录#4 &5:
这里没有 node1 & 之间的相互关系.node4,并且 ApprovalStatus = null 因为 Node1 从未向 node4 & 发送请求节点5.
Record # 4 & 5:
here is no mutual between node1 & node4, and ApprovalStatus = null because Node1 has never sent request to node4 & node5.
我在这里
因此,您可以测试查询.我试图从过去 10-15 天获得这个结果,但我无法获得成功.有什么办法可以达到这个结果.
So, you can test query. I am trying to get this result from last 10-15 days, but I can not get success. Is there any way to achieve this result.
谢谢.
推荐答案
你问题中的关系表没有任何相互关系,这就是你要找的样子,所以我创建了一个非常相似的例子,增加了从 B 到 A 的额外关系.
The relationship table in your question doesn't have any mutual relationships, which is what it looks like you are looking for, so I created a very similar example that adds an additional relationship from B to A.
我在 :FRIEND
关系中添加了状态accepted"和requested",但正如@Stefan 在评论中提到的那样,使用不同的关系类型会更容易,例如 :REQUESTED_FRIEND
和 :FRIEND
来区分两者.在这种情况下,您可以从以下查询中删除 WHERE 子句:
I added statuses "accepted" and "requested" to the :FRIEND
relationships, but as @Stefan mentions in the comments, it would be easier to use different relationship types such as :REQUESTED_FRIEND
and :FRIEND
to distinguish between the two. In that case you could drop the WHERE clause from the following query:
START n=node(*)
MATCH n-[r:FRIEND]->m, m-[r2:FRIEND]->n
WHERE r.status='accepted' AND r2.status='accepted'
RETURN n, COUNT(m) AS MutualCount, COLLECT(m.name)
返回:
n MutualCount MutualWith
(5 {name:"B"}) 1 [A]
(6 {name:"A"}) 1 [B]
这篇关于Neo4j - 相互计数的搜索列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!