0
votes

I'm using neo4j 2.1.2 community edition.

I want to replicate the nodes and relationships to a new path from the existing old path. Consider i have one hierarchy.

Company:Google Street : 5760 W 96th Street City:Marco State : FL Country : US

For the first time i will create a hierarchy with 4nodes with some relationships. The relationship looks like as below:

Create (Google)-[:located_at]->(Marco)-[:belongs_to]->(FL)-[:present_in]->(US)

And then I will create one more company node called as Yahoo and this company is having different street name and same city, state and country name as same as Google.

So now i want to create a yahoo node and street node , and wanted to copy the city, state and country node along with their relationships to this node.So here I wanted to copy the existing nodes into my new path.

How can i do it?

My new node looks like this :

Company: Yahoo
Street : 199 Grandview Road
City : Marco
Sate : FL
Country : US

I do not want to attach the yahoo node with the Google node's city, state, country nodes. (These two paths present in a same database )

1
i advise you to not create duplicate nodes for cities, states, countries. it will have no sense to use a graph db thanulkas
@Ulkas, yes here in the above example which i have shared does not make any sense but in my real case the requirement is such a way that i have duplicate the path.shree11

1 Answers

2
votes

It sounds like an odd way to use a graph database, duplicating the nodes, but if you simply run another Create it will create duplicate nodes. The only thing that would prevent you from having all these duplicates is any unique constraints that you may have setup.

CREATE(:Company{name:'Google'})-[:located_at]->(:City{name:'Marco'})-[:belongs_to]->(:State{abbrv:'FL'})-[:present_in]->(:Country{name:'United States'})

If Marco exists and has properties and you do not want to recreate it from scratch, but create a duplicate then you can:

MATCH (marco:City{name:'Marco'}), (fl:State{abbrv:'FL'}), (us:Country{name:'United States'})
CREATE (:Company{name:'Yahoo'})-[:located_at]->(:City{name:marco.name})-[:belongs_to]->(:State{abbrv:fl.abbrv})-[:present_in]->(:Country{name:us.name)

If you changed your mind and did want to share a common path, you might use:

MATCH (marco:City{name:'Marco'})
CREATE (:Company{name:'Yahoo'})-[:located_at]->(macro)

And to prevent lots of duplicates you might change that to:

MATCH (m:City { name : 'Marco' })
MERGE (:Company { name:'Yahoo' })-[:located_at]->m