【问题标题】:OrientDB Traverse FunctionalityOrientDB 遍历功能
【发布时间】:2015-12-21 02:19:06
【问题描述】:

我想选择所有连接到另一个顶点的顶点。我目前在 OrientDB 中使用traverse 函数。考虑以下示例:

> 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]'

现在,当我尝试遍历以查找 Smith 教授教授的课程时,我使用以下命令:

> 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
----+-----+---------+-----+-----------+-----+-----

为什么这会将边缘而不是我正在寻找的顶点(路线)返回给我?将顶点返回给我的适当命令是什么?我希望返回“微积分”的记录。

【问题讨论】:

    标签: orientdb


    【解决方案1】:

    我稍微扩展了您的图表以尝试您的查询。

    如果您只想知道边“教导”与某个起始顶点的连接顶点,您应该使用SELECT EXPAND (OUT / IN / BOTH),因为如果您希望探索不同深度的图形,TRAVERSE 更有用(在我的情况下“史密斯”有@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]
    ----+-----+------+------------+----------+----------
    

    或使用select expand(out('teaches')) from #11:0,您将获得相同的结果:

    ----+-----+------+------------+----------+----------
    #   |@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]
    ----+-----+------+------------+----------+----------
    

    或者你可以获得所有连接到教授“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]
    ----+-----+----------+------------+----------+----------+------------+----------
    

    您的查询traverse out_teaches from #11:0 似乎列出了起始顶点和所有具有相对INOUT 顶点的连接边:

    ----+-----+---------+-----+-----------+-----------+-----+-----
    #   |@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
    ----+-----+---------+-----+-----------+-----------+-----+-----
    

    我也试过traverse out_teaches from professor,结果和上一个查询类似:

    ----+-----+---------+-----+-----------+-----------+-----+-----
    #   |@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
    ----+-----+---------+-----+-----------+-----------+-----+-----
    

    【讨论】:

      【解决方案2】:

      选择课程的正确语法(至少在 OrientDB 2.1 中)将基于 out('teaches')。例如:

      > select expand(out('teaches')) from (select from Professor where name='Smith')
      
      ----+-----+------+--------+----------
      #   |@RID |@CLASS|name    |in_teaches
      ----+-----+------+--------+----------
      0   |#12:0|Course|Calculus|[size=1]  
      ----+-----+------+--------+----------
      

      也就是说,正如预期的那样,只有一个顶点。

      请注意,“traverse”用于不同的目的。它涉及遍历图的迭代过程。

      out_teaches

      “out_teaches”是对边的引用。使用 OrientDB 2.1.7,我为您的“out_teaches”查询获得的响应如下:

      > select expand(out_teaches) from (select from Professor where name='Smith')
      
      ----+-----+-------+-----+-----
      #   |@RID |@CLASS |out  |in   
      ----+-----+-------+-----+-----
      0   |#13:0|teaches|#11:0|#12:0
      ----+-----+-------+-----+-----
      

      再一次,这是人们所期望的 - 优势。

      【讨论】:

        【解决方案3】:

        您的查询对我来说很好。

        在我的情况下,我的教授是 #11:0,课程是 #12:0,教师是 #13:0

        再次重新运行您的查询或尝试以下操作:

        从 #12:0 开始遍历两个('teaches')

        【讨论】:

        • traverse both('teaches') from #15:0 确实把课程和教授还给了我。我重新运行了之前的查询,但它仍然产生相同的结果。我想知道这个问题的答案。谢谢
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-05-04
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多