Traverse command: how to use $current context variable

124 views
Skip to first unread message

Riccardo Tasso

unread,
Sep 18, 2015, 3:20:05 AM9/18/15
to OrientDB
Hi, I am trying to understand the potential of TRAVERSE command ( http://orientdb.com/docs/2.1/SQL-Traverse.html ).

The documented examples, which all use the $depth context variable are clear.

I can't figure out how to use other variables, first of all $current.
Consider this toy-dataset:

create database memory:tmp admin admin
insert into V set name = 'root'
insert into V set name = '1'   
insert into V set name = '2'
insert into V set name = '1.1'
insert into V set name = '1.2'
insert into V set name = '2.1'
insert into V set name = '2.2'
insert into V set name = '2.3'
create edge from #9:0 to #9:1
create edge from #9:0 to #9:2
create edge from #9:1 to #9:3
create edge from #9:1 to #9:4

I would expect to find something with those query, but no result is returned:
TRAVERSE out() FROM (SELECT FROM V WHERE name = 'root') WHILE name = '2.3'
TRAVERSE
out() FROM (SELECT FROM V WHERE name = 'root') WHILE $current.name = '2.3'

Is there any possibility to stop the TRAVERSE with some condition on the current vertex?

Thanks,
   Riccardo


Luigi Dell'Aquila

unread,
Sep 18, 2015, 3:24:22 AM9/18/15
to orient-...@googlegroups.com
Hi Riccardo,

your root node name is 'root', so it does not match your traversal condition ;-)
Try with this:

TRAVERSE out() FROM (SELECT FROM V WHERE name = 'root') WHILE name = '2.3' or $depth = 0

I hope I understood correctly your problem... the result will be the following:
suppose you have this chain:

A:'root' -> B:'2.3' -> C:'2.3' -> D:'something else'

you will obtain
A
B
C

Luigi




--

---
You received this message because you are subscribed to the Google Groups "OrientDB" group.
To unsubscribe from this group and stop receiving emails from it, send an email to orient-databa...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Riccardo Tasso

unread,
Sep 18, 2015, 4:05:23 AM9/18/15
to orient-...@googlegroups.com
Yeah, you right: now I see the point!

Cheers,
   Riccardo

François Charette

unread,
Sep 18, 2015, 5:08:11 AM9/18/15
to OrientDB
Riccardo,

Maybe you had in mind something like this:

TRAVERSE out() FROM #9:0 UNTIL name ='2.3'

where the hypothetical command UNTIL would mean "continue the traversal until some condition is met, and then stop" (including the vertices that match the UNTIL condition) ?

For some use cases this would greatly simplify traversal operations I think, especially when the depth of the end-points cannot be known in advance.

Cheers
François

Luigi Dell'Aquila

unread,
Sep 18, 2015, 5:41:11 AM9/18/15
to orient-...@googlegroups.com
Hi guys, 

UNTIL can be expressed as a negative WHILE, in this case 

TRAVERSE out() FROM #9:0 UNTIL name ='2.3'

is the same as
 
TRAVERSE out() FROM #9:0 WHILE name <> '2.3'

so there is no strict reason to have this.
I also suggest you to take a look at the roadmap for 2.2, we will support a new query paradigm, that is pattern matching. Here you can find some info https://github.com/orientechnologies/orientdb-docs/blob/master/SQL-Match.md
With MATCH statement you will have much finer control on traversal, using WHERE and WHILE together

Luigi


Reply all
Reply to author
Forward
0 new messages