Neo4j:从 APOC 虚拟关系创建 paths/subgraph 以流式传输到 Gephi

Neo4j: Create paths/subgraph from APOC virtual relationships for streaming to Gephi

我有一个 Neo4j 数据库,其中包含 FilmPerson 节点,这些节点通过 ACTED_IN 关系连接。使用 APOC,我设法在 Person 节点之间直接创建了一组虚拟 ACTED_WITH 关系,反映了它们通过 Film 间接相关的事实,它们都出现在其中:

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

这似乎捕捉到了我正在寻找的间接关系。现在我想将这个结果流式传输到 Gephi 进行可视化。但是相关的 APOC 函数需要一个 paths 参数。所以这有效:

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

如何从第一个代码块中产生的虚拟关系创建一组路径,以便像在第二个代码块中一样传递给 Gephi? (或者有更好的方法来处理这种情况?)

apoc.gephi.add 过程的数据可以是 instanceof:Node、Relationship、Path、Iterable、Map、Iterator、Object。

所以它应该像这样工作:

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

更新:如果你需要权重,那么你可以简单地按电影聚合:

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