'Excluding certain paths from Neo4j Cypher allShortestPaths

I want to find the shortest paths between two nodes in a graph. I use the allShortestPaths algorithm in Neo4j Cypher. Not all possible paths should be considered when applying the algorithm, some certain paths should be excluded from the search. I don't want to exclude certain node labels or certain relationships, I need to exclude a certain path pattern from the algorithm.

Let's consider the following simplified example:

CREATE 
    (b:Bank {name: "Money Bank"}),
    (c1:Customer {customerNumber:123}),
    (c2:Customer {customerNumber:456}),
    (p:Person {name:"Mr. X"}),
    (com:Company {name:"ACME Inc."}),
    (c1)-[:HAS_ACCOUNT_IN]->(b),
    (c2)-[:HAS_ACCOUNT_IN]->(b),
    (c1)-[:MATCHES]->(com),
    (c2)-[:MATCHES]->(p),
    (p)-[:WORKS_FOR]->(com)

graph example

Two customers have an account in the "Money Bank". Customer 123 matches the company "ACME Inc." and Customer 456 matches the person "Mr. X", who is working for "ACME Inc.". I want to find all shortest paths between "Money Bank" and "ACME Inc.", but I don't want to consider the path (Company)-(Customer)-(Bank) in my search for the shortest paths.

To me, the most intuitive way to accomplish this, would be the following cypher:

MATCH path=allShortestPaths((b:Bank {name:"Money Bank"})-[*1..5]-(com:Company{name: "ACME Inc."}))
WHERE NOT (b)<-[:HAS_ACCOUNT_IN]-(:Customer)-[:MATCHES]->(com)
RETURN path

But this returns no records/paths. I would have expected that the WHERE is evaluated before applying the allShortestPath algorithm. Is this assumption wrong? If this is the case, what would be the best way to achieve what I want? Thanks for your help!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source