cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

Using apoc's refactor.cloneNodes to clone graph without extra relationships

nima
Node Link

Hi,

Problem

Using apoc.refactor.cloneNodes, I'm trying to duplicate a graph with a certain condition (all nodes containing a property {"version_id": "version-1"). Problem: extra connections between the previous (existing graph) & new graph also gets created.

Base Graph

CREATE (n:Catalog {catalog_id : 'catalog-1', name: 'Catalog 1', version_id: 'version-1'});
CREATE (n:SalesCategory {sales_category_id : 'category-1', version_id: 'version-1'});
CREATE (n:SalesCategory {sales_category_id : 'category-2', version_id: 'version-1'});

MATCH (c:Catalog {catalog_id: 'catalog-1'}), (s:SalesCategory {sales_category_id: 'category-1', version_id: 'version-1'}) CREATE (c)-[:HAS_SALES_CATEGORY {catalog_id: 'catalog-1', is_deleted: false}]->(s);
MATCH (c:Catalog {catalog_id: 'catalog-1'}), (s:SalesCategory {sales_category_id: 'category-2', version_id: 'version-1'}) CREATE (c)-[:HAS_SALES_CATEGORY {catalog_id: 'catalog-1', is_deleted: false}]->(s);

3X_c_f_cfc7c6b2515f4e8800460c4b16f71ddf5dd51e38.png

Clone Query

MATCH (s) where s.version_id = 'version-1'
WITH collect (s) as nodes
call apoc.refactor.cloneNodes(nodes, true, ['version_id'])
YIELD output as o
SET o.version_id = 'version-2'
RETURN count (o)

Here's the result
3X_f_5_f588081af1b564559427461e06b5332fc9fe37c6.png

What I expect

3X_0_9_09217ae6a311f93cbfb7bba270f1f3dc3f85cbbd.png

Questions

  1. How to clone relations while cloning the graph, but prevent creating extra relations between old & new graphs ?
  2. Since the goal is to clone a graph based on a specific property (version_id here) & updating it to a new value/version, I'm excluing the version_id param when cloning & then updating output's version_id (coming from YEILD), is there a better way of doing this ? since I didn't find any info. on overriding a property on refactor.cloneNodes
  3. Is it safe/a good practice to clone nodes (using apoc.refactor) to clone millions of nodes ?

Regards

1 ACCEPTED SOLUTION

jackson1
Node Link

I can answer your first question, with apoc.refactor.cloneSubgraph: apoc.refactor.cloneSubgraph - APOC Documentation

e.g.

MATCH (s) where s.version_id = 'version-1'
WITH collect (s) as nodes
call apoc.refactor.cloneSubgraph(nodes)
YIELD output as o
SET o.version_id = 'version-2'
RETURN count (o)

There's potential to tune the procedure with the config argument, but I haven't looked too much into it.

View solution in original post

1 REPLY 1

jackson1
Node Link

I can answer your first question, with apoc.refactor.cloneSubgraph: apoc.refactor.cloneSubgraph - APOC Documentation

e.g.

MATCH (s) where s.version_id = 'version-1'
WITH collect (s) as nodes
call apoc.refactor.cloneSubgraph(nodes)
YIELD output as o
SET o.version_id = 'version-2'
RETURN count (o)

There's potential to tune the procedure with the config argument, but I haven't looked too much into it.