cancel
Showing results for 
Search instead for 
Did you mean: 

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

nebuchadnezzar
Node Link

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

PhaniD
Neo4j
Neo4j

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

Nodes 2022
Nodes
NODES 2022, Neo4j Online Education Summit

On November 16 and 17 for 24 hours across all timezones, you’ll learn about best practices for beginners and experts alike.