Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
12-24-2022 10:17 PM
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]
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.
12-25-2022 02:48 AM
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
12-26-2022 03:44 AM
@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.
12-26-2022 08:02 AM
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
12-27-2022 08:30 PM
@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.
12-27-2022 08:45 PM
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.
All the sessions of the conference are now available online