按节点深度排序neo4j cypher查询

时间:2017-09-20 13:53:15

标签: graph neo4j cypher

我有以下图表:

graph

我想通过CAN_DISTRIBUTE Credits Role APPLIES_ON返回所有拥有Group sara权限的用户。

以下查询将admin MATCH (users)-[:IS]->()<-[:CHILD_OF*0..]-(roles)-[:CAN_DISTRIBUTE]->(asset:Asset{name:"Credits"}), (roles)-[:APPLIES_ON]->(group:Group{name:"Digital"}) WITH DISTINCT users RETURN collect(users.name) 作为用户名返回:

Role

现在,我很难按其sara关系深度订购退回的用户。我希望首先返回Manager,因为SuperManager角色是 CREATE (admin:User{name:"admin"}) CREATE (sara:User{name:"sara"}) CREATE (c:Asset{name:"Credits"}) CREATE (marketing:Group{name:"Marketing"}) CREATE (digital:Group{name:"Digital"}) CREATE (super_manager:Role{name:"SuperManager"}) CREATE (manager:Role{name:"Manager"}) CREATE (manager)-[:CAN_DISTRIBUTE]->(c) CREATE (admin)-[:IS]->(super_manager) CREATE (sara)-[:IS]->(manager) CREATE (super_manager)-[:APPLIES_ON]->(marketing) CREATE (super_manager)-[:APPLIES_ON]->(digital) CREATE (manager)-[:APPLIES_ON]->(marketing) CREATE (manager)-[:APPLIES_ON]->(digital) CREATE (manager)-[:CHILD_OF]->(super_manager) 的孩子。 在英语中,它就像是说,给我所有可以在X组上分配信用的用户,按照他们的角色层次排序。

你们有什么想法吗?

以下是创建此图表的查询:

{{1}}

1 个答案:

答案 0 :(得分:1)

你可以使用整个路径的长度来做到这一点:

MATCH p = (users)-[:IS]->()<-[:CHILD_OF*0..]-(roles)-[:CAN_DISTRIBUTE]->(asset:Asset{name:"Credits"}),
      (roles)-[:APPLIES_ON]->(group:Group{name:"Digital"})
WITH DISTINCT users, length(p) as pathLength
RETURN users.name
ORDER BY pathLength

给定数据集的输出:

╒════════════╕
│"users.name"│
╞════════════╡
│"sara"      │
├────────────┤
│"admin"     │
└────────────┘