Hello, I would be really grateful to those who can help me about a Cypher query issue I'm facing these days. I'm sure I'm missing something not so hard, yet have to figure out how to handle it.
The DB has one of the most simple structure, consider only the following two node entities and the relationship between them.
Nodes:
- Organization {id: generated, name}
- Vendor {id: generated, type)
Relationships:
- INVOICE {id: generated, date: String, summary: String)
Note: initially, every Vendor node instance has (for sure!) an incoming relationship which start node is an instance of the Organization node entity (i.e. (o:Organization)-[:INVOICE]->(v:Vendor) ).
What I'm supposed to do: remove every Vendor node which is duplicate and replace the relationships that previously connected such Vendor nodes with new, equal (relationship properties too, this is what I'm trying to achieve), relationships which have as end node the Vendor for such type and as start node the same Organization of the Vendor node removed, for each Vendor node removed in such a way.
MATCH (org:Organization)-[:INVOICE]->(v:Vendor)
WITH v.type as vendorType, collect(v) AS vendorNodes, collect(org) as organizations
WHERE size(vendorNodes) > 1
WITH vendorNodes, organizations
FOREACH (v2 in tail(vendorNodes) | DETACH DELETE v2)
WITH vendorNodes, vendorNodes[0] as firstNode, organizations
FOREACH (o2 in tail(organizations) | CREATE (o2)-[:INVOICE]->(firstNode))
This query correctly removes Vendor duplicates and add a relationship, for each deleted node, so that the DB stores the same informative content as before the query did run.
I tried several ways to "remember" the relationship and then create the relationship with the correct properties values, but I didn't manage to do it correctly.
Hope someone could help, it should not be hard for those who are more expert than me with the Cypher query language.