Cypher:如何导航图以找到正确的路径(Cypher: how to navigate graph to find the right path)

编程入门 行业动态 更新时间:2024-10-25 11:23:20
Cypher:如何导航图以找到正确的路径(Cypher: how to navigate graph to find the right path)

我是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 - 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:

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, pa

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

更多推荐

本文发布于:2023-07-22 10:04:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1219206.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:路径   正确   Cypher   navigate   path

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!