delete duplicate relationships using cypher query

2,319 views
Skip to first unread message

Priyadarshi Lahiri

unread,
Aug 13, 2013, 2:32:42 AM8/13/13
to ne...@googlegroups.com
When I run this query:

START n1=node(7727), n2=node(7730)
MATCH n1-[r:SKILL]->n2 RETURN r

it gives me a list of duplicate relationships that I have between the two nodes. what do I add to the cypher query to iterate over the relationship to keep one relationship and delete the rest?

Michael Hunger

unread,
Aug 13, 2013, 3:35:59 AM8/13/13
to ne...@googlegroups.com
START n1=node(7727), n2=node(7730)
MATCH n1-[r:SKILL]->n2
   SKIP 1
   delete r


Michael

--
You received this message because you are subscribed to the Google Groups "Neo4j" group.
To unsubscribe from this group and stop receiving emails from it, send an email to neo4j+un...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Michael Hunger

unread,
Jul 23, 2014, 6:02:27 AM7/23/14
to Conar Welsh, ne...@googlegroups.com
You'd have to group by the nodes then and collect the relationships into a collection and delete all except the first.

Do something like this 

MATCH (n:SomeLabel)-[r:REL_TYPE]->(m:AnotherLabel)
WITH n, m, collect(r)[1..] as rels
FOREACH (r in rels | DELETE r) 



On Mon, Jul 21, 2014 at 9:22 PM, Conar Welsh <co...@sellwelldesigns.com> wrote:
Would this same query work without specific start points?
i.e. 

MATCH (:SomeLabel)-[r:REL_TYPE]->(:AnotherLabel)
SKIP 1
DELETE r

Basically I am trying to introduce a CREATE UNIQUE, however the data in the graph already has duplicates, so the CREATE UNIQUE is failing due to multiple paths, and I want to clean it up manually.

Rodger

unread,
Jul 25, 2014, 10:28:05 AM7/25/14
to ne...@googlegroups.com, co...@sellwelldesigns.com
Perhaps you could distiguish the relationships with id(r).

Then choose max (id (r))



Dupe rows are a similar problem in Oracle, distinguished using max (rowid).


Perhaps Primary keys on nodes and relationships would solve this kind of issue. 



Reply all
Reply to author
Forward
0 new messages