Reputation: 121
I have three independent category trees that is going to import at any order using cypher.
and need to create the structure mentioned in the figure using the query . The query I written is
MERGE (:Category {name:'c2'})-[:PARENT]->(:Category {name:'c1'})
MERGE (:Category {name:'c4'})-[:PARENT]->(:Category {name:'c3'})-[:PARENT]->(:Category {name:'c1'})
MERGE (:Category {name:'c5'})-[:PARENT]->(:Category {name:'c3'})
But above query creating duplicate category c1 for second merge query which I need to avoid . Also the third query should create new category c3 which is happening correctly now.
One more thing is that these three cypher query should be independently executable .eg: System already have a category tree (c2)-[PARENT]->(c1) and need to add (c4)-[PARENT]->(c3)->[PARENT]->(c1) in to the category tree using cypher.
I can go with some similar approach mention in the documentation http://neo4j.com/docs/stable/cypherdoc-linked-lists.html . but just want to check is there a simple way to solve this problem
Upvotes: 1
Views: 150
Reputation: 121
I solve the problem by adding one more label called Root for the top level category .
Cypher query for first tree - (c2)-[PARENT]->(c1)
MERGE (nc1:Category:Root{name:'c1'})
MERGE (nc3:Category {name:'c2'})-[:PARENT]->(nc1)
Cypher query for second tree - (c4)-[PARENT]->(c3)->[PARENT]->(c1)
MERGE (nc1:Category:Root{name:'c1'})
MERGE (nc3:Category {name:'c3'})-[:PARENT]->(nc1)
MERGE (:Category {name:'c4'})-[:PARENT]->(nc3)
Cypher query for third tree - (c5)-[PARENT]->(c3)
MERGE (nc3:Category:Root{name:'c3'})
MERGE (nc5:Category {name:'c5'})-[:PARENT]->(nc3)
Upvotes: 0
Reputation: 556
You can use single query to avoid duplicate entry
MERGE (:Category {name:'c4'})-[:PARENT]->(:Category {name:'c3'})-[:PARENT]->(:Category {name:'c1'})<-[:PARENT]-(:Category {name:'c2'})
MERGE (:Category {name:'c5'})-[:PARENT]->(:Category {name:'c5'})
Upvotes: 0
Reputation: 591
Try this (without typo in third query)
MERGE (:Category {name:'c2'})-[:PARENT]->(c1:Category {name:'c1'})
MERGE (:Category {name:'c4'})-[:PARENT]->(:Category {name:'c3'})-[:PARENT]->(c1)
MERGE (:Category {name:'c5'})-[:PARENT]->(:Category {name:'c3'})
Upvotes: 1