Faaiz
Faaiz

Reputation: 685

MERGE the "Property Key" of one Label into another Label

(:Label1 {emp_id: 1, name:"abc"})

(:Label1 {emp_id: 2, name:"xyz"})

(:Label1 {emp_id: 3, name:"pqr"})

INDEX ON :Label1(emp_id)

(:Label2 {emp_id: 1, car_name:"A"})

(:Label2 {emp_id: 1, car_name:"B"})

(:Label2 {emp_id: 1, car_name:"C"})

(:Label2 {emp_id: 2, car_name:"A"})

(:Label2 {emp_id: 2, car_name:"D"})

(:Label2 {emp_id: 3, car_name:"B"})

(:Label2 {emp_id: 3, car_name:"F"})

Question: I need to keep only Label1 and need to merge the key "car_name" in Label1. How to do it ?

Note: The nodes in both labels are in millions and Label2 nodes are almost twice the Label1

Upvotes: 0

Views: 38

Answers (1)

Tomaž Bratanič
Tomaž Bratanič

Reputation: 6524

What I would do in your place is create a new label Car which would contain information about car_name and would be connected to nodes labeled Label1. Later I would delete all Label2 nodes, because you don't need them anymore.

As you have millions of nodes it is better to use apoc.periodic.iterate for batching.

CALL apoc.periodic.iterate(
"MATCH (l:Label1), (l2:Label2)
 WHERE l.emp_id = l2.emp_id return l,l2",
"MERGE (c:Car{car_name: l2.car_name})
 MERGE (l)-[:HAS_CAR]->(c)",
 {batchSize:5000, iterateList:true})

And now delete all nodes with a label2

CALL apoc.periodic.iterate(
'MATCH (l:Label2) RETURN l',
'DETACH DELETE l',
{batchSize:1000,parallel:true})

Upvotes: 2

Related Questions