3
votes

Given the following graph I want to get all the projects that are related with some children node with name 'u0.p1.root.1'.

enter image description here

To solve the previous request I can run a query like this one:

select from (
    traverse out('rootSphere', 'children') 
    from (select from V where label = 'project') maxdepth 2
)
where name = 'u0.p0.root.1'

which gives me 'u0.p1'.

The problem is that I also want to get all the vertices and edges that conform the path between 'u0.p1' and 'u0.p1.root.1'

If I slightly modify the query to get the $path

select $path from (
    traverse out('rootSphere', 'children') 
    from (select from V where label = 'project') maxdepth 2
)
where name = 'u0.p0.root.1'

then I get

(#9:1030).out[0](#9:1031).out[1](#9:1033)

but I don't know how to deal with it :-(

Is it possible to get the set of vertices and edges that satisfy the given path? is there maybe a better query to solve this problem?

Thanks in advance.

2

2 Answers

4
votes

It looks like you want to work backward from u0.p1.root.1, so the following query may be close to what you want:

traverse inE('rootSphere', 'children'), outV() from (select from Circle where name='u0.p1.root.1');

(I have given the name "Circle" to the class of all circles in your diagram.)

When run on the graph as shown in the diagram, the above query produces:

----+-----+----------+------------+-----------+-----+-----+-------------+--------------+-----------+--------------
#   |@RID |@CLASS    |name        |in_children|out  |in   |in_rootSphere|out_children  |in_projects|out_rootSphere
----+-----+----------+------------+-----------+-----+-----+-------------+--------------+-----------+--------------
0   |#11:3|Circle    |u0.p1.root.1|[#15:3]    |null |null |null         |null          |null       |null          
1   |#15:3|children  |null        |null       |#11:2|#11:3|null         |null          |null       |null          
2   |#11:2|Circle    |u0.p1.root  |null       |null |null |[#14:1]      |[#15:2, #15:3]|null       |null          
3   |#14:1|rootSphere|null        |null       |#11:1|#11:2|null         |null          |null       |null          
4   |#11:1|Circle    |u0.p1       |null       |null |null |null         |null          |[size=1]   |[#14:1]       
----+-----+----------+------------+-----------+-----+-----+-------------+--------------+-----------+--------------

In any case, one of the problems you ran into arises from the fact that out() restricts the output to vertices, whereas you want nodes and edges.

Another problem you encountered is your use of MAXDEPTH. The OrientDB documentation is not so clear about MAXDEPTH, so consider this query and the results:

traverse outE('rootSphere', 'children'), inV() from (select from V where name='u0.p1') maxdepth 3;

----+-----+----------+------------+-----------+--------------+-----+-----+-------------+--------------+-----------
#   |@RID |@CLASS    |name        |in_projects|out_rootSphere|out  |in   |in_rootSphere|out_children  |in_children
----+-----+----------+------------+-----------+--------------+-----+-----+-------------+--------------+-----------
0   |#11:1|Circle    |u0.p1       |[size=1]   |[#14:1]       |null |null |null         |null          |null       
1   |#14:1|rootSphere|null        |null       |null          |#11:1|#11:2|null         |null          |null       
2   |#11:2|Circle    |u0.p1.root  |null       |null          |null |null |[#14:1]      |[#15:2, #15:3]|null       
3   |#15:2|children  |null        |null       |null          |#11:2|#11:4|null         |null          |null       
4   |#11:4|Circle    |u0.p1.root.0|null       |null          |null |null |null         |null          |[#15:2]    
5   |#15:3|children  |null        |null       |null          |#11:2|#11:3|null         |null          |null       
6   |#11:3|Circle    |u0.p1.root.1|null       |null          |null |null |null         |null          |[#15:3]    
----+-----+----------+------------+-----------+--------------+-----+-----+-------------+--------------+-----------

MAXDEPTH must be set to 3 here to reach the children.

2
votes

In response to the question about accessing the elements of $path, here is another solution to the original problem that uses an approach that is similar to your query using "$path", but it uses traversedElement instead of $path:

select expand( traversedElement )
from (select traversedElement(0,100)
      from ( traverse outE('rootSphere', 'children'), inV()
             from Circle)
      where name = 'u0.p1.root.1' ) 

----+-----+----------+------------+-----------+--------------+-----+-----+-------------+--------------+-----------
#   |@RID |@CLASS    |name        |in_projects|out_rootSphere|out  |in   |in_rootSphere|out_children  |in_children
----+-----+----------+------------+-----------+--------------+-----+-----+-------------+--------------+-----------
0   |#11:1|Circle    |u0.p1       |[size=1]   |[#14:1]       |null |null |null         |null          |null       
1   |#14:1|rootSphere|null        |null       |null          |#11:1|#11:2|null         |null          |null       
2   |#11:2|Circle    |u0.p1.root  |null       |null          |null |null |[size=1]     |[#15:2, #15:3]|null       
3   |#15:3|children  |null        |null       |null          |#11:2|#11:3|null         |null          |null       
4   |#11:3|Circle    |u0.p1.root.1|null       |null          |null |null |null         |null          |[size=1]   
----+-----+----------+------------+-----------+--------------+-----+-----+-------------+--------------+-----------

5 item(s) found. Query executed in 0.051 sec(s).