neo4j 查找父节点是否存在关系 [英] neo4j find parent node is relationship exists
问题描述
我正在尝试开发像 Google Drive 这样的产品.现在是第二阶段.我被困在进行密码查询,我需要帮助.
I am trying to develop a product like a google drive. This is the second phase now. I am stuck in making cypher query and I need help.
用户 A 可以与群组或其他用户 B 共享文件/文件夹.
a user A can share a file/folder with a group or another user B.
case 1:用户A,共享了包含其他子文件夹的文件夹.用户 B 将看到共享文件夹.但是,现在用户 A 共享了一个子文件夹,而用户 B 应该只能看到根文件夹.
case 1: user A, shared the folder which consists of other subfolders. user B would see the shared folder. however, now user A shared a subfolder, and again user B should see only the root folder.
案例2:用户A,共享了唯一的子文件夹,那么用户B将只能看到一个共享的子文件夹
case 2: user A, shared the only subfolder then, user B will only see a shared subfolder
基本上我需要从底部遍历节点到它的父节点和它们的父节点,如果有的话,并在具有这种关系的第一个节点处停止
basically I need to traverse the node from bottom to its parent and their parent with CAN_ACCESS_DIR relationship if any and stop at the first node with this relationship
请帮忙
附加图片场景 ->找到JAVA2的根(JAVA)并检查它是否已与同一组或用户共享(CAN_ACCESS_DIR).如果是,那么我们将只发送 JAVA 文件夹.如果没有,则只发送 JAVA2 文件夹
attached image scenario -> find the root (JAVA) of JAVA2 and check if it has been shared (CAN_ACCESS_DIR) with the same group or a user. If yes, then we will send only the JAVA folder. If not then send only JAVA2 folder
match (g:group) where g.name = "group1"
match p = (d:directory { name: "JAVA2"})<-[:LEAF | CONTAINS*0..]-(a:directory) where (d)<-[:CAN_ACCESS_DIR]-(g)
unwind nodes(p) as i
return collect(distinct(i)) as result
match (u:user) where u.name = "John"
match (t:tower) where t.name = "Genome"
match (d:directory) where d.name = "Root"
match (u)-[:MEMBER_OF_GROUP]->(g:group)
match (d)-[:CONTAINS*0..]->(s) where (s)<-[:CAN_ACCESS_DIR]-(g)<-[:MEMBER_OF_GROUP]-(u)
不是neo4j的专家,请指导我.谢谢
not an expert in neo4j, please guide me out. thanks
推荐答案
[UPDATED]
此查询应返回最接近根的组可访问的a
目录,从特定(子)目录开始:
This query should return the group-accessible a
directory that is closest to the root, starting at a specific (sub)directory:
MATCH p=(g:group)-[:CAN_ACCESS_DIR]->(d:directory)<-[:CONTAINS*0..]-(a)
WHERE g.name = "group1" AND d.name = "JAVA2"
MATCH (a)<-[:CAN_ACCESS_DIR]-(g)
RETURN p, a
ORDER BY LENGTH(p) DESC
LIMIT 1
由于单个 MATCH
子句会过滤掉同一关系的多次使用,此查询使用 2 个 MATCH
子句来允许相同的 CAN_ACCESS_DIR
匹配两次的关系,以防 d
是唯一可访问的目录(因此与 a
相同).
Since a single MATCH
clause would filter out multiple uses of the same relationship, this query uses 2 MATCH
clauses to allow the same CAN_ACCESS_DIR
relationship to match twice, in case d
is the only accessible directory (and therefore the same as a
).
这篇关于neo4j 查找父节点是否存在关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!