Dynamically return virtual relationship based on matched relationships

I have following graph with nodes and relationship

Country (IN) -[CONTAIN]-> State(MH)-[CONTAIN]->District-[CONTAIN]->Tahashil-[CONTAIN]->Village<-[LIVES]->[Person]<-[HOME]-address

Country(US) -[CONTAIN]-> State(California)-[CONTAIN]->District-[CONTAIN]->Tahashil-[CONTAIN]->Village<-[LIVES]->[Person]<-[HOME]-address<-[OFFICE]-address

additional relation between [Person]-[MATCHED]->[Person]

maheshwar_ligad_0-1671947247965.png

I have above graph, if person name match I have to return the graph

Actual data

country-> state-> district->tahasil->village->Person

Output

country -> state-> Person

OR

country-> Person

OR

country->State->district->Tahasil->Person

How to achieve this using virtualrelationship? Thanks in advance.

If your query results in the path identified as ‘actual data’, you can construct a virtual path for each of the paths identified as ‘output’ using the apoc procedure/functions described in the link below.

https://neo4j.com/labs/apoc/4.2/virtual/virtual-nodes-rels/#_function_overview

Try these. I matched the full path then returned the path you wanted with virtual relationships. Match sure the match meets your data model. There seems to be a little inconsistency with the Person node and its relationship type and direction to its related node.

Path 1:

match (country:Country)-[r1:CONTAIN]->(state:State)-[r2:CONTAIN]->(district:District)-[r3:CONTAIN]->(tahashil:Tahashil)-[r4:CONTAIN]->(village:Village)<-[r5:LIVES]-(person:Person)
return 
    country, 
    r1, 
    state, 
    apoc.create.vRelationship(person,'LIVES',{}, state),
    person

Path 2:

match (country:Country)-[r1:CONTAIN]->(state:State)-[r2:CONTAIN]->(district:District)-[r3:CONTAIN]->(tahashil:Tahashil)-[r4:CONTAIN]->(village:Village)<-[r5:LIVES]-(person:Person)
return 
    country, 
    apoc.create.vRelationship(person,'LIVES',{}, country),
    person

Path 3:

match (country:Country)-[r1:CONTAIN]->(state:State)-[r2:CONTAIN]->(district:District)-[r3:CONTAIN]->(tahashil:Tahashil)-[r4:CONTAIN]->(village:Village)<-[r5:LIVES]-(person:Person)
return 
    country, 
    r1,
    state,
    r2,
    district,
    r3,
    tahashil,
    apoc.create.vRelationship(person,'LIVES',{}, tahashil),
    person

@glilienfield Thanks for the response, but can you give me sample example, I am not able to see the sample example in documentation. That will be helpful.

@glilienfield This looks good but every time I have to write separate cypher query for each one, is it possible to make it dynamic with one single query to return result.

Dynamic based on what? You need get the file path and then create the virtual path. This is done with cypher that needs to be written.

One thing I could think of is if you passed a list of ordered pairs of integers that specified the node indexes of the start and end nodes that you want a virtual relationships between. You could write code that would substitute each segment with a virtual relationships, then output the remaining nodes, relationships, and virtual relationships.

This would be a little complicated, and I would think that you would have known scenarios that you wanted to implement, so you would only write the code for each scenario ones and use many times.