Hello,
I am pretty new to CYPHER and NEO4J.
My requirement is to find the relations of an array of input nodes.
Example:
I am using the following query,
WITH ["A", "B","F"] AS names
UNWIND names AS nn
MATCH (n {name: nn})
WITH collect(n) AS nds
UNWIND nds AS n1
UNWIND nds AS n2
WITH nds, n1, n2 WHERE id(n1) > id(n2)
MATCH path = allShortestPaths((n1)-[*]-(n2))
WITH nds, path WHERE ALL(n IN nds WHERE n IN nodes(path))
RETURN path ORDER BY length(path) ASC
getting the following output which is correct,
But when I gave input ["A", "B","F","H"] in the query I am getting empty output which is not desired.
The desired output is similar to the above mentioned output but also containing A->H relation.
Please let me know where I am going wrong and what needs to be done.
But the same query works for the inputs "A","B", "H'.
WITH ["A", "B","H"] AS names
UNWIND names AS nn
MATCH (n {name: nn})
WITH collect(n) AS nds
UNWIND nds AS n1
UNWIND nds AS n2
WITH nds, n1, n2 WHERE id(n1) > id(n2)
MATCH path = allShortestPaths((n1)--(n2))
WITH nds, path WHERE ALL(n IN nds WHERE n IN nodes(path))
RETURN path ORDER BY length(path) ASC
Thanks for the reply. so what I am doing here just retrieving a single common path then. so it will not return multiple paths.
What to do to achieve my requirement ?
The reason why you weren't getting results with A, B, F, and H was because there is no single common path that can connect them all.
A path only has a single start node, and a single end node, and it cannot reuse relationships in the path (relationship isomorphism). Because of this, F and H are dead ends. Once you reach there, the path cannot continue. So you can go B-A-F, or B-A-G-H, or F-A-G-H (or the reverse of any of those). But you cannot go B-A-F-A-G-H because it reuses the relationship between A and F.
So you cannot return the graph you want with a single path. It would require an approach that generates multiple paths, and the graph formed by those multiple paths would include all 4 nodes.