Returning properties from nodes and relationship in a path

Hello! I'm very new to Neo4j, and this might be a very simple question.
However: I'm currently working on a project where I need a query that returns properties from both nodes and relationships in a path.

What I currently have:

MATCH (p1:Node { UniversalId: '00000000-0c00-090e-0001-060f0e030f0b' }) 
MATCH (p2:Node { UniversalId: '00000000-0c00-090e-0001-0700000a0c06' }) 
CALL apoc.algo.dijkstra(p1, p2, 'AVSTAND>', 'cost_algo') YIELD path, weight 
return path

Which yields me the following graph: (6 nodes, 5 relationships)


I would like a way to return properties from both of the nodes between a relationship, as well as from the relationship.
Example:
╒════════════════╤════════════════╤═════════╤══════════╕
│"a.name".................................│"b.name" .............................. │"rel.dir".............│"rel.cost"..............│
╞════════════════╪════════════════╪═════════╪══════════╡
│"name1".................................│ "bname1"............................. │"right" ...............│"1" ........................│
├────────────────┼────────────────┼─────────┼──────────┤
│"name2" ................................│"bname2"...............................│"left"..................│ "10".....................│
├────────────────┼────────────────┼─────────┼──────────┤
│"name3" ............................... │"bname3"...............................│"right"...............│"100".....................│
├────────────────┼────────────────┼─────────┼──────────┤
│"name4" ............................... │"bname4"...............................│"left"..................│"100".....................│
├────────────────┼────────────────┼─────────┼──────────┤
│"name5" ............................... │"bname5"...............................│"left"..................│"100".....................│
├────────────────┼────────────────┼─────────┼──────────┤

I have tried the following which almost gives me what I want, but it also returns

MATCH (p1:Node { UniversalId: '00000000-0c00-090e-0001-060f0e030f0b' }) 
MATCH (p2:Node { UniversalId: '00000000-0c00-090e-0001-0700000a0c06' }) 
CALL apoc.algo.dijkstra(p1, p2, 'AVSTAND>', 'cost_algo') YIELD path, weight 
unwind  relationships(path) as rel
match (a)-[rel]-(b)
return a.name,b.name,rel.dir, rel.cost

This however yields me some extra rows which are other relationships between the nodes a and b which isn't in the path given by the dijkstra algorithm.
Res: (Got 10 rows back, expected 5 because there is 5 relationships in the path)
╒════════════════╤════════════════╤═════════╤══════════╕
│"a.name".................................│"b.name"................................│"rel.dir"..............│"rel.cost"..............│
╞════════════════╪════════════════╪═════════╪══════════╡
│"Hemberget Test"................│"Astrakan Test" ..................│"right" ..............│"1" .........................│
├────────────────┼────────────────┼─────────┼──────────┤
│"Astrakan Test" ...................│"Hemberget Test"...............│"right" ..............│"1"..........................│
├────────────────┼────────────────┼─────────┼──────────┤
│"Astrakan Test"....................│"Astrakan Test"....................│"right" ..............│"1"..........................│
├────────────────┼────────────────┼─────────┼──────────┤
│"Astrakan Test"....................│"Astrakan Test"....................│"right" ..............│"1"..........................│
├────────────────┼────────────────┼─────────┼──────────┤
│"Astrakan Test"....................│"Astrakan Test"....................│"right" ..............│"1.1"......................│
├────────────────┼────────────────┼─────────┼──────────┤
│"Astrakan Test"....................│"Astrakan Test"....................│"right" ..............│"1.1"......................│
├────────────────┼────────────────┼─────────┼──────────┤
│"Astrakan Test"....................│"Astrakan Test"....................│"right" ..............│"1"..........................│
├────────────────┼────────────────┼─────────┼──────────┤
│"Astrakan Test"....................│"Astrakan Test"....................│"right" ..............│"1"..........................│
├────────────────┼────────────────┼─────────┼──────────┤
│"Astrakan Test"....................│"Astrakan Test"....................│"right" ..............│"1"..........................│
├────────────────┼────────────────┼─────────┼──────────┤
│"Astrakan Test"....................│"Astrakan Test"....................│"right" ..............│"1"..........................│
└────────────────┴────────────────┴─────────┴──────────┘
I hope I made myself understandable, thank you in advanced! :slight_smile:

Relationships allow you to access the start and end nodes, so you don't need to match to get them. Try this:

MATCH (p1:Node { UniversalId: '00000000-0c00-090e-0001-060f0e030f0b' }) 
MATCH (p2:Node { UniversalId: '00000000-0c00-090e-0001-0700000a0c06' }) 
CALL apoc.algo.dijkstra(p1, p2, 'AVSTAND>', 'cost_algo') YIELD path, weight 
unwind  relationships(path) as rel
with startNode(rel) as a, endNode(rel) as b, rel
return a.name,b.name,rel.dir, rel.cost
1 Like

That is exactly what I was looking for! Thank you so much! :)