1
votes

I am new to Neo4j and I have been stuck on a simple problem for a whole day. It should be a very simple fix but I just can't get it right! I am trying to model a company's hierarchical structure (from CEO to interns). I have a cvs file with the Employee_ID numbers and their job titles. In the same file I have another column with the ID numbers of the Employees' managers. So in each row I have 3 cells: Employee's ID, job title, Employee's manager's ID, and I have 30 rows(for now).

(For example: next to Employee John's ID there is employee Sam's ID because Sam is John's manager. Sam also appear in the Employee column, and next to his ID number there is Mark's ID, because Mark is Sam's manager..and so on)

It seems very similar to the classic example Actor-[ACTED_IN]->Movie, but in this case I don't have 30 actor nodes and 30 movie nodes, and I am not sure how to build 30 relationship between the same 30 nodes.

Every time I try to create the WORKS_UNDER relationship I end up creating new nodes and I can't get Neo4j to realise that the nodes on the right side of the relationship are already in the graph.

I am sure this is a simple problem, but as I said, I am new to Neo4j and I could really use the help!

UPDATE: I managed to make it work by adding the managers' IDs as a property in the Employees' nodes.

LOAD CSV WITH HEADERS FROM "file:///Personnel.csv" AS row FIELDTERMINATOR ';'

MERGE (person:Person { employee_ID:row.ID, job_title:row.Jobtitle, manager_ID:row.ManagerID })

WITH *

MATCH (p1:Person)

MATCH (p2:Person)

WHERE p2.employee_ID = p1.manager_ID

MERGE (p1)-[r:WORKS_UNDER]-(p2)

I'm sure there must be a way to do this that doesn't require me to add the managers' IDs as a property, but I can't figure it out :(

2
You best share the query you are usingChristophe Willemsen
Please edit your question to include the cypher create/merge/match statement(s) you're using during import. Otherwise, we can only guess at the issue.David Makogon

2 Answers

1
votes

If you're ending up with new unexpected nodes then more than likely you're using MERGE on the entire pattern instead of merging on each node first and only then merging the relationship between them.

MERGE is like a MATCH followed by a CREATE (if the match didn't succeed). Any elements of the pattern that are not bound already will be created.

Here's a knowledge base article on better understanding how MERGE works.

0
votes

Make sure you have created the constraint on your business id

 CREATE CONSTRAINT ON (n:<LabelName>) ASSERT n.<propertyKey> IS UNIQUE