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.

Duplicate a subgraph

Hello,
can I duplicate the following graph in neo4j? and if it is possible, then what cyphers can can me?


Have a lovely day 🙂

1 ACCEPTED SOLUTION

You can try this query to duplicate all nodes and their relationships:

MATCH (n)
WITH collect(n) AS nodes
CALL apoc.refactor.cloneSubgraph(nodes)
YIELD input, output, error
RETURN input, output, error;

You can also add a WHERE clause to select nodes and their relationships from of a subgraph:

MATCH (n)
WHERE n.name = "Graph1"
WITH collect(n) AS nodes
CALL apoc.refactor.cloneSubgraph(nodes)
YIELD input, output, error
RETURN input, output, error;

There is more documentation here.

View solution in original post

25 REPLIES 25

Hello @verachkaverachka

If you want to duplicate this graph in another database?

You can use the Graph Data Science plugin.

First, you create the in-memory graph:

CALL gds.graph.project('graph', '*', '*')
YIELD graphName AS graph, nodeProjection, nodeCount AS nodes, relationshipProjection, relationshipCount AS rels

Then, you export the graph to a new database:

CALL gds.graph.export('graph', { dbName: 'mydatabase' })

Finally, you can create the new database and use it:

:use system
CREATE DATABASE mydatabase;
:use mydatabase

Regards,
Cobra

thank you so much for your answers
and if I want to duplicate this graph in the same databse? is it possible?
and if it is, then which query can help me?

You can have a look at APOC plugin with clone nodes functions or clone subgraph functions. I think the apoc.refactor.cloneNodesWithRelationships() function is what you need.

Maybe this query will help you:

MATCH (n)
CALL apoc.refactor.cloneNodesWithRelationships([n])
YIELD input, output
RETURN *

Hello ,I've installed the APOC library, and tried the suggested query.
this one:
MATCH (n)
CALL apoc.refactor.cloneNodesWithRelationships([n])
YIELD input, output
RETURN *what i've tried to get was this whole graph twice , like to saparated similar graphs:


what i've actually got is something weird that looks like this:

What I try to do is find a query that will duplicate my original graph and i'll gey two of these.
What query can I do to solve it?
have a great day.
Vera

Can you share your dataset please?

Where get I get it from?(the dataSet) so I can send it here?

I can try to export the data I have to one of these:

or dataSet means something else?

The dataset is the data, can you share queries to recreate the graph?

I create the graph with the help of java (Springboot Dao) it starts with an entity of CellType that represents the type of a node I'm going to use(operation or input ) and with the first celltype(that must be a type of input) I connect it to a "real cell" and I call it "cellInstaces".(they also will appear in my frontend later without all the attributes). This cellInstance is going to be connected to another cellInstance from Celltype(again cellInstance must have a type-cellType) of function because in the end my purpose is to run a simulator on this graph and calculate stuff from function to function. Then, I connect an entity named a subgraphInstance to cellInstance(that represents the input)to my cellInstance. and all the nodes that are connected to this first cellInstance we will see them.
the query I use to create cell instance is this one


The query to connect between cellInstances is

I did use a query when I wanted to get my graph that looks like this:

and if you meant they tables that represents the data of this graph for example:


it look like this:
{
"identity": 0,
"labels": [
"CellInstance"
],
"properties": {
"yCoordinate": 5.6,
"xCoordinate": 3.7
}
}
{
"identity": 20,
"labels": [
"SubGraphInstance"
],
"properties": {
"name": "Graph1"
}
}
{
"identity": 77,
"labels": [
"Variable"
],
"properties": {
"variableName": "x"
}
}
{
"identity": 78,
"labels": [
"Function"
],
"properties": {
"expression": "x"
}
}
{
"identity": 79,
"labels": [
"CellType"
],
"properties": {
"name": "Input Cell",
"transformType": "INPUT_TO_ANALOG"
}
}
{
"identity": 84,
"labels": [
"CellInstance"
],
"properties": {
"yCoordinate": 6.6,
"xCoordinate": 4.7
}
}
{
"identity": 85,
"labels": [
"Variable"
],
"properties": {
"variableName": "x"
}
}
{
"identity": 86,
"labels": [
"Variable"
],
"properties": {
"variableName": "y"
}
}
{
"identity": 87,
"labels": [
"Function"
],
"properties": {
"expression": "x+y"
}
}
{
"identity": 88,
"labels": [
"CellType"
],
"properties": {
"name": "Addition ",
"transformType": "ANALOG_TO_ANALOG"
}
}
{
"identity": 89,
"labels": [
"CellInstance"
],
"properties": {
"yCoordinate": 6.6,
"xCoordinate": 4.7
}
}
{
"identity": 90,
"labels": [
"Connection"
],
"properties": {
"delay": 1
}
}
{
"identity": 91,
"labels": [
"Connection"
],
"properties": {
"delay": 1
}
}

You can try this query to duplicate all nodes and their relationships:

MATCH (n)
WITH collect(n) AS nodes
CALL apoc.refactor.cloneSubgraph(nodes)
YIELD input, output, error
RETURN input, output, error;

You can also add a WHERE clause to select nodes and their relationships from of a subgraph:

MATCH (n)
WHERE n.name = "Graph1"
WITH collect(n) AS nodes
CALL apoc.refactor.cloneSubgraph(nodes)
YIELD input, output, error
RETURN input, output, error;

There is more documentation here.

I will try it and let you know today! ,thank you so so much for trying to help me understand !!

yeahi! it works!
thank you so much for helping me

Hello!
Can I duplicate my graph by his name or id with a query?for example if I have a few different subgraphs(When I say subgraphisntance i mean the node and all his connected elements) and I want to duplicate all the elements of a specific subgraph in my DB and not duplicate them all, is there a way to do it with apoc? Like you showed me with the first query but this time finding the whole subgraph by his id or name?
Vera

Hello @verachkaverachka

It's in the answer I gave you, the second query with the WHERE clause should do what you want.

Regards,
Cobra

I tried the second one, and got only 1 duplicated node which was Graph1 that represents subGraphInstance, my problem is that i got it without all the connected elements of this node and I need them too

All the nodes of the subgraph must be tagged with the property, not only one node.

Can you please elaborate how will the second query look like if will tag the other nodes there??

Also is there a way to create a query where I can choose specific nodes from my graph and duplicate them and get their connections ?

I already gave you all information in other topic:

CALL gds.graph.project(
    "graph",            
    "*",             
    "*"               
)
YIELD graphName AS graph, nodeProjection, nodeCount AS nodes, relationshipProjection, relationshipCount AS rels
CALL gds.wcc.write('graph', { writeProperty: 'componentId' })
YIELD nodePropertiesWritten, componentCount;
CALL gds.graph.drop('graph', false) YIELD graphName;

So after you can change the 0 by the componentId you want:

MATCH (n)
WHERE n.componentId = 0
WITH collect(n) AS nodes
CALL apoc.refactor.cloneSubgraph(nodes)
YIELD input, output, error
RETURN input, output, error;

If you know which nodes you want to duplicates, you can change the WHERE clause in the previous query to select only the nodes you want.

Hey Cobra, thank you for your answers!
I only have the apoc library my version of neo4j is 3.5.30
so I tried to create a new graph, a simple one that looks loke this:


and I tried to duplicate query like this:

MATCH (n)
WHERE n.name = "Graph1" AND WHERE ID(type) ="CellType"
AND WHERE ID(type)="Function" AND WHERE ID(type) = "cellInstance" And WHERE ID(type) ="Connection"
AND WHERE ID(type) = "Variable"
WITH collect(n) AS nodes
CALL apoc.refactor.cloneSubgraph(nodes,[rel in relationships Where type(rel) = ‘From_Graph’ AND Where type(rel) =’FROM_TYPE’ Where type(rel) = ‘CELL_TYPE_FUNCTION’ AND Where type(rel) = ‘FROM_CELL’ AND Where type(rel) = ‘FUNCTION_INPUT_VARIABLE’ AND Where type(rel)= ‘TO_CELL’,AND Where type(rel) = ‘Functions_VARS’ ])
YIELD input, output, error
RETURN input, output, error;
still doesn't work for me 😞
any suggestions?

You must use the latest version of Neo4j which is 4.4.6 then install GDS plugin on the database to use the queries I gave you.

Isn't there any other way to do it with this query you offered and use apoc only?
MATCH (n)
WHERE n.name = "Graph1"
WITH collect(n) AS nodes
CALL apoc.refactor.cloneSubgraph(nodes)
YIELD input, output, error
RETURN input, output, error;

No because you need GDS to tag nodes correctly. Moreover Neo4j 3.5 is no longer maintained so you must upgrade.

Ok, so i'll try to upgrade it and use the queries you offered. i'll let you know if I succeeded
Cobra thank you so much for trying to help me I really
appriciate it.
have a lovely weekend!