概述
使用语句spread().src().depth()
可以执行广度优先搜索,从每个遍历起点向外展开,逐层获取一步路径,随着路径逐步加深,向邻居点扩展。

这就是从遍历起点(上图中的红色节点)逐层展开的过程。在第k
步:
- 识别遍历起点的
k-1
步邻居(k = 1
时对应遍历起点)与k
步邻居之间的一步路径。 - 与此同时,发现
k
步邻居之间的一步路径,包括k
步邻居的自环路径。 - 特别注意,遍历起点的自环路径被识别为第一步路径。
语法
spread().src(<filter?>).depth(<steps>)
- 语句别名:类型为
PATH
- 方法:
方法 |
参数 |
描述 | 可选 |
别名类型 |
---|---|---|---|---|
src() |
<filter?> |
将过滤条件包裹在{} 中,或使用别名指定遍历的起点集。 留空时会作用在所有点上 |
否 | NODE |
depth() |
<steps> |
展开的最大深度(≥1) | 否 | N/A |
node_filter() |
<filter?> |
将过滤条件包裹在{} 中,对路径中除遍历起点外的所有点生效。留空则不应用任何过滤条件 |
是 | N/A |
edge_filter() |
<filter?> |
将针对路径中边的过滤条件包裹在{} 中。留空则不应用任何过滤条件 |
是 | N/A |
direction() |
<leftRight> |
指定向外展开时,路径中边的方向,可以为left 或right |
是 | N/A |
limit() |
<N> |
限制每个遍历起点返回的路径数量(N ≥-1);-1 表示返回所有路径 |
是 | N/A |
示例图集

在一个空图集中,逐行运行以下语句,创建示例图集:
create().edge_property(@default, "weight", int32)
insert().into(@default).nodes([{_id:"A"}, {_id:"B"}, {_id:"C"}, {_id:"D"}, {_id:"E"}, {_id:"F"}, {_id:"G"}])
insert().into(@default).edges([{_from:"A", _to:"C", weight:1}, {_from:"E", _to:"B", weight:1}, {_from:"A", _to:"E", weight:4}, {_from:"D", _to:"C", weight:2}, {_from:"E", _to:"D", weight:3}, {_from:"B", _to:"A", weight:2}, {_from:"F", _to:"A", weight:4}])
从点展开
从点B
展开1步:
spread().src({_id == "B"}).depth(1) as p
return p
结果:p

从点B
展开2步:
spread().src({_id == "B"}).depth(2) as p
return p
结果:p

过滤邻居点
从点D
展开2步,同时不经过点E
:
spread().src({_id == "D"}).depth(2).node_filter({_id != "E"}) as p
return p
结果:p

不经过点E
意味着从图中移除点E
和与其相连的边。
过滤边
分别从点A
和点B
展开2步,同时仅经过边属性weight
大于1的边:
spread().src({_id in ["A", "B"]}).depth(2).edge_filter({weight > 1}) as p
return p
结果:p

排除属性weight
不超过1的边,相当于将这些边从图中移除。
设置边方向
从点B
沿出向边展开2步:
spread().src({_id == "B"}).depth(2).direction(right) as p
return p
结果:p

从点B
沿入向边展开2步:
spread().src({_id == "B"}).depth(2).direction(left) as p
return p
结果:p

虽然语句spread()
返回的结果为出向一步路径格式,但direction()
方法限制了从当前点向外查询的方向。
限制查询数量
分别从点A
和点B
展开2步,且每个遍历起点仅返回2条路径:
spread().src({_id in ["A", "D"]}).depth(2).limit(2) as p
return p
结果:p

基于广度优先搜索的特性,展开过程中,优先返回浅层路径。
使用OPTIONAL
本条查询中,spread()
语句执行两次,每次使用n
中的一条记录。使用OPTIONAL
前缀后,如果没有查询到数据,则返回null
:
find().nodes({_id in ["F", "G"]}) as n
OPTIONAL spread().src(n).depth(1) as p
return p
结果:p

若不使用OPTIONAL
前缀,则仅返回一条结果:
find().nodes({_id in ["F", "G"]}) as n
spread().src(n).depth(1) as p
return p
结果:p
