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。可以用IN
、OUT
和BOTH
来指定起始点上该Edge type的方向。默认为BOTH
。
示例
以下面的示例图进行演示。
查询从点
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 {}]] |
+-------------------------------------------------------------------------+-----------------------------------------------------------------------------------------------------------------------------+
返回的子图如下。
查询从点
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"})] | [] |
+------------------------------------------------------+-------------------------------------------------------------------------+
返回的子图如下。
FAQ
为什么返回结果中会出现超出step_count
跳数之外的关系?
为了展示子图的完整性,会在满足条件的所有点上额外查询一跳。例如下图。
用
GET SUBGRAPH 1 STEPS FROM "A";
查询的满足结果的路径是A->B
、B->A
和A->C
,为了子图的完整性,会在满足结果的点上额外查询一跳,即B->C
。用
GET SUBGRAPH 1 STEPS FROM "A" IN follow;
查询的满足结果的路径是B->A
,在满足结果的点上额外查询一跳,即A->B
。
如果只是查询满足条件的路径或点,建议使用MATCH或GO语句。例如:
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 {}]] |
+-------------------------------------------------------+--------------------------------------------+