cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Building Hierarchy using Neo4j

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?

7 REPLIES 7

Fanny
Node Link

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!

@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)

Hi, thanks. yes it worked.

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

Hi @glilienfield .. I have one more question. My graph is like below.

  1. 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
  2. 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

3X_0_4_04a45b6122580496986321dd1466ce62e3a17eb0.png

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

3X_0_4_044d176db75ecf4ba50260d5f7baf203327a2539.png

Thanks a lot @glilienfield ..