Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
04-28-2022 02:15 PM
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 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!
Solved! Go to Solution.
04-28-2022 02:27 PM
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
04-28-2022 02:27 PM
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
04-28-2022 02:29 PM
That is exactly what I was looking for! Thank you so much! 🙂
All the sessions of the conference are now available online