Initial situation
- Rooted Tree, representing a directory like structure
- root node
- no cycles
- non binary
- directory names are not unique
- directories are modeled, but no „files“
- graph is connected
- graph is directed from root to leaves
- Model
(:Root)-[:CONTAINS]->(:Directory)-[:CONTAINS*]->(:Directory)
- Neo4j 3.5.11
- about 20 levels deep
CREATE
(root:Root {name:'Root'}),
(dirA:Directory {name:'dir A'}),
(dirB:Directory {name:'dir B'}),
(dirC:Directory {name:'dir C'}),
(dirD:Directory {name:'dir D'}),
(dirE:Directory {name:'dir E'}),
(dirF:Directory {name:'dir F'}),
(dirG:Directory {name:'dir G'}),
(root)-[:CONTAINS]->(dirA),
(root)-[:CONTAINS]->(dirB),
(dirA)-[:CONTAINS]->(dirC),
(dirA)-[:CONTAINS]->(dirD),
(dirD)-[:CONTAINS]->(dirE),
(dirD)-[:CONTAINS]->(dirF),
(dirD)-[:CONTAINS]->(dirG);
- level of freedom
:Rootlabel can be modeled also as(:Directory name:’Root’)if necessary- the apoc library is highly welcomed
Given input parameter
- row list of directly linked directory names
- varying amount of directories
- adjacent directories of the breadcrumb string are also directly linked in the tree / graph
example:
WITH 'dir A/dir D/dir G' as inputString
WITH split(inputString, '/') AS directories
UNWIND
directories AS directory
RETURN
directory;
╒═══════════╕
│"directory"│
╞═══════════╡
│"dir A" │
├───────────┤
│"dir D" │
├───────────┤
│"dir G" │
└───────────┘
Challenge to be solved
For a specified breadcrumb string ("dir A/dir D/dir G") I need its representing path in Cypher, which will be part of a more complex query. I can not just search the tree for the last directory entry ("dir G") of the breadcrumb, because the directory names are not unique. How can my request be realized in Cypher?
Expected result:
╒═══════════════════════════════════════════════════════════════════════════════════════════════════════════════╕
│"path" │
╞═══════════════════════════════════════════════════════════════════════════════════════════════════════════════╡
│[{"name":"Root"},{},{"name":"dir A"},{"name":"dir A"},{},{"name":"dir D"},{"name":"dir D"},{},{"name":"dir G"}]│
└───────────────────────────────────────────────────────────────────────────────────────────────────────────────┘
