cancel
Showing results for 
Search instead for 
Did you mean: 

create a new node based on the common property found in the existing nodes

Hi All,

I am trying to create a new node based on the common property found in the existing nodes. Say for example, I have two nodes Car1 and Car2. Both have the same property color:'blue'. Now I want to create a new node called Color and create a relationship between the Cars and the Color.

I am running the below query to achieve the same but I getting the memory error.

MATCH (n:Car)
UNWIND n.color AS color
WITH  color, collect(n) AS cars
MERGE (c:Color {car_color:color})
WITH c, cars
UNWIND cars AS n
WITH c,n
MERGE (n)-[:GROUP_TYPE]->(c);
 
But as I have 10k nodes, running this command is throwing the memory error. 
 
'The allocation of an extra 273.2 MiB would use more than the limit 250.0 MiB. Currently using 2.8 MiB. dbms.memory.transaction.global_max_size threshold reached'
 
Can someone help me optimize the query. Thanks a lot
1 ACCEPTED SOLUTION

glilienfield
Ninja
Ninja

Is your ‘color’ property really an array, as you are unwinding it?  I will provide solutions for both an array and not an array. 
You can approach it the way you did by collecting all cars by color and processing each color in full, but this will require a lot of memory if the database is not small. Instead, I suggest processing each car one-by-one. 

MATCH (n:Car)
MERGE (c:Color {car_color: n.color})
MERGE (n)-[:GROUP_TYPE]->(c)
 
if a car’s color is really an array, you could use a ‘forEach’ loop to process each color value of each car. 

MATCH (n:Car)
WITH n.Color as carColors
FOREACH (i in carColors | 
MERGE (c:Color {car_color: i})
MERGE (n)-[:GROUP_TYPE]->(c))
 

 

View solution in original post

4 REPLIES 4

Cobra
Ninja
Ninja

Hello @HaribaskarS 🙂

This query should solve your issue (you will need APOC plugin):

CALL apoc.periodic.iterate("
    MATCH (c:Car) RETURN c
    ", "
    MERGE (c)-[:GROUP_TYPE]->(:Color {car_color: c.color)
    ", {batchSize: 10000, parallel:true}
)

Regards,
Cobra

glilienfield
Ninja
Ninja

Is your ‘color’ property really an array, as you are unwinding it?  I will provide solutions for both an array and not an array. 
You can approach it the way you did by collecting all cars by color and processing each color in full, but this will require a lot of memory if the database is not small. Instead, I suggest processing each car one-by-one. 

MATCH (n:Car)
MERGE (c:Color {car_color: n.color})
MERGE (n)-[:GROUP_TYPE]->(c)
 
if a car’s color is really an array, you could use a ‘forEach’ loop to process each color value of each car. 

MATCH (n:Car)
WITH n.Color as carColors
FOREACH (i in carColors | 
MERGE (c:Color {car_color: i})
MERGE (n)-[:GROUP_TYPE]->(c))
 

 

Thanks a lot!! that works

Thanks for the reply Cobra,

The query which you mentioned is creating a new Color node for each of the car nodes. I just want to create distinct color nodes so that all the car nodes that have the same color will point to a single color node.