cancel
Showing results for 
Search instead for 
Did you mean: 

Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.

How to correctly write a query for projection to use GDS collapse paths ?

Hi, everyone!

How to project the following Cypher pattern:

MATCH (c1:Company)<-[:MEMBER_OF]-(e1:employee
)-[*1..3]-(e2:employee
)-[:MEMBER_OF]->(c2:Company) RETURN c1.id as source, c2.id as target

so i can use it in GDS collapse path?

And get from that input graph below (red nodes are (Company), yellow nodes are (employee)

photo_2022-08-09_23-55-08.jpg

 

That graph in result

photo_2022-08-10_01-03-56.jpg

 Any tips on how to improve performance or improve the query are welcome

 

2 REPLIES 2

Collapse path doesn't support variable length paths at this time. As per the documentation - 
Starting from every node in the specified graph, these relationship types are traversed one after the other using the order specified in the configuration. Only nodes reached after traversing every relationship type specified are used as end nodes.


I think you can apply collapse path repeatedly to get what you are looking for. For example, 

CALL gds.graph.project(
 'employee_company',
 ['Company', 'Employee'],
 {
   MEMBER_OF: {
     type: 'MEMBER_OF',
     orientation: 'NATURAL'
   },
   MEMBER_OF_REV: {
     type: 'MEMBER_OF',
     orientation: 'REVERSE'
   },
   FOLLOWING: {
     type: 'FOLLOWING',
     orientation: 'NATURAL'
   }
});

CALL gds.alpha.collapsePath.mutate(
 'employee_company',
 {
   relationshipTypes: ['MEMBER_OF_REV', 'FOLLOWING', 'MEMBER_OF'],
   allowSelfLoops: false,
   mutateRelationshipType: 'COLLAPSED_1HOP'
 }
) YIELD relationshipsWritten;

CALL gds.alpha.collapsePath.mutate(
 'employee_company',
 {
   relationshipTypes: ['MEMBER_OF_REV', 'FOLLOWING', 'FOLLOWING', 'MEMBER_OF'],
   allowSelfLoops: false,
   mutateRelationshipType: 'COLLAPSED_2HOP'
 }
) YIELD relationshipsWritten;

...

 

Thanks for the answer, but this code does not work, and I need one name for mutateRelationshipType