Article DetailsDetailsHelp DetailsIn Neo4j 5.7 and 5.8 the allshortestpaths() results have changed. Only 1 path is returned instead of the expected 2. neo4j@test1> CREATE (n:person {pin: 'abc123'}) CREATE (m:ppty {code: 'XYZ'}) CREATE (n) - [:lives_in] -> (m) CREATE (n) - [:owns] -> (m) RETURN allShortestPaths((n) - [*0..2] -> (m)) as p; +----------------------------------------------------------------+ | p | +----------------------------------------------------------------+ | [(:person {pin: "abc123"})-[:lives_in]->(:ppty {code: "XYZ"})] | +----------------------------------------------------------------+ In Neo4j 5.6, earlier 5.x versions, and Neo4j 4.x the results include 2 paths correctly as expected. neo4j@test2> CREATE (n:person {pin: 'abc123'}) CREATE (m:ppty {code: 'XYZ'}) CREATE (n) - [:lives_in] -> (m) CREATE (n) - [:owns] -> (m) RETURN allShortestPaths((n) - [*0..2] -> (m)) as p; +--------------------------------------------------------------------------------------------------------------------------+ | p | +--------------------------------------------------------------------------------------------------------------------------+ | [(:person {pin: "abc123"})-[:lives_in]->(:ppty {code: "XYZ"}), (:person {pin: "abc123"})-[:owns]->(:ppty {code: "XYZ"})] | +--------------------------------------------------------------------------------------------------------------------------+ Workarounds 1. In Neo4j 5.7 and 5.8, the old shortest path implementation is still available, but behind a feature flag. Enabling it will fix the issue. It is a dynamic feature and can be enabled by calling: CALL dbms.setConfigValue("internal.cypher.use_legacy_shortest_path", "true"); CALL db.clearQueryCaches(); After enabling this feature, the results will be as below: neo4j@test1> MATCH (n:person {pin: 'abc123'}) MATCH (m:ppty {code: 'XYZ'}) WITH n, m MATCH p=allShortestPaths((n) - [*0..2] -> (m)) RETURN COLLECT(p) AS ps; +--------------------------------------------------------------------------------------------------------------------------+ | ps | +--------------------------------------------------------------------------------------------------------------------------+ | [(:person {pin: "abc123"})-[:owns]->(:ppty {code: "XYZ"}), (:person {pin: "abc123"})-[:lives_in]->(:ppty {code: "XYZ"})] | +--------------------------------------------------------------------------------------------------------------------------+ This old shortest path implementation is a bit slower though. 2. The bug only affects shortest path when it is inside an expression. One could rewrite the query so that it is given its own plan, like below: neo4j@test2> CREATE (n:person {pin: 'abc123'}) CREATE (m:ppty {code: 'XYZ'}) CREATE (n) - [:lives_in] -> (m) CREATE (n) - [:owns] -> (m) WITH n, m MATCH p=allShortestPaths((n) - [*0..2] -> (m)) RETURN COLLECT(p) AS ps; +--------------------------------------------------------------------------------------------------------------------------+ | ps | +--------------------------------------------------------------------------------------------------------------------------+ | [(:person {pin: "abc123"})-[:owns]->(:ppty {code: "XYZ"}), (:person {pin: "abc123"})-[:lives_in]->(:ppty {code: "XYZ"})] | +--------------------------------------------------------------------------------------------------------------------------+ NOTE: This issue has been fixed in versions of Neo4j 5.9 and higher. Reference: https://neo4j.com/docs/cypher-manual/current/patterns/concepts/#shortest-path-all-shortest-pathsInformationTitleAllshortestpaths() returns defferent result in Neo4j 5.7 and 5.8Article Created Date3/15/2024, 10:54 PMURL Name18825258809875-Allshortestpaths-returns-defferent-result-in-Neo4j-5-7-and-5-8Last Modified Date5/28/2024, 9:02 PM