How to identify relationship dynamically and traverse between nodes to get results in cypher

Hi,

I have Graph like this below.
A-[:x]->B-[:y]->C-[:z]->D

Assume I dont know what relation exists between A and D, but I know A and D are related. I want to write a dynamic query which can detect A.name connected to d.name="XXX" without writing the relations between A to D.

I tried something like this but it is not giving correct results. I am expecting A nodes whcih are related to d,name="HEN" but I am getting all A nodes related to D nodes.
MATCH (a:A),(d:D {name:"HEN"}), p = shortestPath((a)-[ * ]-(d))
RETURN distinct a,p,d

Can you let me know how can I achieve this. I want to identify the relation between two nodes first and using that relation between two nodes I want to find out result.

Thanks in Advance

MATCH p=(start:A { name: "Starting Point" })-[*]->(end:D { name: "Ending Point" })
RETURN p;

The use of the -[*]-> will match any set of relationships of any type in any order or length. It'll find a path from A to D for you. All of this gets put into a "path variable" called p which gets returned. You can use list functions to then extract out of that all of the nodes and relationships in the middle, if you wish.

3 Likes

I have a relationship as
A-[:x]->B-[:y]->C
OR
A-[:x]->C
Does the above solutions holds true for this situation as well.... I haven't tried yet though. (Will do in couple of days)