Search code examples
javaorientdb

Find shorterst path length in OrientDB


How can I find the shortest path length in OrientDB? There are a lot of examples how to find the shortest path, but there must be a way to get only the length of this path?

So instead of a list of vertices like with this query

SELECT expand(path) FROM (

  SELECT shortestPath($from, $to) AS path 

  LET 

    $from = (SELECT FROM Profiles WHERE Name='Santo' and Surname='OrientDB'), 

    $to = (SELECT FROM Countries WHERE Name='United States') 

  UNWIND path

)

I would just like a single number as result.

I am using Java to query the DB, so a Java API function is also OK.


Solution

  • Try this:

    SELECT sum(path.size()) FROM (
    
      SELECT shortestPath($from, $to) AS path 
    
      LET 
    
        $from = (SELECT FROM Profiles WHERE Name='Santo' and Surname='OrientDB'), 
    
        $to = (SELECT FROM Countries WHERE Name='United States') 
    
      UNWIND path
    
    )
    

    Hope it helps.

    Regards