简体   繁体   English

Cypher:如何导航图以找到正确的路径

[英]Cypher: how to navigate graph to find the right path

I'm new to Cypher, I'm trying to learn to navigate a graph correctly. 我是Cypher的新手,我正在努力学习如何正确地导航图形。 I hava a situation like this: 2 Users have associated the same Service, the service is accessible via an Account. 我有这样的情况:2用户已关联相同的服务,该服务可通过帐户访问。 So, the user 'usr01' can access to the Service 'srv01' with account 'acct01'; 因此,用户'usr01'可以使用帐户'acct01'访问服务'srv01'; the user 'usr02 can access to the Service 'srv01' with account 'acct02'. 用户'usr02可以使用帐户'acct02'访问服务'srv01'。 The aim is to extract 2 records like this: 目的是提取2条记录,如下所示:

usr01 - srv01 - acct01
usr02 - srv01 - acct02

So, I executed these queries: 所以,我执行了这些查询:

  • Creation of nodes: 创建节点:

     create (s:XService {serviceId:'srv01'}) return s; create (u:XUser {userId:'usr01'}) return u; create (u:XUser {userId:'usr02'}) return u; create (u:XAccount {accountId:'acct01'}) return u; create (u:XAccount {accountId:'acct02'}) return u; 
  • Relationship creation: 关系创造:

     MATCH (u:XUser{userId:'usr01'}), (s:XService {serviceId:'srv01'}), (a:XAccount {accountId:'acct01'}) CREATE (u)-[:HAS_SERVICE]->(s)-[:HAS_ACCOUNT]->(a) MATCH (u:XUser{userId:'usr02'}), (s:XService {serviceId:'srv01'}), (a:XAccount {accountId:'acct02'}) CREATE (u)-[:HAS_SERVICE]->(s)-[:HAS_ACCOUNT]->(a) 

The graph result I've received is this 我收到的图表结果就是这个

If I execute this query - starting from the user usr01: 如果我执行此查询 - 从用户usr01开始:

MATCH (u:XUser {userId: 'usr01'}) OPTIONAL MATCH (u)-[:HAS_SERVICE]->(s:XService) OPTIONAL MATCH (s)-[:HAS_ACCOUNT]->(a:XAccount) 

RETURN u.userId, s.serviceId, a.accountId; 返回u.userId,s.serviceId,a.accountId;

I obtain this result: 我得到了这个结果:

So, how can I do to obtain the result described above (usr01 - srv01 - acct01) and not the cartesian product that I've received? 那么,我怎样才能获得上述结果(usr01-srv01-acct01)而不是我收到的笛卡儿积? Thanks in advance 提前致谢

The problem is that when you add the relationship between the service and the account you do not indicate an association relationship with the user. 问题是,当您添加服务和帐户之间的关系时,您不指示与用户的关联关系。 As a solution, you can make a smart node "Access Rule": 作为解决方案,您可以创建智能节点“访问规则”:

MERGE (s:XService {serviceId:'srv01'})

MERGE (u1:XUser {userId:'usr01'})
MERGE (ua1:XAccount {accountId:'acct01'})
MERGE (u1)-[:can_access]->(ca1:AccessRule)-[:to_service]->(s)
MERGE (ca1)-[:with_account]->(ua1)

MERGE (u2:XUser {userId:'usr02'})
MERGE (ua2:XAccount {accountId:'acct02'})
MERGE (u2)-[:can_access]->(ca2:AccessRule)-[:to_service]->(s)
MERGE (ca2)-[:with_account]->(ua2)

And a query: 一个查询:

MATCH (u:XUser {userId: 'usr01'})
OPTIONAL MATCH ps = (u)-[:can_access]->(ca1:AccessRule)-[:to_service]->(s:XService)
OPTIONAL MATCH pa = (ca1)-[:with_account]->(a:XAccount)
RETURN u, ps, pa

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM