Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
11-23-2022 11:19 PM
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
#Neo4J #CSV #GraphDatabase
Solved! Go to Solution.
11-26-2022 08:51 AM - edited 11-26-2022 08:54 AM
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.
11-27-2022 10:50 AM
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.
11-27-2022 05:55 PM
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.
11-24-2022 12:25 AM
The illustration here has no problem. Please ignore!
I have figured out the problem with my main data.
Thanks
11-26-2022 02:12 AM
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
11-26-2022 08:51 AM - edited 11-26-2022 08:54 AM
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.
11-27-2022 06:40 AM
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!
11-27-2022 10:50 AM
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.
11-27-2022 05:24 PM
thanks for the clarification!
11-27-2022 05:35 PM
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 ?
11-27-2022 05:55 PM
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.
11-26-2022 07:43 PM
You are right! Thanks!
All the sessions of the conference are now available online