johndodo
johndodo

Reputation: 18271

Neo4j: how to join multiple relations into a summarized one?

I have a graph where there are many relations between some of the nodes, which makes browsing these graphs difficult. Is it possible to replace such multiple relations with a single "aggregated" one, possible of another type?

Ideally, I would like to join just those relations where there are more than 10 relations between a node pain - but anything will help.

Upvotes: 1

Views: 55

Answers (1)

Bruno Peres
Bruno Peres

Reputation: 16365

I think you can do something like:

// Match the pattern
MATCH (a:Node)-[t:REL_TYPE]->(b:Node)
// pass to the next context nodes a, b, list of t 
// where number of relations between a and b is greater than 10
WITH a, b, collect(t) AS ts, count(t) AS count
WHERE count > 10
// create new relation between a and b nodes
CREATE (a)-[:OTHER_REL_TYPE]->(b)
// pass list of old relations to the next context
WITH ts
// unwind & delete old relations
UNWIND (ts) AS t
DELETE t
RETURN *

Upvotes: 1

Related Questions