Cypher 查询以查找与给定列表中至少两个其他用户连接的用户
Cypher query to find users who are connected to at least two other user in given list
我是 neo4j 的终极初学者。我有一个类似于
的密码查询
WITH ["123", "456", "789"] as ids
MATCH (p:user)-[:follower]->(m:user)
WHERE m.id in ids
WITH p, 2 as inputCnt, count(DISTINCT p) as cnt
WHERE cnt = inputCnt
RETURN p
123、456 和 789 是用户 ID。列表长度是动态的,可以大于 3。
我要查找的是 123、456、789 以及在给定列表中至少关注 2 个用户的其他节点。如果一个节点只关注 1 个用户,则不需要。
我从 here 得到了主要思想,但显然那里的问题不同,所以我的查询没有 return 任何结果。我确定我的图表中正好有 1 个节点满足我的条件,所以我应该看到一个包含 4 个节点的结果。
让我举一些例子来说明:
When there are no users following at least 2 of them return:
当没有用户关注其中至少 2 个,但他们相互关注时,return:
When there's a single user, say (000), following 123 and 456 return:
When there are two users, say 000 and XXX, one following all 3 of
them, one following 2 of them, return:
当您说“至少 2 个用户”时,应该是 >= 而不是 =。然后你计算不同的用户 m 后面跟着另一个用户 p1, p2,..,pn.
WITH ["123", "456", "789"] as ids
MATCH (p:user)-[:follower]->(m:user)
WHERE m.id in ids
WITH p, count(DISTINCT m) as cnt where cnt >= 2
RETURN p
如果你想 return 用户 m 然后收集并检查大小。
WITH ["123", "456", "789"] as ids
MATCH (p:user)-[:follower]->(m:user)
WHERE m.id in ids
WITH p, collect(DISTINCT m) as m_users where size(m_users) >= 2
RETURN p, m_users
编辑:
- Do a match of users from the id list
- Using OPTIONAL match, find all followers to m
- Check if the count is >= 2 OR no connection to the list OR connection within the list
- Return distinct followers p and users m
WITH [ "222" , "333", "789"] as ids
MATCH (m:user) WHERE m.ID in ids
WITH collect(m) as ms
OPTIONAL MATCH (p:user)-[:follower]->(m) WHERE m in ms
WITH p, ms, collect(DISTINCT m) as m_users
WHERE size(m_users) >= 2 OR p is null OR p in ms
WITH p, ms + m_users as allUsers
UNWIND allUsers as m
RETURN distinct p, m
查看以下结果:
我是 neo4j 的终极初学者。我有一个类似于
的密码查询WITH ["123", "456", "789"] as ids
MATCH (p:user)-[:follower]->(m:user)
WHERE m.id in ids
WITH p, 2 as inputCnt, count(DISTINCT p) as cnt
WHERE cnt = inputCnt
RETURN p
123、456 和 789 是用户 ID。列表长度是动态的,可以大于 3。
我要查找的是 123、456、789 以及在给定列表中至少关注 2 个用户的其他节点。如果一个节点只关注 1 个用户,则不需要。
我从 here 得到了主要思想,但显然那里的问题不同,所以我的查询没有 return 任何结果。我确定我的图表中正好有 1 个节点满足我的条件,所以我应该看到一个包含 4 个节点的结果。
让我举一些例子来说明:
When there are no users following at least 2 of them return:
当没有用户关注其中至少 2 个,但他们相互关注时,return:
When there's a single user, say (000), following 123 and 456 return:
When there are two users, say 000 and XXX, one following all 3 of them, one following 2 of them, return:
当您说“至少 2 个用户”时,应该是 >= 而不是 =。然后你计算不同的用户 m 后面跟着另一个用户 p1, p2,..,pn.
WITH ["123", "456", "789"] as ids
MATCH (p:user)-[:follower]->(m:user)
WHERE m.id in ids
WITH p, count(DISTINCT m) as cnt where cnt >= 2
RETURN p
如果你想 return 用户 m 然后收集并检查大小。
WITH ["123", "456", "789"] as ids
MATCH (p:user)-[:follower]->(m:user)
WHERE m.id in ids
WITH p, collect(DISTINCT m) as m_users where size(m_users) >= 2
RETURN p, m_users
编辑:
- Do a match of users from the id list
- Using OPTIONAL match, find all followers to m
- Check if the count is >= 2 OR no connection to the list OR connection within the list
- Return distinct followers p and users m
WITH [ "222" , "333", "789"] as ids
MATCH (m:user) WHERE m.ID in ids
WITH collect(m) as ms
OPTIONAL MATCH (p:user)-[:follower]->(m) WHERE m in ms
WITH p, ms, collect(DISTINCT m) as m_users
WHERE size(m_users) >= 2 OR p is null OR p in ms
WITH p, ms + m_users as allUsers
UNWIND allUsers as m
RETURN distinct p, m
查看以下结果: