I am working with a Neo4j database and trying to create :Impact relationships between the Issue node and Version nodes that follow from firstAffected to fixedVersion. However, as seen from the result, not all relationships are being created.
Here is my query:
MATCH (issue:Issue {name: "Issue 1"})
MATCH (firstAffected:Version)<-[:First_Affected]-(issue), (fixedVersion:Version)<-[:Fixed]-(issue)
WITH fixedVersion, firstAffected, issue
MATCH path = (firstAffected)<-[:NEXT_VERSION*]-(fixedVersion)
WITH nodes(path) AS allNodes, issue
WITH allNodes[..-1] AS impact_target_nodes, issue
UNWIND impact_target_nodes AS targetNode
CREATE (issue)-[:Impact]->(targetNode)
The query does not add the Impact relationship to Version 7. I expect that all versions in the path will be linked to the Issue, but this is not the case.
How can I modify the query to correctly create Impact relationships for all versions between firstAffected and fixedVersion?
It looks like you're trying add a relationship from the issue to every version that is either the first impacted version, or every downstream version that does not have the fix. A nice way to do this is to use a quantified path pattern with an inline predicate:
MATCH (issue:Issue {name: "Issue 1"})-[:First_Affected]->(firstAffected:Version)
(()<-[:NEXT_VERSION]-(r:Version) WHERE NOT EXISTS { (r)<-[:Fixed]-(issue) } )*
(impactedVersion:Version)
WITH DISTINCT issue, impactedVersion
CREATE (issue)-[:Impact]->(impactedVersion)
This will traverse as far as it can until either it comes to a dead end (as with version 7) or just before it encounters the version with a fix (as with version 2). It uses the predicate NOT EXISTS { (r)<-[:Fixed]-(issue) }
to stop before version 3, which is where the fix was introduced.