0
votes

I want to select all vertices that are connected to another vertex. I am currently using the traverse function in OrientDB. Consider the following example:

> create class professor extends V
> create class course extends V
> insert into professor set name='Smith'
Inserted record 'professor#14:0{name:Smith} v1'

> insert into course set name='Calculus'
Inserted record 'course#15:0{name:Calculus} v1'

> create class teaches extends E
> create edge teaches from #14:0 to #15:0
Created edge '[teaches#16:0{out:#14:0,in:#15:0} v3]'

Now when I try to traverse to find the course(s) that professor Smith teaches I use the following command:

> traverse out_teaches from #15:0
----+-----+---------+-----+-----------+-----+-----
#   |@RID |@CLASS   |name |out_teaches|out  |in   
----+-----+---------+-----+-----------+-----+-----
0   |#14:0|professor|Smith|[size=1]   |null |null 
1   |#16:0|teaches  |null |null       |#14:0|#15:0
----+-----+---------+-----+-----------+-----+-----

Why does this return to me the edge and not the vertex (course) that I am looking for? What is the appropriate command to return to me the vertex? I want the record for 'Calculus' to be returned.

3

3 Answers

2
votes

I expanded your graph a bit to try your query.

enter image description here

If you want to know only the connected vertices to some starting vertex by the edge 'teaches' you should use SELECT EXPAND (OUT / IN / BOTH) because TRAVERSE is more useful if you wish to explore the graph at different depths (in my case "Smith" has the @rid #11:0):

select expand(out('teaches')) from (select from Professor where name='Smith')

----+-----+------+------------+----------+----------
#   |@RID |@CLASS|name        |in_teaches|in_follows
----+-----+------+------------+----------+----------
0   |#12:0|course|Calculus    |[size=1]  |[size=1]
1   |#12:1|course|Astrophysics|[size=1]  |[size=1]
2   |#12:2|course|Law         |[size=2]  |[size=1]
----+-----+------+------------+----------+----------

or with select expand(out('teaches')) from #11:0 you will obtain the same result:

----+-----+------+------------+----------+----------
#   |@RID |@CLASS|name        |in_teaches|in_follows
----+-----+------+------------+----------+----------
0   |#12:0|course|Calculus    |[size=1]  |[size=1]
1   |#12:1|course|Astrophysics|[size=1]  |[size=1]
2   |#12:2|course|Law         |[size=2]  |[size=1]
----+-----+------+------------+----------+----------

or you can obtain all the connected vertices to the professor "Smith"

select expand(out()) from professor where name="Smith"

----+-----+----------+------------+----------+----------+------------+----------
#   |@RID |@CLASS    |name        |in_teaches|in_follows|in_studiesAt|in_worksAt
----+-----+----------+------------+----------+----------+------------+----------
0   |#12:0|course    |Calculus    |[size=1]  |[size=1]  |null        |null
1   |#12:1|course    |Astrophysics|[size=1]  |[size=1]  |null        |null
2   |#12:2|course    |Law         |[size=2]  |[size=1]  |null        |null
3   |#16:0|university|Cambridge   |null      |null      |[size=1]    |[size=1]
----+-----+----------+------------+----------+----------+------------+----------

Your query traverse out_teaches from #11:0 seems to list the starting vertex and all of the connected edges with relative IN and OUT vertices:

----+-----+---------+-----+-----------+-----------+-----+-----
#   |@RID |@CLASS   |name |out_teaches|out_worksAt|out  |in
----+-----+---------+-----+-----------+-----------+-----+-----
0   |#11:0|professor|Smith|[size=3]   |[size=1]   |null |null
1   |#13:0|teaches  |null |null       |null       |#11:0|#12:0
2   |#13:1|teaches  |null |null       |null       |#11:0|#12:1
3   |#13:2|teaches  |null |null       |null       |#11:0|#12:2
----+-----+---------+-----+-----------+-----------+-----+-----

I tried also traverse out_teaches from professor and the result is similar to the previous query:

----+-----+---------+-----+-----------+-----------+-----+-----
#   |@RID |@CLASS   |name |out_teaches|out_worksAt|out  |in
----+-----+---------+-----+-----------+-----------+-----+-----
0   |#11:0|professor|Smith|[size=3]   |[size=1]   |null |null
1   |#13:0|teaches  |null |null       |null       |#11:0|#12:0
2   |#13:1|teaches  |null |null       |null       |#11:0|#12:1
3   |#13:2|teaches  |null |null       |null       |#11:0|#12:2
4   |#11:1|professor|Green|[size=1]   |[size=1]   |null |null
5   |#13:3|teaches  |null |null       |null       |#11:1|#12:2
----+-----+---------+-----+-----------+-----------+-----+-----
1
votes

The correct syntax for selecting the courses (at least in OrientDB 2.1) would be based on out('teaches'). For example:

> select expand(out('teaches')) from (select from Professor where name='Smith')

----+-----+------+--------+----------
#   |@RID |@CLASS|name    |in_teaches
----+-----+------+--------+----------
0   |#12:0|Course|Calculus|[size=1]  
----+-----+------+--------+----------

That is, there's just one vertex, as expected.

Please note that 'traverse' is used for a different purpose. It involves an iterative procedure for traversing graphs.

out_teaches

"out_teaches" is a reference to an edge. Using OrientDB 2.1.7, the response I obtained for your "out_teaches" query is as follows:

> select expand(out_teaches) from (select from Professor where name='Smith')

----+-----+-------+-----+-----
#   |@RID |@CLASS |out  |in   
----+-----+-------+-----+-----
0   |#13:0|teaches|#11:0|#12:0
----+-----+-------+-----+-----

Again, this is what one would expect - an edge.

0
votes

Your query is working fine for me.

In my case I have the rid as #11:0 for professor, #12:0 for course and #13:0 for teaches

Just rerun your query once again or try the below :

traverse both('teaches') from #12:0