6

I am new to OrientDB and I want to use the new shortestPath() method to get the edges that are between two vertices.

What I do is:

OSQLSynchQuery<T> sql = new OSQLSynchQuery<T>("select shortestpath(" + firstVertex + ", " + secondVertex + ").asString()");

List<ODocument> execute = db.query(sql);

and what I can only get is [#-2:1{shortestpath:[#8:1, #8:3]} v0].

So, I wanted to know how could I extract the edges (well, only one edge in this case, because these two vertices are directly connected) from this output or from the output that I get without asString():

[#-2:1{shortestpath:[2]} v0]
TylerH
  • 20,799
  • 66
  • 75
  • 101
Lucia Pasarin
  • 2,268
  • 1
  • 21
  • 37

3 Answers3

1

OrientDB has collection and map types. To make a collection the result set (what you're interested) you've to flatten it:

select flatten( shortestpath(" + firstVertex + ", " + secondVertex + ") )

To get the edges outgoing edges there are so many ways. Below a few of them:

select vertices.out from (
   select flatten( shortestpath(" + firstVertex + ", " + secondVertex + ") ) as vertices
)

Or also:

select flatten( shortestpath(" + firstVertex + ", " + secondVertex + ").out )
Lvca
  • 8,938
  • 2
  • 24
  • 25
  • I still cannot get the edge/s. I get now `[#-2:0 v0]` – Lucia Pasarin Apr 25 '13 at 21:50
  • Could it be because of using `db.query(sql)`? With it I can only get an ArrayList and not a Map. If it is because of this, what else could I use to execute an sql query in Java? – Lucia Pasarin Apr 25 '13 at 22:07
  • You've to cross the edges. There are several ways. I change my answer to support it – Lvca Apr 26 '13 at 12:20
  • Unfortunately, none of them worked for me. Using the first way I got `[#-2:1 v0]` and with the second I got `[#-2:0 v0]`. Maybe I should use `doc.field("some_field");` for each `ODocument doc` in `List execute` to be able to see the results. If it is the case, which is the "some_field" I should use? – Lucia Pasarin Apr 26 '13 at 18:13
  • What release are you using? I suggest you to use 1.4.0-SNAPSHOT (will be final in few days) – Lvca Apr 29 '13 at 21:39
  • I am using 1.3.0. Now I changed to 1.4.0-SNAPSHOT but it is still not working. I get the same as with 1.3.0. – Lucia Pasarin Apr 29 '13 at 22:12
  • Have you used latest snapshot: https://oss.sonatype.org/content/repositories/snapshots/com/orientechnologies/orientdb/1.4.0-SNAPSHOT/ – Lvca May 01 '13 at 11:22
  • Yes. I have just downloaded it just to test if Maven was not finding the right one, but I am still getting the same result. – Lucia Pasarin May 01 '13 at 12:52
  • Hey Lucia, you were right. With new BP we don't have anymore out and in but out_* and in_*. These functions must support the new shape. Hope to fix it in few hours. – Lvca May 01 '13 at 13:27
  • Ok. Thank you for the fast reply. – Lucia Pasarin May 01 '13 at 13:46
  • Done. Can you retry with latest snapshot please? – Lvca May 02 '13 at 11:23
  • I have tried now with `orientdb-1.4.0-20130502.170215-187` and I got `com.orientechnologies.orient.core.sql.OCommandSQLParsingException: Error on parsing command at position #0: No function for name SHORTESTPATH, available names are : (...)`. – Lucia Pasarin May 02 '13 at 17:52
  • You've to use the GraphEdition because right now all the graph algo are in the graph-db module. Do: "git clone https://github.com/nuvolabase/blueprints.git", then "cd blueprints", then "mvn clean install". You will have a fresh BP in your system. I suggest you to import the projects "blueprints-orient-graph" in your IDE to work with the snapshot. However in few days will be final. – Lvca May 02 '13 at 17:58
  • I am not using git for my project and I use Eclipse's m2e. I tried changing my `blueprints-orient-graph` to version `2.4.0-SNAPSHOT` (I was using 2.3.0 before) in pom.xml and also downloading blueprints-orient-graph-2.4.0-20130428.201000-9.jar from https://oss.sonatype.org/content/repositories/snapshots/com/tinkerpop/blueprints/blueprints-orient-graph/2.4.0-SNAPSHOT/. I even tried changing `com.tinkerpop.gremlin` `gremlin-java` to version `2.4.0-SNAPSHOT`. Unfortunately, none of them worked. – Lucia Pasarin May 02 '13 at 18:38
  • You could get last snapshot GraphEd 1.4.0 from: http://www.orientdb.org/download.htm – Lvca May 02 '13 at 18:58
  • This one https://s3.amazonaws.com/orientdb/releases/orientdb-graphed-1.4.0-SNAPSHOT.tar.gz didn't work. – Lucia Pasarin May 02 '13 at 19:03
  • I retried and this time I could download it, add all libs in Eclipse and now it doesn't fail but I am still getting the same results that I put above (`[#-2:1 v0]` and `[#-2:0 v0]`). – Lucia Pasarin May 03 '13 at 20:04
  • Can you send us the db as json or the set of commands to recreate it? – Lvca May 06 '13 at 17:46
  • Lucia, your graphdb has no edges. That's the reason. – Lvca May 09 '13 at 13:16
1

For me, it didn't work how dante or Lvca explained it:

select flatten(shortestPath) from (select shortestPath(#12:0,#12:2,'BOTH')

This lead to errors in the console or to no records returned in the OrientDB Studio.

Instead, when I used an alias for the function result, it worked finally. So maybe try out this form:

select flatten(sp) from (select shortestPath(#12:0,#15:2,'BOTH') as sp)

(Note. I'm using v1.7.4)

dajood
  • 3,758
  • 9
  • 46
  • 68
0

Try

select expand(shortestPath) from (select shortestPath(" + firstVertex + ", " + secondVertex + "))

You will receive vertices.

dante
  • 809
  • 5
  • 3