EliasAtBerlin
EliasAtBerlin

Reputation: 65

Aggregating relationships via cypher

I am fairly certain I have seen it somewhere but all keywords I have tried came up empty.

I have a graph that connects persons and companies via documents:

(:Person/:Company)-[ ]-(:Document)-[ ]-(:Person/:Company)

What I would like to do is return a graph that shows the connection between persons and companies directly with the relationship strength based on the number of connections between them.

I get the data with

MATCH (p)-[]-(d:Document)-[]-(c)
WHERE p:Person or p:Company and c:Person or c:Company
WITH p,c, count(d) as rel
RETURN p,rel,c

However in the Neo4J-Browser, the nodes appear without any relationships. Is there a way to achieve this or do I have to create some kind of meta relationship?

Upvotes: 2

Views: 141

Answers (1)

InverseFalcon
InverseFalcon

Reputation: 30397

If you install APOC Procedures, you'll be able to create virtual relationships which are used for visualization but aren't actually stored in the db.

MATCH (p)-[]-(d:Document)-[]-(c)
WHERE (p:Person or p:Company AND c:Person or c:Company)
 AND  id(p) < id(c)
WITH p,c, count(d) as relStrength
CALL apoc.create.vRelationship(p,'REL',{strength:relStrength}, c) YIELD rel
RETURN p,rel,c

I also added a predicate on ids of p and c so you don't repeat the same two nodes with p and c switched.

Upvotes: 0

Related Questions