neo4jcypherpattern-matchingshortest-path

How to find the shortest path not containing a pattern?


I am using Neo4j 5.9.0 community

Problem description : My graph only has one type of relationship [:Relationship], and one type of node (:Node). I want to find the shortest path between two nodes, but I do not want the shortest path returned to contain this pattern : (:Node)<-[:Relationship]-(:Node)-[:Relationship]->(:Node)

I have read here that

If the MATCH clause of the shortestPath function includes a WHERE clause, a shortest path that satisfies the WHERE clause conditions is returned if one exists.

so this should be possible, and I have tested without using the shortestPath function that paths that verify this requirement indeed exist in my graph.

What I have tried: I have tested mutliple ways to filter the shortest path returned, but none of them were successful. I tried using the exists{} subquery:

match p = shortestPath((startNode:Node {id : 1})-[:Relationship*]-(endNode:Node {id : 2})) 
where none(pathNode in nodes(p) where exists{
match (n1:Node)<-[:Relationship]-(pathNode)-[:Relationship]->(n2:Node)
where ((n1 in nodes(p)) and (n2 in nodes(p)))
}) 
return p

which returns 'Unknown expression type during transformation (class org.neo4j.cypher.internal.ir.ast.ExistsIRExpression)',

as well as using isEmpty() predicate function :

match p = shortestPath((startNode:Node {id : 1})-[:Relationship*]-(endNode:Node {id : 2}))
where isEmpty ([(n1)<-[:Relationship]-(n2)-[:Relationship]->(n3) where ((n1 in nodes(p)) and (n2 in nodes(p)) and (n3 in nodes(p))) | n2.id])
return p

which returns 'Unknown expression type during transformation (class org.neo4j.cypher.internal.ir.ast.ListIRExpression)'

I do not understand why the queries above do not work properly. Any help would be appreciated, thank you.


Solution

  • After explaining the problem on the Neo4j GitHub, the Neo4j team addressed this bug and declared that it has been fixed in the 5.15 release. I have not personally verified nor tested this.