我是Cypher的新手,我正在努力学习如何正确导航图形。 我有这样的情况:2用户已经关联了相同的服务,该服务可以通过帐户访问。 因此,用户'usr01'可以通过账户'acct01'访问服务'srv01'; 用户'usr02可以使用账户'acct02'访问服务'srv01'。 目标是提取2个这样的记录:
usr01 - srv01 - acct01 usr02 - srv01 - acct02所以,我执行了这些查询:
创建节点:
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;建立关系:
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)我收到的图表结果是这样的
如果我执行这个查询 - 从用户usr01开始:
MATCH (u:XUser {userId: 'usr01'}) OPTIONAL MATCH (u)-[:HAS_SERVICE]->(s:XService) OPTIONAL MATCH (s)-[:HAS_ACCOUNT]->(a:XAccount)返回u.userId,s.serviceId,a.accountId;
我得到这个结果:
那么,我如何才能获得上述结果(usr01 - srv01 - acct01),而不是我收到的笛卡尔产品? 提前致谢
I'm new to Cypher, I'm trying to learn to navigate a graph correctly. I hava a situation like this: 2 Users have associated the same Service, the service is accessible via an Account. So, the user 'usr01' can access to the Service 'srv01' with account 'acct01'; the user 'usr02 can access to the Service 'srv01' with account 'acct02'. The aim is to extract 2 records like this:
usr01 - srv01 - acct01 usr02 - srv01 - acct02So, 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:
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;
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? Thanks in advance
最满意答案
问题是,当您添加服务和帐户之间的关系时,您不指示与用户的关联关系。 作为解决方案,您可以创建一个智能节点“访问规则”:
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)和一个查询:
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, paThe 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更多推荐
发布评论