2
votes

I have a Neo4j database comprising Film and Person nodes connected by ACTED_IN relationships. Using APOC, I've managed to create a set of virtual ACTED_WITH relationships directly between Person nodes reflecting the fact that they're indirectly related through a Film in which they both appeared:

MATCH (a:Person)-[:ACTED_IN]->()<-[:ACTED_IN]-(b:Person)
WITH a, b
CALL apoc.create.vRelationship(a, 'ACTED_WITH', {}, b) YIELD rel
RETURN a, rel, b

This seems to capture the indirect relationship I'm looking for. Now I want to stream the results of this to Gephi for visualisation. But the relevant APOC function takes a paths argument. So this works:

MATCH path = ()-[:ACTED_IN]->()
WITH collect(path) AS paths
CALL apoc.gephi.add(null, 'workspace0', paths) YIELD nodes, relationships, time
RETURN nodes, relationships, time

How can I create a set of paths from the virtual relationships yielded in the first code block for passing to Gephi as in the second? (Or is there a better way to deal with this kind of case?)

1

1 Answers

0
votes

The data for the apoc.gephi.add procedure can be instanceof: Node, Relationship, Path, Iterable, Map, Iterator, Object.

So it should work like this:

MATCH (a:Person)-[:ACTED_IN]->()<-[:ACTED_IN]-(b:Person) WHERE ID(a) > ID(b) 
WITH DISTINCT a, b
CALL apoc.create.vRelationship(a, 'ACTED_WITH', {}, b) YIELD rel AS rel1
CALL apoc.create.vRelationship(b, 'ACTED_WITH', {}, a) YIELD rel AS rel2
WITH collect(rel1) + collect(rel2) + collect(a) + collect(b) AS data
CALL apoc.gephi.add(null, 'workspace0', data) YIELD nodes, relationships, time
RETURN nodes, relationships, time

Update: If you need weights, then you can simply aggregate by the films:

MATCH (a:Person)-[:ACTED_IN]->(M:Movie)<-[:ACTED_IN]-(b:Person) WHERE ID(a) > ID(b) 
WITH a, b, 
     count(M) as vWeight
CALL apoc.create.vRelationship(a, 'ACTED_WITH', {weight: vWeight}, b) YIELD rel AS rel1
CALL apoc.create.vRelationship(b, 'ACTED_WITH', {weight: vWeight}, a) YIELD rel AS rel2
WITH collect(rel1) + collect(rel2) + collect(a) + collect(b) AS data
CALL apoc.gephi.add(null, 'workspace0', data) YIELD nodes, relationships, time
RETURN nodes, relationships, time