Skip to content

Instantly share code, notes, and snippets.

@Torenable
Created October 14, 2016 00:50
Show Gist options
  • Save Torenable/958e4f3e4d32443d9695e18d825865d8 to your computer and use it in GitHub Desktop.
Save Torenable/958e4f3e4d32443d9695e18d825865d8 to your computer and use it in GitHub Desktop.
Dedup relationships with identical properties in Cypher
MATCH (a) -[r]- (s)
// use two `WITH` clauses to build a sorted list
WITH r, a // a for group by
ORDER BY id(r)
WITH type(r) AS t, collect(r) AS coll, a
// identify duplications
WITH t, reduce(s = [], x IN coll|
CASE any(y IN coll WHERE id(x) > id(y)
AND y.KEY1 = x.KEY1
AND y.KEY2 = x.KEY2)
WHEN true THEN s + x
ELSE s
END
) AS del, a
FOREACH(x IN del| DELETE x)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment