Search code examples
graphneo4jcyphergraph-databases

Get path lengths for every relationship neo4j


So I have a graph that looks like this(starting from the rightmost side) with relationships that have a unique number attribute called Isnad. I want to write a query to get the length of every Isnad from the start node to the end node but I can't figure it out. I don't know how to traverse every path for every Isnad separately. Any help?

Graph


Solution

  • I don't know if it is the most elegant and solution, but I think it works. First, I'm getting all unique Isnad values of relationships outgoing from the rightmost side node using an identifier. Then I'm using a variable-length pattern matching where all relationships have the same value for Isnad property. Then the Isnad value and the path length are returned.

    match ({id:'unique-identifier-of-rightmost-side-node'})-[r]->()
    with distinct r.Isnad as Isnad
    match p = ()-[*{Isnad : Isnad}]->()
    return Isnad, length(p) as Length