[英]How to match all possible paths through multiple relationship types with filtering (Neo4j, Cypher)
I am new to Neo4j and I have a relatively complex (but small) database which I have simplified to the following: 我是Neo4j的新手,我有一个相对复杂(但很小)的数据库,已将其简化为以下内容:
The first door has no key, all other doors have keys, the window doesn't require a key. 第一扇门没有钥匙,其他所有门都有钥匙,窗户不需要钥匙。 The idea is that if a person has key:'A'
, I want to see all possible paths they could take. 这个想法是,如果一个人有key:'A'
,我想看看他们可以走的所有可能路径。
Here is the code to generate the db 这是生成数据库的代码
CREATE (r1:room {name:'room1'})-[:DOOR]->(r2:room {name:'room2'})-[:DOOR {key:'A'}]->(r3:room {name:'room3'})
CREATE (r2)-[:DOOR {key:'B'}]->(r4:room {name:'room4'})-[:DOOR {key:'A'}]->(r5:room {name:'room5'})
CREATE (r4)-[:DOOR {key:'C'}]->(r6:room {name:'room6'})
CREATE (r2)-[:WINDOW]->(r4)
Here is the query I have tried, expecting it to return everything except for room6
, instead I have an error which means I really don't know how to construct the query. 这是我尝试过的查询,期望它返回除room6
以外的room6
,相反,我有一个错误,这意味着我真的不知道如何构造查询。
with {key:'A'} as params
match (n:room {name:'room1'})-[r:DOOR*:WINDOW*]->(m)
where r.key=params.key or not exists(r.key)
return n,m
To be clear, I don't need my query debugged so much as help understanding how to write it correctly. 需要明确的是,我不需要调试查询就可以帮助理解如何正确地编写查询。
Thanks! 谢谢!
This should work for you: 这应该为您工作:
WITH {key:'A'} AS params
MATCH p=(n:room {name:'room1'})-[:DOOR|WINDOW*]->(m)
WHERE ALL(r IN RELATIONSHIPS(p) WHERE NOT EXISTS(r.key) OR r.key=params.key)
RETURN n, m
With your sample data, the result is: 使用您的样本数据,结果是:
╒════════════════╤════════════════╕
│"n" │"m" │
╞════════════════╪════════════════╡
│{"name":"room1"}│{"name":"room2"}│
├────────────────┼────────────────┤
│{"name":"room1"}│{"name":"room3"}│
├────────────────┼────────────────┤
│{"name":"room1"}│{"name":"room4"}│
├────────────────┼────────────────┤
│{"name":"room1"}│{"name":"room5"}│
└────────────────┴────────────────┘
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.