Neo4j-相互计数的搜索列表 [英] Neo4j - Search list with mutual count

查看:92
本文介绍了Neo4j-相互计数的搜索列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在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: 节点3(用户C)在节点1和节点2之间是相互的.因为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关系中添加了已接受"和已请求"状态,但是正如@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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆