Not able to access nodes outside of FOREACH clause

11 views
Skip to first unread message

Avik Pal

unread,
Aug 4, 2017, 7:22:01 AM8/4/17
to Neo4j
Hello,

I am new to Neo4j and currently using 3.0 community edition.

Requirement: I have an excel file with 10 columns and I have to read data from excel, create node and relationships accordingly. There are few rows in my excel sheet where the value is NA, and for those kind of records I need to skip node creation and adjust the relationship accordingly.

I am able to skip the node creation but it is not creating the relationship. To me it seems like enrichgraph and enrich is not accessible outside that FOREACH clause. Belwo is the code:
// create the Enrich Graph node if value is not NA
FOREACH(ignoreMe IN CASE WHEN trim(line.EnrichGraph) <> "NA" THEN [1] ELSE [] END | 
MERGE (enrichgraph:EnrichGraph { title:'Enrich Graph', name: line.EnrichGraph }))

// create the Enrich File node if value is not NA
FOREACH(ignoreMe IN CASE WHEN trim(line.EnrichFile) <> "NA" THEN [1] ELSE [] END | 
MERGE (enrich:EnrichedFile { title:'Enriched File', name: line.EnrichFile }))

// create the Relationship only if both of them are not NA
FOREACH(ignoreMe IN CASE WHEN trim(line.EnrichFile) <> "NA" THEN [1] ELSE [] END | 
FOREACH(ignoreMe IN CASE WHEN trim(line.EnrichGraph) <> "NA" THEN [1] ELSE [] END | 
MERGE (enrichgraph)-[:PRODUCES]->(enrich) ) )

Is there a way to set those as global variables or something? or is there another way of implementing this? Please help.

Thanks,
Avik

Michael Hunger

unread,
Aug 4, 2017, 7:33:55 AM8/4/17
to ne...@googlegroups.com
The easiest way would be a multi-pass where you do the filters on the fields to create the nodes with WHERE 
and then a last pass for the relationships.

Based on your code you can also do a second MERGE in the 2nd FOREACH


// create the Enrich Graph node if value is not NA
FOREACH(ignoreMe IN CASE WHEN trim(line.EnrichGraph) <> "NA" THEN [1] ELSE [] END | 
MERGE (enrichgraph:EnrichGraph { name: line.EnrichGraph }))

// create the Relationship only if both of them are not NA
FOREACH(ignoreMe IN CASE WHEN trim(line.EnrichFile) <> "NA" THEN [1] ELSE [] END | 
MERGE (enrich:EnrichedFile { name: line.EnrichFile })

FOREACH(ignoreMe IN CASE WHEN trim(line.EnrichGraph) <> "NA" THEN [1] ELSE [] END | 
MERGE (enrichgraph:EnrichGraph { name: line.EnrichGraph })
                MERGE (enrichgraph)-[:PRODUCES]->(enrich) ) )

--
You received this message because you are subscribed to the Google Groups "Neo4j" group.
To unsubscribe from this group and stop receiving emails from it, send an email to neo4j+unsubscribe@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages