cancel
Showing results for 
Search instead for 
Did you mean: 

MERGING multiple nodes + SETTING properties

Reuben
Node Clone

I am having a challenge merging nodes and also setting a property at the same time. Although it works, some nodes are still very independent instead of linking to other nodes. My objective is to connect the nodes with similar identities.

______

"LOAD CSV WITH HEADERS FROM $path AS row "
"MERGE (fo:food {food_name: row.Food_name}) "
"MERGE (ma:materials {materials: row.Material}) "
"MERGE (o:origin {origin: row.Origin}) "

"MERGE (fo)-[:related_material]->(ma) "
"MERGE (fo)-[:material_origin]->(o) "

_____

"LOAD CSV WITH HEADERS FROM $path AS row "
"MERGE (exm:material{material:row.Material}) "
"SET exm.Chinese_name = row.Chinese_name, exm.English_name = row.English_name "

_____

"LOAD CSV WITH HEADERS FROM $path AS row "
"MERGE (exo:orgin {origin:row.Origin}) "
"SET exo.Region = row.Region, exo.Season=row.Season "

_____

To illustrate, I have attached an example including the outcome (not what I was expecting). Please, what's the best way to resolve this issue? Thanks eg.pnggraph_example.png

#Neo4J #CSV #GraphDatabase

3 ACCEPTED SOLUTIONS

I see one issue in your queries. In the first one you label is materials and the second it is material. Thus, the merge fails and creates new nodes. They are not duplicates, but have different labels.

the other observation is you have multiple rows with the same origin, thus, the values set will only reflect the last row with the same origin.

View solution in original post

Sure, that is fine. the merge will either match an existing node or create a new one to match

You can either delete the wrong ones, or correct them. Which ever option is easiest. You can remove a label with ‘remove n:LabelToRemove’, where ‘n’ is the node’s binding variable. You can add a label with ‘set n:LabelToAdd’

You should delete the ones with the wrong label.

View solution in original post

The relationships between two nodes are fixed, regardless of you changing the labels on either node. You have to manually add/remove relationships.

you can either delete all the nodes created and run all three corrected queries, or delete the extra ones, fix the label if necessary, and run the 2nd and 3rd corrected queries.

View solution in original post

9 REPLIES 9

Reuben
Node Clone

The illustration here has no problem. Please ignore!

I have figured out the problem with my main data.

Thanks

Reuben
Node Clone

Please can anyone explain the reason for the duplicates in this case study? I and A are duplicated (independent nodes), while connected and at the same time, the materials are also duplicated while also linked as instructed.

#Neo4j #CSV

I see one issue in your queries. In the first one you label is materials and the second it is material. Thus, the merge fails and creates new nodes. They are not duplicates, but have different labels.

the other observation is you have multiple rows with the same origin, thus, the values set will only reflect the last row with the same origin.

Please I would like to ask if it is appropriate to create a new label when merging CSV files as I did in the above example.

---- "MERGE (ma:materials {materials: row.Material}) "
"MERGE (o:origin {origin: row.Origin}) "

+

---- "MERGE (exm:materials{materials:row.Material}) "

"MERGE (exo:orgin {origin:row.Origin}) "

Thank you!

Sure, that is fine. the merge will either match an existing node or create a new one to match

You can either delete the wrong ones, or correct them. Which ever option is easiest. You can remove a label with ‘remove n:LabelToRemove’, where ‘n’ is the node’s binding variable. You can add a label with ‘set n:LabelToAdd’

You should delete the ones with the wrong label.

thanks for the clarification!

So, If I may, what happens to the new label "exm" since it is merged with "ma", same with "o" and "exo"? Are they overridden ?

The relationships between two nodes are fixed, regardless of you changing the labels on either node. You have to manually add/remove relationships.

you can either delete all the nodes created and run all three corrected queries, or delete the extra ones, fix the label if necessary, and run the 2nd and 3rd corrected queries.

Reuben
Node Clone

You are right! Thanks!

Nodes 2022
Nodes
NODES 2022, Neo4j Online Education Summit

On November 16 and 17 for 24 hours across all timezones, you’ll learn about best practices for beginners and experts alike.