3

I'm trying to transfert all ingoing and outgoing relationships from a node to another, before deleting the first one. they both have the same label. I saw this Neo4j Cypher: copy relationships and delete node but in my case i don't know the type of the relations and i want to transfer both ingoing and outgoing ones.

i'm looking for either a cypher query or a query based on neo4j.rb

Community
  • 1
  • 1
armedwing
  • 113
  • 1
  • 10

1 Answers1

4

I don't think that this is possible with pure cypher. Here's a solution using neo4j.rb that I think will work:

# Assuming node1 already loaded
node_query = Neo4j::Session.query.match(node: {neo_id: node1.neo_id})

types = node_query.match('node-[rel]-()').pluck('DISTINCT type(rel)')

types.each do |type|
  node_query.match('node-[rel]->(other)').with(:node, :rel, :other).create("node-[new_rel]->other").set('new_rel = rel').exec
  node_query.match('node<-[rel]-(other)').with(:node, :rel, :other).create("node<-[new_rel]-other").set('new_rel = rel').exec
end
Brian Underwood
  • 10,746
  • 1
  • 22
  • 34
  • i got the types, but then in the each loop i'm not sure how to specify the other node for the new_rel. thanks again Brian;) – armedwing Feb 01 '15 at 13:03
  • Just editing because the second query was going in the wrong direction and they both needed an `exec` – Brian Underwood Feb 01 '15 at 13:57
  • The other node is getting specified by the `other` variable. That should be passed through in the `WITH` clause and then matched on again in the `CREATE` clause. I could be misunderstanding... – Brian Underwood Feb 01 '15 at 13:57
  • i'm sorry, i'm still struggling with that. assuming node1 and node2 are loaded, we want to transfer relationships from node1 to node2. how would you use the `WITH` clause in this case ? – armedwing Feb 01 '15 at 15:33
  • The two lines inside of the `each` are generating cypher queries. The `with` in the middle of the method call chain is what generates the `WITH` part of the cypher clause. I'm not 100% sure that using the with will copy the properties of the one rel to the other, but I saw somebody using the same technique on another SO post – Brian Underwood Feb 01 '15 at 20:06
  • i half way now. i can transfer Neo4j::ActiveRel but not undeclared relationships. i tried another way and i had to add a test on rel.to_node to know if it is a Neo4j::ActiveRel. i still don't know how to transfer the other ones ` relations = self.rels(dir: :outgoing) relations.each do |rel| if defined? rel.to_node new_node.create_rel(rel.type, rel.to_node, rel.props) end end relations = self.rels(dir: :incoming) relations.each do |rel| if defined? rel.from_node rel.from_node.create_rel(rel.type, new_node, rel.props) end end ` – armedwing Feb 01 '15 at 22:22
  • This is starting to get a bit crowded for comments, maybe open up a new question? – Brian Underwood Feb 02 '15 at 06:28
  • indeed, done : http://stackoverflow.com/questions/28274009/neo4j-rb-transfer-all-relationships-before-replacing-a-node-by-another – armedwing Feb 02 '15 at 08:55