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?
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.
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})
@pesle.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)
is it possible that if if i search on top most node lets say employee d, I can get only initial level of relation, in this case node a, y, x? likewise when searched on node q, i can get only z....means all intermediate nodes and relations are not displayed
vice-versa of point 1, when searched on first level , only end level of nodes is returned. for example when searched on node a, node d is returned
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