First time asking on SO and a newby of cypher and neo4j.
I need to get all the nodes, and all their relationships, that compose the lineage of a particular node; to display them in a directed graph I need a list of nodes
and a list of the relationships (links
).
This is my dummy set of nodes, and this is what I want to get (nodes plus links): result
This is what I came up after hours of research and attempts:
MATCH lineage = (n:Sample {name:"P"})-[:CHILD_OF*]->(parent:Sample)
MATCH (parent)-[r_out]->(child)-[r_in]->(parent)
WHERE parent IN nodes(lineage) OR child IN nodes(lineage)
RETURN
collect(DISTINCT parent) AS nodes,
collect({ source: parent.name, rel: type(r_out), target: child.name }) AS links_out,
collect({ source: child.name, rel: type(r_in), target: parent.name }) AS links_in;
(I created two different list of relationships so i can distinguish the source and target.)
However this does not return n in the list of nodes and actually multiplicates the links, as I guess returns all the possible paths between n
and all the other nodes.
I could not figure out a solution and I am also convinced it should be a much more elegant query...
Any help? Thanks