Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get Path Length from Cypher Query

So, Say that I've got a list of nodes like so:

A -> B -> C -> D -> ...

I want to add node F onto the beginning of this list. To complicate this pattern, I can be given a reference to any node on this list as the "starting point," from which I will need to derive the starting point. For example, I could be given a reference to node "C", and need to derive an algorithm that will return a reference to A.

I figure this should be able to be done with a query such as

    START n = node(*), a = node(*)
    MATCH a -[:LINKED*]> n
    WHERE n.id! = <ID>
    RETURN a

If I could sort the relationships by length, I could simply take the longest path as the first node in the relationship, and go along my merry way. Trouble is, I can't figure out how to order the results by path length. I figure that it must be possible, I'm just missing a small query command. Any takers?

-pYr0

like image 899
pYr0 Avatar asked Oct 15 '25 04:10

pYr0


1 Answers

Length is function: http://docs.neo4j.org/chunked/stable/query-functions-scalar.html#functions-length

START n = node(*), a = node(*)
MATCH p=a -[:LINKED*]-> n
WHERE n.id! = <ID>
RETURN a
ORDER BY length(p) desc
like image 82
Luanne Avatar answered Oct 17 '25 17:10

Luanne



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!