Query ab().src().dest().depth()
can find and return paths from an initial-node to a terminal-node, by specifying depth (number of edges), applying filters on the initial-node, terminal-node, all edges and all intermediate nodes. Paths with circle can be filtered out, and the number of results of subquery can be limited.
Syntax:
- Statement alias: supported (PATH)
- All parameters:
Parameter | Type | Specification | Description | Structure of Custom Alias |
---|---|---|---|---|
src() |
Filter | Mandatory | The filtering rules of the start node; error will occur if multiple nodes are found | NODE |
dest() |
Filter | Mandatory | The filtering rules of the end node; error will occur if multiple nodes are found | NODE |
depth() |
Range | Mandatory | To set the depth of the path depth(N) : N edges depth(:N) : 1~N edges depth(M:N) : M~N edges depth(N).shortest(<>) : the shortest (or the least weighted) paths within N edges |
Not supported |
shortest() |
/ or @<schema>.<property> |
Numeric edge property; LTE is required | When no edge property is specified, return the shortest path; when edge property is specified, return the least weighted paths using the specified property as the weight factor (edges without the property do not form the path). When shortest() is used, value inside depth() should be a constant, that is, depth(N).shortest(<>) , means the shortest (or the least weighted) paths within N edges |
Not supported |
node_filter() |
Filter | The filtering rules that nodes other than src and dest need to satisfy |
Not supported | |
edge_filter |
Filter | The filtering rules that all edges need to satisfy | Not supported | |
path_ascend() |
@<schema>.<property> |
Numeric edge property; LTE is required | To return paths where the specified property ascends (edges without the property do not form the path) | Not supported |
path_descend() |
@<schema>.<property> |
Numeric edge property; LTE is required | To return paths where the specified property descends (edges without the property do not form the path) | Not supported |
direction() |
String | left, right | To specify the direction of the edge | Not supported |
no_circle() |
/ | / | To dismiss the paths that have circles; see Basic Concept - Terminologies for the definition of circle | Not supported |
limit() |
Int | -1 or >=0 | Number of results to return for each subquery, -1 means to return all results | Not supported |
Sample graph: (to be used for the following examples)
create().edge_property(@default, "weight", int32)
insert().into(@default).nodes([{_id:"A", _uuid:1}, {_id:"B", _uuid:2}, {_id:"C", _uuid:3}, {_id:"D", _uuid:4}, {_id:"E", _uuid:5}, {_id:"F", _uuid:6}])
insert().into(@default).edges([{_uuid:1, _from_uuid:1, _to_uuid:3, weight:1}, {_uuid:2, _from_uuid:5, _to_uuid:2 , weight:1}, {_uuid:3, _from_uuid:1, _to_uuid:5 , weight:4}, {_uuid:4, _from_uuid:4, _to_uuid:3 , weight:2}, {_uuid:5, _from_uuid:5, _to_uuid:4 , weight:3}, {_uuid:6, _from_uuid:2, _to_uuid:1 , weight:2}, {_uuid:7, _from_uuid:6, _to_uuid:1 , weight:4}])
Filter Depth
Example: Find 3-step paths from A to E, carry all properties
ab().src({_id == "A"}).dest({_id == "E"}).depth(3) as p
return p{*}
A --1--> C <--4-- D <--5-- E
Example: Find 1~3-step paths from A to E, carry all properties
ab().src({_id == "A"}).dest({_id == "E"}).depth(:3) as p
return p{*}
A --3--> E
A --1--> C <--4-- D <--5-- E
A <--6-- B <--2-- E
Example: Find 2~3-step paths from A to E, carry all properties
ab().src({_id == "A"}).dest({_id == "E"}).depth(2:3) as p
return p{*}
A --1--> C <--4-- D <--5-- E
A <--6-- B <-2-- E
Non-weighted Shortest Path
Example: Find shortest paths from A to E within 3 steps, carry all properties
ab().src({_id == "A"}).dest({_id == "E"}).depth(3)
.shortest() as p
return p{*}
A --3--> E
Weighted Shortest Path
Example: Find shortest paths from A to E within 3 steps, use @default.weight as weight, carry all properties
ab().src({_id == "A"}).dest({_id == "E"}).depth(3)
.shortest(@default.weight) as p
return p{*}
A <--6-- B <--2-- E
Analysis: Porperty @default.weight should be loaded to engine (LTE).
Filter Intermediate Nodes
Example: Find 1~3-step paths from A to E and not passing D, carry all properties
ab().src({_id == "A"}).dest({_id == "E"}).depth(:3)
.node_filter({_id != "D"}) as p
return p{*}
A --3--> E
A <--6-- B <--2-- E
Filter Edges
Example: Find 1~3-step paths from A to E where the weight of edge is greater than 1, carry all properties
ab().src({_id == "A"}).dest({_id == "E"}).depth(:3)
.edge_filter({weight > 1}) as p
return p{*}
A --3--> E
Edge Property Ascend/Descend
Example: Find 1~3-step paths from A to E with property @default.weight ascending along the path, carry all properties
ab().src({_id == "A"}).dest({_id == "E"}).depth(:3)
.path_ascend(@default.weight) as p
return p{*}
A --3--> E
A --1--> C <--4-- D <--5-- E
Analysis: Porperty @default.weight should be loaded to engine (LTE).
Example: Find 1~3-step paths from A to E with property @default.weight descending along the path, carry all properties
ab().src({_id == "A"}).dest({_id == "E"}).depth(:3)
.path_descend(@default.weight) as p
return p{*}
A --3--> E
A <--6-- B <--2-- E
Analysis: Porperty @default.weight should be loaded to engine (LTE).
Filter Edge Direction
Example: Find 1~3-step paths from A to E with all edges right-pointing, carry all properties
ab().src({_id == "A"}).dest({_id == "E"}).depth(:3)
.direction(right) as p
return p{*}
A --3--> E
Example: Find 1~3-step paths from A to E with all edges left-pointing, carry all properties
ab().src({_id == "A"}).dest({_id == "E"}).depth(:3)
.direction(left) as p
return p{*}
A <--6-- B <--2-- E
Filter Circles
Example: Find 4-step paths from A to E that do not contain circle, carry all properties
ab().src({_id == "A"}).dest({_id == "C"}).depth(4).no_circle() as p
return p{*}
A <--6-- B <--2-- E --3--> D --4--> C
Analysis: Paths with circle will be returned if not using no_circle():
A --3--> E --2--> B --6--> A --1--> C
A <--6-- B <--2-- E --3--> D --4--> C
A <--6-- B <--2-- E <--3-- A --1--> C
limit()
Example: Find a 1~3-step path from A to E, carry all properties
ab().src({_id == "A"}).dest({_id == "E"}).depth(:3).limit(1) as p
return p{*}
A <--6-- B <--2-- E