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.

Looking for a way to use merge in the case of more than 2 relationships

admin3
Node Clone

I have four "generic labels":

  1. EngineCapacity
  2. HorsePower
  3. Transmission
  4. NumberOfDoors

Now I want to connect these nodes to my Car nodes.
But I don't want to connect them straight onto to it. I first connect them to a Combo node and then that is connected to the Car.

So, I first need to make sure the "generic" nodes exists, then make sure the "combo" node exists and then make sure the "combo" node is connected to the car.

I tried this

with 13900 as engineCapacity, 160 as horsePower, 'automatic' as transmission, 5 as nDoors

merge (engineNode:EngineCapacity {value:engineCapacity})
merge (hpNode:HorsePower {value:horsePower})
merge (transNode:Transmission {value:transmission})
merge (doorsNode:NumberOfDoors {value:nDoors})
merge (carNode:Car {value:'VW Golf mk6 1.4 TSI 160BHP'})

with engineNode, hpNode, transNode, doorsNode, carNode

merge (combo:Combo)<-[:IN_COMBO]-(engineNode),
(combo)<-[:IN_COMBO]-(hpNode),
(combo)<-[:IN_COMBO]-(transNode),
(combo)<-[:IN_COMBO]-(doorsNode),
(combo)-[:SPECIFIES_CAR]->(carNode)

which obviously doesn't work.

Then I tried

with 13900 as engineCapacity, 160 as horsePower, 'automatic' as transmission, 5 as nDoors

merge (engineNode:EngineCapacity {value:engineCapacity})
merge (hpNode:HorsePower {value:horsePower})
merge (transNode:Transmission {value:transmission})
merge (doorsNode:NumberOfDoors {value:nDoors})
merge (carNode:Car {value:'VW Golf mk6 1.4 TSI 160BHP'})

with engineNode, hpNode, transNode, doorsNode, carNode

match (combo:Combo)<-[:IN_COMBO]-(engineNode),
(combo)<-[:IN_COMBO]-(hpNode),
(combo)<-[:IN_COMBO]-(transNode),
(combo)<-[:IN_COMBO]-(doorsNode),

with count(combo) as counter, engineNode, hpNode, transNode, doorsNode, carNode
case counter
	when 0 then create(combo2:Combo)<-[:IN_COMBO]-(engineNode),
        (combo2)<-[:IN_COMBO]-(hpNode),
        (combo2)<-[:IN_COMBO]-(transNode),
        (combo2)<-[:IN_COMBO]-(doorsNode)
end
merge (combo2)-[:SPECIFIES_CAR]->(carNode)

which again doesn't work.

Any ideas how to handle this?
Thanks

1 ACCEPTED SOLUTION

Thanks for providing good amount of details in your question. But it'd be more helpful if you could provide the errors you're getting in both the ways.
Btw, for now I solved it by merging each relation separately.

with 13900 as engineCapacity, 160 as horsePower, 'automatic' as transmission, 5 as nDoors

merge (engineNode:EngineCapacity {value:engineCapacity})
merge (hpNode:HorsePower {value:horsePower})
merge (transNode:Transmission {value:transmission})
merge (doorsNode:NumberOfDoors {value:nDoors})
merge (carNode:Car {value:'VW Golf mk6 1.4 TSI 160BHP'})

with engineNode, hpNode, transNode, doorsNode, carNode

merge (combo:Combo)<-[:IN_COMBO]-(engineNode)
merge (combo)<-[:IN_COMBO]-(hpNode)
merge (combo)<-[:IN_COMBO]-(transNode)
merge (combo)<-[:IN_COMBO]-(doorsNode)
merge (combo)-[:SPECIFIES_CAR]->(carNode)

Hope it helps

View solution in original post

3 REPLIES 3

Thanks for providing good amount of details in your question. But it'd be more helpful if you could provide the errors you're getting in both the ways.
Btw, for now I solved it by merging each relation separately.

with 13900 as engineCapacity, 160 as horsePower, 'automatic' as transmission, 5 as nDoors

merge (engineNode:EngineCapacity {value:engineCapacity})
merge (hpNode:HorsePower {value:horsePower})
merge (transNode:Transmission {value:transmission})
merge (doorsNode:NumberOfDoors {value:nDoors})
merge (carNode:Car {value:'VW Golf mk6 1.4 TSI 160BHP'})

with engineNode, hpNode, transNode, doorsNode, carNode

merge (combo:Combo)<-[:IN_COMBO]-(engineNode)
merge (combo)<-[:IN_COMBO]-(hpNode)
merge (combo)<-[:IN_COMBO]-(transNode)
merge (combo)<-[:IN_COMBO]-(doorsNode)
merge (combo)-[:SPECIFIES_CAR]->(carNode)

Hope it helps

Wow it works
I can't believe it was that simple as it took me a couple of hours and I gave up.
It's just a concept to begin with which I'm not sure I'm going to use.

Thanks!

Glad I could help 🙂