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.

Next-to-last node in longest path

dpetit1
Node Link

I have graph that contains source code control commits along with their parents and authors.
The schema is below:

I am trying to write a cypher query to find the earliest pull request in a chain of nodes where a given starting commit is the merge result (:MERGED_BY) for a particular pullrequest. The source commit (:SOURCE) of that pull request may itself be a commit that is the merge result of another pullrequest, and so on. The chain should end with the pull request whose source commit is not the merge result of another pull request.

I was trying this out by getting the path from the starting node to the tail of the path, connecting node by either :MERGED_BY or :SOURCE, but this ultimately yields a longest path whose last node is the commit, not the pull request that I need.

MATCH p=(c:Commit {uri:"xxxx"})-[:MERGED_BY|SOURCE*1..8]-(tail)
return p ORDER BY length(p) desc limit 1

Any help is appreciated.

1 REPLY 1

You can split your path and add an extra bit at the end

p=(c:Commit {uri:"xxxx"})-[:MERGED_BY|SOURCE*1..7]-(preTail),
  (preTail)-[:MERGED_BY|SOURCE]-(tail)