0

As I have learnt from another question it is apparently not possible in Cypher alone to copy a subgraph or to duplicate a chain of nodes. But I need to do this somehow. Therefor I looked for a more hacky approach to do this.

Specifically I need to transform am graph that looks like this:

(A)──>(N1:T1)──>(B)

into a graph that looks like this

(A)──>(N1:T1)──>(B)
 │               ^
 └───>(N2:T2)────┘

Though the part between A and B can be arbitrarily long. What matters is that all nodes of type T1 get converted to type T2 in the copy of the original path (the one with N1 in the example).

So, if I have for example this graph:

MERGE (N1:A)-[:r]->(N2:B)-[:r]->(N3:C)
MERGE (N1)-[:r]->(N4:C)-[:r]->(N5:C)

and a query that looks like this:

MATCH p=(:A)-[*0..]->(:B)-[*0..]->(:C)
RETURN p

and I execute it with graph.cypher.execute(<query>) then the record returned by execute() is:

   | p                         
---+----------------------------
 1 | (:A)-[:r]->(:B)-[:r]->(:C)

Now I would like to get the IDs of all the nodes. Then I could generate a string with Cypher code that represents the copied path and execute it again with execute(). And in that string I would use the IDs of nodes in the record to connect to the existing nodes in the database. But I recall that it is somehow not possible either, to target nodes in the database from outside via an ID... or is in fact it possible? Are there any other ways I could go about this? Somebody mentioned the dump functionality, but the documentation is very lacking on that and I don't quite understand how it is supposed to work. And in addition I am not even sure if it does what I need.

lo tolmencre
  • 3,804
  • 3
  • 30
  • 60
  • 1
    Do *all* the nodes between the `:A` and `:C` have the `:B` label? – cybersam Mar 16 '16 at 16:06
  • 1
    Also, it is possible for you to use a node's `ID` (and it will remain unique as long as the node is not deleted). – cybersam Mar 16 '16 at 16:07
  • No, only some of the nodes between the `:A`-nodes and the `:C`-nodes have label `:B` (or label `:T1` respectively, if we go with the second example). How can I however get the IDs of the nodes in the chain of the record? – lo tolmencre Mar 16 '16 at 22:29
  • 1
    You can use the [ID()](http://neo4j.com/docs/stable/query-functions-scalar.html#functions-id) function to get the neo4j-generated ID for any node or relationship. That ID is guaranteed to be unique for the lifetime of that node/relationship, but can be reused after it is deleted. – cybersam Mar 16 '16 at 22:36

0 Answers0