Importing CSV with multiple columns containing arrays that are related

Hi everyone, I am trying to import some data into neo4j from a CSV file and I am struggling to figure out exactly how to get the result I want.

The first column is the id of the recipe, second column is the name, third is the list of ingredients, forth column contains the amount of each ingredient, and the final column contains the unit that is meant to go with that amount. Using this data, I am attempting to create a graph database with the following structure:

image

I know how to unwind the ingredient list to create multiple ingredient nodes and create the appropriate relationships between the recipes and the ingredients, but I am stumped when it comes to adding the appropriate "amount" and "units" values to the relationships as attributes. I am using the APOC extension to load the csv.

Any help would be very much appreciated. Apologies if something similar has been brought up before, but I have genuinely been unable to find any post online discussing a similar issue.

Hi, Welcome to the neo4j community

I think that you could do the CSV with row individually or you could try the following:

with recipe, ingredientlist, amountlist
MERGE (rec:Recipe{name:recipe})
WITH collect(ingredientlist) AS ingredientcollect,
WITH collect(amountlist) AS amountcollect, range(0,size(ingredientcollect)-1,1) AS coll_size
WHERE size(ingredientcollect) = size(amountcollect)
UNWIND coll_size AS idx
MERGE (ing:Ingredient{name:ingredientcollect[idx]})
MERGE (rec) - [:CONTAINS_INGREDIENT{amount:amountcollect[idx]}] -> (ing)

Thanks

Thank you for the reply! How could I go about doing the CSV with row individually?

1 Like