简体   繁体   中英

Neo4j: optional relationship in cypher query?

I think i don't quite understand how optional relationships work in cypher queries.

Sample data can be found here http://console.neo4j.org/?id=qnyvxb

My insitution-[:PERMISSION]->My institution document
Other insitution-[:PERMISSION]->Other institution document
Parent institution document-[:PERMISSION]->Parent institution document
My Institution-[:ATTACHED_TO]->Parent institution<-[:ATTACHED_TO]-[Other institution
Super user-[:MEMBER_OF]->My Institution

I need to get documents that Super user can access. The query i have used:

START member=node(7) 
MATCH (member)-[m:MEMBER_OF]->()-[?:ATTACHED_TO*..5]->()-[p:PERMISSION]->(documents) 
RETURN documents.name

But it only returns "Parent institution document". I'm i getting the optional relationship concept wrong? Should i use two queries?

Thanks!

You don't need optional relationships to solve this one. Try:

CYPHER 1.9  START member=node(7) 
MATCH member-[:MEMBER_OF]->inst-[:ATTACHED_TO*0..5]-otherinst-[:PERMISSION]->doc 
RETURN doc.name

Note the 0..5 as this includes the first inst found off the member also when looking for doc . I should also mention that I have made the attached and permission links multi directional by removing the > , as your given ATTACHED_TO relationships don't chain in any particular order.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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