Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
02-24-2021 11:03 AM
I am creating relations between nodes n and l.
Sometimes duplicate relations occur.
If a relation of this type already exists, it should merge.
So to be clear: only the relation should merge - not the nodes.
Unfortunately I have not found something like apoc.refactor.mergeRels, som i am using mergeNodes.
I get this exception, that i need to delete the relationships first. But how do I integrate a deletion of relationships into this query?
Here are the query that gives the exception (same as above from picture)
MATCH(n) WHERE ID(n) = {fromNodeId}
MATCH(l) WHERE ID(l) = {toNodeId}
CREATE (n)-[r:{relationType}]->(l)
WITH n,l
MATCH (n)-[r:{relationType}]->(l)
WITH n ORDER BY n.creationTime DESC
WITH collect(n) AS origin
CALL apoc.refactor.mergeNodes(origin, { properties: 'discard', mergeRels:true})
YIELD node
RETURN node
Thank you!
02-24-2021 11:28 AM
You can use the command
MATCH (n:yourNode)..
DETACH DELETE n
Andy
08-30-2021 07:57 AM
That's a good method
02-24-2021 12:20 PM
EDIT:
I have found apoc.merge.relationship
The problem of duplicate relations continues
WITH n,l
CALL apoc.merge.relationship
(
n,'{relationType}',{},{},l,{}
)
YIELD rel
RETURN rel
Now even more so, as relation between nodes that are neither (n) nor (l) will be created and duplicated
05-21-2021 06:57 PM
Try this:
MATCH (a:Name)-[r]->(b:Name)
WITH a, collect(r) as rels, b
WHERE size(rels) > 1
UNWIND tail(rels) as rel
DELETE rel
return a, b
05-21-2021 06:13 PM
I'm getting this same error message while trying to use:
CALL apoc.refactor.mergeNodes([n, n2])
to merge duplicate nodes. Did you find a solution?
10-14-2022 07:04 AM
Same problema with apoc.refactor.mergeNodes([n1,n2])
10-14-2022 08:24 AM
To check the if the relationship already exists between two nodes and if it already exists then it should merge the relationship not the Nodes.
I have faced same issue of duplicate relationships getting created between two nodes in Knowledge Graph.
Here is my simple approach for same..
MATCH (n) WHERE ID(n) = {$fromNodeId},
MATCH (l) WHERE ID(l) = {$toNodeId}
WHERE NOT EXISTS ((n)-[:$relationType]->(l))
MERGE (n)-[:$relationType]->(l)
Thank you.
10-14-2022 02:21 PM
Every time you run this query, you will create a new relationship with the 'create' statement. I suggest you use 'merge' instead. You can them eliminate the 'match' too.
MATCH(n) WHERE ID(n) = {fromNodeId}
MATCH(l) WHERE ID(l) = {toNodeId}
MERGE (n)-[r:{relationType}]->(l)
WITH n ORDER BY n.creationTime DESC
WITH collect(n) AS origin
CALL apoc.refactor.mergeNodes(origin, { properties: 'discard', mergeRels:true})
YIELD node
RETURN node
I am not familiar with the syntax on line 3 that specifies the relationship type. Node labels and relationship types can not be parameterized.
All the sessions of the conference are now available online