Search code examples
neo4jneo4j-apoc

Neo4j: use in a projection a virtual relationship with weights


I can create a relationship in my graph, assign weights and write in disk as follows:

MATCH (a1:node1)-[rel1]-(p:other_node)-[rel2]-(a2:node2)
// create the weight
with a1.id as id1, a2.id as id2, count(p) as common_ocurrences
// create the rel
MERGE (a1)-[r:NEW_REL]-(a2)
// use the weights as a property of the created relationship
ON CREATE SET r.counter = common_occurrences
ON MATCH SET r.counter = common_occurrences

// create projection 
CALL gds.graph.project(
 'new-graph',
 'node1',
 {NEW_REL:{
 orientation:'UNDIRECTED',
 properties:'counter'
 }
 }
)
yield *

but what i want is to do the same, without writing the relationship nor the associated weight. for that i tried using virtual relationships like with this method:

MATCH (a1:node1)-[rel1]-(p:other_node)-[rel2]-(a2:node2)
with a1, a2, count(p) as common_occurrences
where a1.id>a2.id
return 
a1, a2, 
apoc.create.vRelationship(a1, 'COAUTHORS_WITH_VIRTUAL2', {weight: common_occurrences}, a2) as REL_VIRTUAL2;

this part seems to work but now i'm quite puzzled about how to use it. if right after the last clause i write something like

CALL gds.graph.project(
 'graph42',
 'node1',
 {REL_VIRTUAL_2:{
 orientation:'UNDIRECTED',
 properties:'common_occurrences'
 }
 }

)
yield *

it says that the relationship can not be found. any help on this?


Solution

  • There are several issues with both of your queries.

    For example, REL_VIRTUAL2 is just a temporary variable name, not a relationship type. COAUTHORS_WITH_VIRTUAL2 is the relationship type you should have used in the projection. However, changing that would still not fix things because GDS does not support projecting virtual nodes or relationships.

    Your first approach should work, but you may need to solve some other issues first. For example, the projection should use ['node1', 'node2'] instead of just 'node1' for the node labels,. And you can replace ON CREATE SET x = y ON MATCH SET x = y with just SET x = y.