Adding multiple labels when loading from CSV

When using the neo4j-admin tool I can attach multiple labels to a node by using the :LABELS column. I want to do something similar with LOAD CSV and thought that a statement like:

LOAD CSV WITH HEADERS FROM "file:///tlpdb/48871/outnew/node-TLPackage.csv" AS row
         MERGE (c:TLPackage { uuid: row.uuid, name:, revision: toInt(row.revision) })
         ON CREATE set c:row.`:LABELS`

might work, but that didn't do it. Is there a way to use the labels taken from a column (in my case there is only one additional label there) and add it to the load/merge/on create call?

standard cypher does not allow for dynamic labels - this is basically what you want with :LABELS column.

CALL apoc.create.addLabels to the rescue!

1 Like

you should only use the uuid for merging (and create an constraint on it).

There is also apoc.merge.node() which allows for dynamic labels too.

Hi Stefan, hi Michael,
thanks for suggestion apoc functions, which I have tried, but it seems that in the ON CREATE part of the MERGE command calls are not allowed. That would mean I would have to add additional calls after the initial merge to rewrite labels I guess.

Since I have only 6 or so sub-classes (different labels to add) I have changed the approach, split the original data into separate csv files per label, and then just add one cypher merge call for each csv separately.

This is not optimal, because it codes parts of the logic into the cypher code, while having programmatically being able to add labels from CSV columns would be future proof.

Thanks a lot for your suggestions


apoc.merge.node allows you to supply two maps: on for identifying properties and another one for the remainder. What are you missing there?

apoc.merge.node ??? I cannot find this listed on APOC User Guide which according to GitHub - neo4j-contrib/neo4j-apoc-procedures: Awesome Procedures On Cypher for Neo4j - codenamed "apoc"                     If you like it, please ★ above ⇧            

All included procedures are listed in the overview in the documentation and detailed in subsequent sections.

Where can I find documentation for it.

Ahh, wait Feature request : apoc support for MERGE for nodes and rels · Issue #271 · neo4j-contrib/neo4j-apoc-procedures · GitHub here it is mentioned that Summer 2017 Release of the APOC Procedures Library contains references to

CALL apoc.merge.node(['Label'], {id:uniqueValue}, {prop:value,...}) YIELD node;
CALL apoc.merge.relationship(startNode, 'RELTYPE', {[id:uniqueValue]}, {prop:value}, endNode) YIELD rel;

I will play with it, since there is no documentation but I guess I can guess what it does. Maybe update the documentation would be a good service ;-)

Thanks for the pointer!!!

I was having this same exact problem, but apoc.merge.node did not solve my issue. I read the docs on this apoc statement, and it was not clear to me how to add multiple labels to only those nodes that are created with the MERGE + ON CREATE SET piece.

My question is: using MERGE ON CREATE SET, how can I add more labels only to those nodes that are created during the MERGE execution?

This is what gave me the result I was looking for:

FROM "file:///file.csv" AS row
MERGE(p:Person {id:})
              SET p:New,
                      p.role = row.role,
                      p.location = row.location

I do this because this statement helps me add new nodes to my database. With the "New" label on these nodes, I can easily query these nodes to just focus on these to create the new relationships to the other nodes on the graph.

Is there a better way to do this with apoc.merge.node?