跳转至

GET SUBGRAPH

GET SUBGRAPH语句检索指定Edge type的起始点可以到达的点和边的信息,返回子图信息。

语法

GET SUBGRAPH [WITH PROP] [<step_count> STEPS] FROM {<vid>, <vid>...}
[IN <edge_type>, <edge_type>...]
[OUT <edge_type>, <edge_type>...]
[BOTH <edge_type>, <edge_type>...];
  • WITH PROP:展示属性。不添加本参数则隐藏属性。
  • step_count:指定从起始点开始的跳数,返回从0到step_count跳的子图。必须是非负整数。默认值为1。
  • vid:指定起始点ID。
  • edge_type:指定Edge type。可以用INOUTBOTH来指定起始点上该Edge type的方向。默认为BOTH

Note

GET SUBGRAPH语句检索的路径类型为trail,即检索的路径只有点可以重复,边不可以重复。详情请参见路径

示例

以下面的示例图进行演示。

A sample graph for GET SUBGRAPH

  • 查询从点player100开始、0~1跳、所有Edge type的子图。
    nebula> GET SUBGRAPH 1 STEPS FROM "player100";
    +-------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------+
    | _vertices                                                               | _edges                                                                                                                      |
    +-------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------+
    | [("player100" :player{})]                                               | [[:serve "player100"->"team200" @0 {}], [:follow "player100"->"player101" @0 {}], [:follow "player100"->"player102" @0 {}]] |
    +-------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------+
    | [("team200" :team{}), ("player101" :player{}), ("player102" :player{})] | [[:follow "player102"->"player101" @0 {}]]                                                                                  |
    +-------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------+
    

    返回的子图如下。

    GET SUBGRAPH FROM "player100"

  • 查询从点player100开始、0~1跳、follow类型的入边的子图。
    nebula> GET SUBGRAPH 1 STEPS FROM "player100" IN follow;
    +---------------------------+--------+
    | _vertices                 | _edges |
    +---------------------------+--------+
    | [("player100" :player{})] | []     |     
    +---------------------------+--------+
    | []                        | []     |
    +---------------------------+--------+
    

    因为player100没有follow类型的入边。所以仅返回点player100

  • 查询从点player100开始、0~1跳、serve类型的出边的子图,同时展示边的属性。
    nebula> GET SUBGRAPH WITH PROP 1 STEPS FROM "player100" OUT serve;
    +------------------------------------------------------+-------------------------------------------------------------------------+
    | _vertices                                            | _edges                                                                  |
    +------------------------------------------------------+-------------------------------------------------------------------------+
    | [("player100" :player{age: 42, name: "Tim Duncan"})] | [[:serve "player100"->"team200" @0 {end_year: 2016, start_year: 1997}]] |
    +------------------------------------------------------+-------------------------------------------------------------------------+
    | [("team200" :team{name: "Warriors"})]                | []                                                                      |
    +------------------------------------------------------+-------------------------------------------------------------------------+
    

    返回的子图如下。

    GET SUBGRAPH FROM "100" OUT serve

FAQ

为什么返回结果中会出现超出step_count跳数之外的关系?

为了展示子图的完整性,会在满足条件的所有点上额外查询一跳。例如下图。

FAQ

  • GET SUBGRAPH 1 STEPS FROM "A";查询的满足结果的路径是A->BB->AA->C,为了子图的完整性,会在满足结果的点上额外查询一跳,即B->C
  • GET SUBGRAPH 1 STEPS FROM "A" IN follow;查询的满足结果的路径是B->A,在满足结果的点上额外查询一跳,即A->B

如果只是查询满足条件的路径或点,建议使用MATCHGO语句。例如:

nebula> match p= (v:player) -- (v2) where id(v)=="A" return p;

nebula> go 1 steps from "A" over follow;

为什么返回结果中会出现低于step_count跳数的关系?

查询到没有多余子图数据时会停止查询,且不会返回空值。

nebula> GET SUBGRAPH 100 STEPS FROM "player141" OUT follow;
+-------------------------------------------------------+--------------------------------------------+
| _vertices                                             | _edges                                     |
+-------------------------------------------------------+--------------------------------------------+
| [("player141" :player{age: 43, name: "Ray Allen"})]   | [[:follow "player141"->"player124" @0 {}]] |
+-------------------------------------------------------+--------------------------------------------+
| [("player124" :player{age: 33, name: "Rajon Rondo"})] | [[:follow "player124"->"player141" @0 {}]] |
+-------------------------------------------------------+--------------------------------------------+

最后更新: August 23, 2021
Back to top