cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

apoc.path.spanningTree - Return Single Node

I have a graph that represents a file system. The purpose of the code below is to take any file paths passed in as parameters, match them to nodes in the graph, then find all of the their children. One of the file paths in the parameter "filepaths" represents a dir, making it possible to have children. the other file path represents a file, which never has children. The dir actually has children, so the call to apoc.path.spanningTree returns a path that I can use for further processing. Conversely, the file does not have children, so the call to apoc.path.spanningTree does not return a result, stopping the stream of records and halting execution for that filepath. In the end i get results for the dir and i get nothing for the file. Instead, I would like to get results for both. For the dir I would like a path that contains the dir and its children. For the file, I would like a path that simply contains the file. If the file were returned in path, then I could still do further processing with the file. Is there something I can do get the behavior I want?

with ['/portal/src/config', '/portal/.github/workflows/cd.yml'] as filepaths
    match (project:Project) 
        where project.name = 'Portal'
    match (project)-[:COMPRISES]->(root:FileObject)
    match (leaf:FileObject)
        where not exists ((leaf)-[:COMPRISES]->(:FileObject)) 
        and leaf.path =~ '^' + root.path + '/.+'
with project, root, collect(leaf) as leaves, filepaths
unwind filepaths as filePath
    match (parent:FileObject) where parent.path = filePath
    call apoc.path.spanningTree(parent, {
        labelFilter: '+FileObject',
        relationshipFilter: '>COMPRISES',
        terminatorNodes: leaves
    }) yield path
...more code below
1 REPLY 1

looks like this does the trick

apoc.path.spanningTree(parent, {
    . . .
    filterStartNode: true
})