试图在cypher中找到连接最多的节点

时间:2013-11-27 09:14:04

标签: neo4j

我正在使用NEO4J 2.0 M6,我正在尝试找到连接最多的节点,并按其连接降序对它们进行排序。我从很多其他帖子中尝试了许多片段,但没有成功。

我的数据很简单:

create (Account1 { id:123}),
    (Account2 { id:456}),
    (Account3 { id:789}),
    (Account4 { id:101}),
    (PERMISSION1 { name: 'ChangeOneThing'}),
    (PERMISSION2 { name: 'ChangeAnotherThing'}),
    (PERMISSION3 { name: 'ConsumeThePlanet'}),
    (Account1)-[:ADDED]->(PERMISSION1),
    (Account1)-[:ADDED]->(PERMISSION2),
    (Account2)-[:ADDED]->(PERMISSION2),
    (Account4)-[:ADDED]->(PERMISSION2),
    (Account3)-[:REMOVED]->(PERMISSION3)

我需要的结果如下所示,因为我正在尝试确定哪些是最新添加的权限,以便在新系统中创建分组。

PermissionName       Count
==========================
ChangeAnotherThing   3
ChangeOneThing       1

这将允许我确定已分配给帐户的最常用权限组,这将有助于我将当前无限自定义分配简化为小组。

我对cypher很新,这是我尝试让它工作的原因:

match (account)-[:ADDED]->(permission)<-[:ADDED]-(other_account)
return count(permission) asscore, collect(permission.name) as permissions
order by score desc

但那只是给了我:

6     ChangeAnotherThing, ChangeAnotherThing, ChangeAnotherThing, ChangeAnotherThing, ChangeAnotherThing, ChangeAnotherThing

1 个答案:

答案 0 :(得分:0)

如果我理解正确,您需要以下内容:获取每个权限,找到已添加权限的所有帐户并计算它们以查看权限的使用次数。最简单的方法是在创建图表时标记您的帐户和权限,例如

CREATE (acct1:Account {id:123}), (acct2:Account {id:456}), 
    (acct3:Account {id:789}), (acct4:Account {id:101}),
    (perm1:Permission {name:'ChangeOneThing'}),
    (perm2:Permission {name:'ChangeAnotherThing'}),
    (perm3:Permission {name:'ConsumeThePlanet'}),
    (acct1)-[:ADDED]->(perm1), (acct1)-[:ADDED]->(perm2),
    (acct2)-[:ADDED]->(perm2), (acct4)-[:ADDED]->(perm2),
    (acct3)-[:REMOVED]->(perm3)

然后像这样查询

MATCH (permission:Permission)<-[:ADDED]-(account:Account) 
RETURN permission.name, COUNT(account) AS score 
ORDER BY score DESC

当您返回a, count(b) a成为分组键时,您不必对权限进行计数或分组 - 每个a获得一行,b的总值为。