Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
02-28-2022 09:34 PM
hi, Requesting help on this.
Requirement - I have a dataset having two columns employee and manager and want to build a hierarchical graph. Below is the sample data set
|Employee|Manager|
|a|b|
|b|c|
|c|d|
|d|no manager|
Problem - the problem that i am facing is there are two b nodes, two c nodes coming instead of linking everything with just one bunch. Expected output : a---has manager---b---has manager---c---has manager---d in graphical form.
Can somebody please help me with cypher code for this?
03-01-2022 03:41 AM
Hi,
Could you please provide some sample of your Cypher query leading to this problem?
My guess is you are using CREATE which leads to duplicate nodes. Instead, you should probably use MERGE which will first check whether the nodes already exist. See this part of documentation for examples of Cypher queries.
Hope it helps!
03-01-2022 10:13 AM
@Fanny was most likely correct on the cause of your issue. You can try the following query:
load csv with headers from 'file:///OrgChart.csv' as row
merge (n:Employee{id:row.Employee})
with n, row
where row.Manager is not null
merge (m:Employee{id:row.Manager})
merge (n)-[:HAS_MANAGER]->(m)
03-02-2022 12:24 AM
Hi, thanks. yes it worked.
03-01-2022 09:13 AM
Hello @ask.singh90
No need to add Employee
or Manager
in your model since the relationships give you that information:
USING PERIODIC COMMIT LOAD CSV FROM 'file:///records.csv' AS line FIELDTERMINATOR '|'
MERGE (:Person {name: line.Employee})-[:IS_MANAGED_BY]->(:Person {name: line.Manager})
Regards,
Cobra
03-03-2022 04:34 AM
Hi @glilienfield .. I have one more question. My graph is like below.
03-03-2022 06:45 AM
The best way to determine an approach for solving these problems is to identify the pattern that describes your requirement. For question 1, you are asking for nodes that are linked to node 'd' through a series of HAS_MANAGER relations, but the required nodes should not have any HAS_MANAGER relationships inbound. The following query will do this:
match(d:Employee{id:'d'})
match (n)-[:HAS_MANAGER*]->(d)
where not exists(()-[:HAS_MANAGER]->(n))
return distinct n
Using the same logic, question 2 can be addressed with the following query:
match(n:Employee{id:'a'})
match (n)-[:HAS_MANAGER*]->(d)
where not exists((d)-[:HAS_MANAGER]->())
return distinct d
All the sessions of the conference are now available online