GET SUBGRAPH

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

语法

  1. GET SUBGRAPH [WITH PROP] [<step_count> STEPS] FROM {<vid>, <vid>...}
  2. [IN <edge_type>, <edge_type>...]
  3. [OUT <edge_type>, <edge_type>...]
  4. [BOTH <edge_type>, <edge_type>...];
  • WITH PROP:展示属性。不添加本参数则隐藏属性。

  • step_count:指定从起始点开始的跳数,返回从0到step_count跳的子图。必须是非负整数。默认值为1。

  • vid:指定起始点ID。

  • edge_type:指定Edge type。可以用INOUTBOTH来指定起始点上该Edge type的方向。默认为BOTH

示例

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

A sample graph for GET SUBGRAPH

  • 查询从点player100开始、0~1跳、所有Edge type的子图。

    1. nebula> GET SUBGRAPH 1 STEPS FROM "player100";
    2. +-------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------+
    3. | _vertices | _edges |
    4. +-------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------+
    5. | [("player100" :player{})] | [[:serve "player100"->"team200" @0 {}], [:follow "player100"->"player101" @0 {}], [:follow "player100"->"player102" @0 {}]] |
    6. +-------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------+
    7. | [("team200" :team{}), ("player101" :player{}), ("player102" :player{})] | [[:follow "player102"->"player101" @0 {}]] |
    8. +-------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------+

    返回的子图如下。

    GET SUBGRAPH FROM "player100"

  • 查询从点player100开始、0~1跳、follow类型的入边的子图。

    1. nebula> GET SUBGRAPH 1 STEPS FROM "player100" IN follow;
    2. +---------------------------+--------+
    3. | _vertices | _edges |
    4. +---------------------------+--------+
    5. | [("player100" :player{})] | [] |
    6. +---------------------------+--------+
    7. | [] | [] |
    8. +---------------------------+--------+

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

  • 查询从点player100开始、0~1跳、serve类型的出边的子图,同时展示边的属性。

    1. nebula> GET SUBGRAPH WITH PROP 1 STEPS FROM "player100" OUT serve;
    2. +------------------------------------------------------+-------------------------------------------------------------------------+
    3. | _vertices | _edges |
    4. +------------------------------------------------------+-------------------------------------------------------------------------+
    5. | [("player100" :player{age: 42, name: "Tim Duncan"})] | [[:serve "player100"->"team200" @0 {end_year: 2016, start_year: 1997}]] |
    6. +------------------------------------------------------+-------------------------------------------------------------------------+
    7. | [("team200" :team{name: "Warriors"})] | [] |
    8. +------------------------------------------------------+-------------------------------------------------------------------------+

    返回的子图如下。

    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语句。例如:

  1. nebula> match p= (v:player) -- (v2) where id(v)=="A" return p;
  2. nebula> go 1 steps from "A" over follow;

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

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

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