0

Suppose you've got two nodes that represent the same thing, and you want to merge those two nodes. Both nodes can have any number of relations with other nodes.

The basics are fairly easy, and would look something like this:

MATCH (a), (b) WHERE a.id == b.id
MATCH (b)-[r]->()
CREATE (a)-[s]->()
SET s = PROPERTIES(r)
DELETE DETACH b

Only I can't create a relation without a type. And Cypher doesn't support variable labels either. I'd love to be able to do something like

CREATE (a)-[s:{LABELS(r)}]->(o)

but that doesn't work. To create the relation, you need to know the type of the relation, and in this case I really don't.

Is there a way to dynamically assign types to relationships, or am I going to have to query the types of the old relation, and then string concat new queries with the proper types? That's not impossible, but a lot slower and more complex. And this could potentially match a lot of elements and even more relationships, so having to generate a separate query for every instance is going to slow things down quite a lot.

Or is there a way to change the target of the old relationship? That would probably be the fastest, but I'm not aware of any way to do that.

2 Answers 2

2

I think you need to take a look at APOC, especially apoc.create.relationship which enable creating relationships with dynamic type.

Adapting your example, you should end up with something along the line of (not tested):

MATCH (a), (b) WHERE a.id == b.id
MATCH (b)-[r]->(n)
CALL apoc.create.relationship(a, type(r), properties(r), n)
DETACH DELETE b

NB

  • relationships have TYPE and not label
  • the proper cypher statement to delete relationships attached to a node and the node itself is DETACH DELETE (and not DELETE DETACH)

Related resource: https://markhneedham.com/blog/2016/10/30/neo4j-create-dynamic-relationship-type/

1

The APOC procedure apoc.refactor.mergeNodes should be very helpful. That procedure is very powerful, and you need to read the documentation to understand how to configure it to do what you want in your specific situation.

Here is a simple example that shows how to use the procedure's default configuration to merge nodes with the same id:

MATCH (node:Foo)
WITH node.id AS id, COLLECT(node) AS nodes
WHERE SIZE(nodes) > 1
CALL apoc.refactor.mergeNodes(nodes, {}) YIELD node
RETURN node

In this example, I specified an arbitrary Foo label to avoid accidentally merging unwanted nodes. Doing so also helps to speed up the query if you have a lot of nodes with other labels (since they will not need to be scanned for the id property).

The aggregating function COLLECT is used to collect a list of all the nodes with the same id. After checking the size of the list, it is passed to the procedure.

3
  • Interesting. I didn't know about mergeNodes yet. I have heard of apoc, but I thought we weren't using it. Turns out the server does have apoc installed, I just don't have it locally. So I might go with that. Shame I won't be able to keep our queries clean and pure Cypher. Those apoc calls look like they don't belong there, but I guess you've got to if you need to do things like this.
    – mcv
    May 22, 2020 at 0:01
  • Usage of the APOC plug-in is very common, and often necessary. So, I would not be skittish about using it.
    – cybersam
    May 22, 2020 at 0:59
  • I'll give it a try. Although my first approach is still going to be to try to prevent this duplication in the first place.
    – mcv
    May 27, 2020 at 9:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.