I stumbled on the following syntax for setting labels during a LOAD CSV operation in Neo4j with Cypher. It works but I don't understand why, and all of the following modifications break it:
- Removing any of the
YIELDstatements - Changing
YIELD nodetoYIELD node2or any other name on the 2nd and 3rdYIELDstatements - Removing any of the repeated
WITH n,rowstatements between theYIELDstatements - Adding
UNIONbetween the calls (at least, I couldn't get it to work)
Can anyone enlighten me? I'm a relative newcomer to Cypher and APOC and I'd love to understand how to make repeated APOC calls properly.
LOAD CSV WITH HEADERS FROM 'file:///myfile.csv' AS row
MERGE (n:Person{id:row.ID,name:row.Name})
WITH n,row
CALL apoc.create.addLabels(id(n), [row.Title,row.Position] YIELD node
WITH n,row
CALL apoc.create.addLabels(id(n), split(row.Roles, ',')) YIELD node
WITH n,row
CALL apoc.create.addLabels(id(n), split(row.Aliases, ',')) YIELD node
You should read the documentation for CALL, WITH, and UNION.
Here are answers to your specific questions:
When you
CALLa procedure that can return results, you generally must also specify aYIELDfor at least one of the procedure's result fields and use the exact field name. You can use SHOW PROCEDURES to get the signature of a procedure, which includes its result fields. For example, to get the signature ofapoc.create.addLabels:YIELDmust specify at least one of the result field name(s) used in the procedure's signature. You cannot use arbitrary names, although you can immediately useASto rename a field (e.g.,YIELD officalName AS foo).Cypher does not permit multiple
YIELDs to produce the same variable name (in the same scope). To get around this, you can useASto rename such variables. For example, this should work:(Also FYI: WITH changes the set of variables that are in scope. If any subsequent clauses need a variable, then a
WITHclause must specify that variable.)As documented, a UNION "combines the results of two or more queries into a single result set". You cannot just put
UNIONbetween arbitrary clauses.