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.

Cypher query for returning paths between start and end node/nodes types.

Hi all. I am writing a query where I want to find all the possible paths between the start node and the end node.

eg: lets say there are 3 paths from start node 'a' to end node 'z' with multiple intermediate nodes and relationships.

Now, if there are 3 paths from a->z (eg: 1. a->p->n->z,
2. a->o->b->r->z,
3. a->z) then I want these 3 paths as the return output of this cypher query. 

I have written the following query and using apoc.path.expandConfig:

MATCH (start:User{Name:"xyz"})
match (end:Resource)
WITH start, collect(end) as tN
CALL apoc.path.expandConfig(start, {
    uniqueness : 'RELATIONSHIP_PATH',
    relationshipFilter : 'CanAccess|BelongsTo>|<isAttachedTo|Uses>'
} )
yield path
return path
 
But this query is returning all the intermediate paths as well. Can I configure the query so it returns only the final paths(3 path objects in the above case).
If not, can someone help me with some alternate query which might be more efficient?
1 REPLY 1

If I understand correctly, you want only those paths that terminate on the 'end' nodes.  If so, you can configure the expandConf to return only those paths that terminate on those nodes.

MATCH (start:User{Name:"xyz"})
match (end:Resource)
WITH start, collect(end) as tN
CALL apoc.path.expandConfig(start, {
    uniqueness : 'RELATIONSHIP_PATH',
    relationshipFilter : 'CanAccess|BelongsTo>|<isAttachedTo|Uses>',
    terminatorNodes: tN
} )
yield path
return path