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.

Adding multiple labels when loading from CSV

norbert
Node Link

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: row.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?

6 REPLIES 6

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

CALL apoc.create.addLabels to the rescue!

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.

norbert
Node Link

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

Norbert

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 https://neo4j-contrib.github.io/neo4j-apoc-procedures/index33.html#_overview_of_apoc_procedures_func... which according to https://github.com/neo4j-contrib/neo4j-apoc-procedures

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 https://github.com/neo4j-contrib/neo4j-apoc-procedures/issues/271 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!!!

carib
Graph Buddy

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:

LOAD CSV WITH HEADERS
FROM "file:///file.csv" AS row
MERGE(p:Person {id: row.id})
ON CREATE
              SET p:New,
                      p.name = row.name,
                      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?

Thanks.