How to divide the dependency of a node related with two different nodes in neo4j?

163 Views Asked by At

I have a model in Graph theory of Movies and Actors. The relation between the two is "requires". The graph is shown below. A and B are Movies. 1,2,3,4,5,6 are actors. Movie A requires 1,2,3,4 actors. Movie B requires 4,5,6 actors. We can see that 4 is shared among both movies.

Current Query:

MATCH (m :Movie) -[r :require]-> (a :actor)
RETURN m,r,a;
Current Output

Current Output

Expected Output

I want to display something as below. Here actor 4 is shown once for each movie. Can someone help me fix this ?

Expected Output

2

There are 2 best solutions below

2
On

The visualization logic in Neo4j Browser displays each node only once, therefore you cannot get node 4 two times. A workaround would be using the neo4j APOC library and return virtual nodes as copies of the blue nodes instead of the real node. If you create two virtual nodes out of node 4, the UI considers them being distinct and therefore show two nodes.

0
On

You can do that my grouping the actors by movie and end up with a row of movie, [list of actors ]

MATCH (m:Movie)-[r:require]->(a :actor)
WITH m collect(a) as actors
return m,actors