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.

Source - Target - Path finding

Chiara
Node Link

Dear All,

I am new to neo4j. I am using it to store strongly related biological data (nodes are biological entities, edges are connecting verbs of two entities). I have implemented a python Panel User Interface through which Users can search the database based on queries i have implemented. One of the search options is source-target path finding. I am currently using the Dijkstra algorithm from the GDS library. My edges are directed and i do not have weights. My question is the following: Is it possible to get the edge properties from the edges that connect the nodes in the path as return values as well? Because the edge properties yield important information and i would like to visualise the path as well with matplotlib. My current query is this:

MATCH (source:Entity {name: $source}), (target:Entity {name: $target})
CALL gds.shortestPath.dijkstra.stream('Knowledge1', {
sourceNode: source,
targetNode: target })
YIELD index, sourceNode, targetNode, nodeIds, path
RETURN
index,
gds.util.asNode(sourceNode).name AS sourceNodeName,
gds.util.asNode(targetNode).name AS targetNodeName,
[nodeId IN nodeIds | gds.util.asNode(nodeId).name] AS nodeNames
ORDER BY index
Thank you in advance
1 ACCEPTED SOLUTION

A relationship object has a startNode, an endNide, properties, a type, and an id. All of these are accessible via methods.

The easiest method to extract attributes from a list into a new list is with list comprehension. It allows you to interate through all list elements, apply an optional filter predicate, and apply an optional transformation. The result is a new list. Your current query is using list comprehension to extract the nodeNames. The following will give you a list of the properties from each relationship in ‘path’. Each relationship’s properties is a map, so you will get a list of maps.

[x in relationships(path) | properties(x)]

https://neo4j.com/docs/cypher-manual/current/syntax/lists/#cypher-list-comprehension

View solution in original post

3 REPLIES 3

You can get the relationships from a path ‘p’ from ‘relationships(p)’. This will be a list. For each relationship ‘x’ in the list, you can get its properties from ‘properties(x)’.

Chiara
Node Link

Thank you, I am getting indeed a list of relationships when adding

'relationships(path) as rel' however I was imagining to get a list of the connector verbs, but instead i am getting a list that looks like this: '[(cost), (cost)]'
To the second part, could you elaborate on how get the properties. I am from a python background so I would tackle this with a for loop, is this possible with Cypher as well?

A relationship object has a startNode, an endNide, properties, a type, and an id. All of these are accessible via methods.

The easiest method to extract attributes from a list into a new list is with list comprehension. It allows you to interate through all list elements, apply an optional filter predicate, and apply an optional transformation. The result is a new list. Your current query is using list comprehension to extract the nodeNames. The following will give you a list of the properties from each relationship in ‘path’. Each relationship’s properties is a map, so you will get a list of maps.

[x in relationships(path) | properties(x)]

https://neo4j.com/docs/cypher-manual/current/syntax/lists/#cypher-list-comprehension