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.

Create a edge property using the properties of the 2 nodes it is connected to

I have been trying to create a new edge property using the property of the nodes it has been connected to but am unable to complete the cypher query.

Database: Using the neo4j gds examples airports database

Task: set a property "avgPageRank" to the "has_route" relation from the two nodes its connected to having the property page rank.

````match (p:Airport)-[r:HAS_ROUTE]->(q:Airport)
with collect(r) as routes
foreach(route in routes | SET route.avgPageRank = p.pagerank + q.pagerank)```

Here I am unable to get the access to p and q, the starting airport and the ending airport

1 ACCEPTED SOLUTION

glilienfield
Ninja
Ninja

The variables ‘p’ and ‘q’ are out of scope when you try to reference them in the forEach block. You need to pass them in your ‘with’ clause.

I don’t see the need for the ‘collect’, followed by ‘forEach’, as you are setting each relationship independently based on their end nodes.

this should work just as well:

match (p:Airport)-[r:HAS_ROUTE]->(q:Airport)

SET r.avgPageRank = p.pagerank + q.pagerank

View solution in original post

1 REPLY 1

glilienfield
Ninja
Ninja

The variables ‘p’ and ‘q’ are out of scope when you try to reference them in the forEach block. You need to pass them in your ‘with’ clause.

I don’t see the need for the ‘collect’, followed by ‘forEach’, as you are setting each relationship independently based on their end nodes.

this should work just as well:

match (p:Airport)-[r:HAS_ROUTE]->(q:Airport)

SET r.avgPageRank = p.pagerank + q.pagerank