Help needed with Cypher query

The query in Kafka Connect aims to achieve two goals:

  1. Assign multiple labels to a node.
  2. Establish relationships between nodes.

Could help me whether the below query satisfy both the needs?

neo4j.topic.cypher.kafka_car = 
WITH event
CALL {
	WITH event
	MERGE (n:Manufacturer {id: event.id})
	ON CREATE SET n += event
	ON MATCH SET n += event
	WITH n
	SET n:Country:Make:Color
	FOREACH ( _ IN CASE WHEN n.Make = 'BMW' THEN [1] ELSE [] END | SET n:Bmw:Germany)
	FOREACH ( _ IN CASE WHEN n.Make = 'Mercedes' THEN [1] ELSE [] END | SET n:Mercedes:Germany)
	FOREACH ( _ IN CASE WHEN n.Make = 'Land Rover' THEN [1] ELSE [] END | SET n:LandRover:England)
	FOREACH ( _ IN CASE WHEN n.Make = 'Tesla' THEN [1] ELSE [] END | SET n:Tesla:America:Black)
	FOREACH (q IN [(q:Manufacturer {ParentName: n.Name})-[*0]-() | q] | MERGE (n)-[:Parent_To]->(q))
	FOREACH (w IN [(w:Engine {Chasis: n.SerialNumber})-[*0]-() | w] | MERGE (n)-[:Has_Engine]->(w))
}

The query intends to assign the labels "Country," "Make," and "Color" to a node by structuring the data as "Country:Make:Color." Also is this the optimised query?

Additionally, I need to remove any existing relationships for a node before establishing the current relationship, ensuring only the latest relationship is retained. How do I achieve it with the above query?

Note, when the make is either of the four car makes, you are setting the labels as specified and removing the three set earlier: Country, Make, and Color.

What is your goal with the last two forEach clauses? The way the pattern is written with a zero path length you will always get the q or w nodes. I don’t think there is a need to have a pattern the way they are written.

You can use the following if you want to remove all the relationships connected to node ‘n’

Match(n:XYZ{id:100})
Match(n)-[r]-()
Delete r

Or, using your forEach pattern

Match(n:XYZ{id:100})
ForEach(x in [(n)-[r]-()|r] | delete x)

If the car's make matches, will it erase the previous labels for Country, Make, and Color? What I actually need is for a node to hold onto labels for Manufacturer, Country, Make, and Color, and then add any specified labels if a match is detected in a FOREACH statement.

For instance, a node with the make "BMW" should have labels for Manufacturer, Country, Make, Color, BMW, and Germany. Similarly, a node with the make "Tesla" should have labels for Manufacturer, Country, Make, Color, Tesla, America, and Black.

neo4j.topic.cypher.kafka_car = 
WITH event
CALL {
	WITH event
	MERGE (n:Manufacturer {id: event.id})
	ON CREATE SET n += event
	ON MATCH SET n += event
	WITH n
	FOREACH ( _ IN CASE WHEN n.Make = 'BMW' THEN [1] ELSE [] END | SET n:Country:Make:Color:Bmw:Germany)
	FOREACH ( _ IN CASE WHEN n.Make = 'Mercedes' THEN [1] ELSE [] END | SET n:Country:Make:Color:Mercedes:Germany)
	FOREACH ( _ IN CASE WHEN n.Make = 'Land Rover' THEN [1] ELSE [] END | SET n:Country:Make:Color:LandRover:England)
	FOREACH ( _ IN CASE WHEN n.Make = 'Tesla' THEN [1] ELSE [] END | SET n:Country:Make:Color:Tesla:America:Black)
	FOREACH ( x IN [(n)-[r]-()|r] | DELETE x)
	FOREACH ( q IN [(q:Manufacturer {ParentName: n.Name})-[*0]-() | q] | MERGE (n)-[:Parent_To]->(q))
	FOREACH ( w IN [(w:Engine {Chasis: n.SerialNumber})-[*0]-() | w] | MERGE (n)-[:Has_Engine]->(w))
}

Will the revised query effectively delete the relationship, ensuring only the latest relationship is retained and multiple node labels is set as my requirement?

It should.

Btw, the original Manufacturer label is lost after this query completes. Is that intended?

No, it should not. I need "Manufacturer" too. So, I need to add it in every FOREACH for setting additional labels right.

Also, FOREACH ( x IN [(n)-[r]-()|r] | DELETE x) will this ensure delete the old relationships for the particular incoming node?

Yes you will. You could use APOC procedures to add/remove individual labels.

Using them will make your code dependent on the APOC library.

There are also procedures to perform conditional logic.

I prefer the simplest approach that uses cypher , so I only use the APOC when necessary.

Sounds good. Thanks for your help. Let me try it.