Statement khop().src().depth()
can find and return nodes that a start node can reach in K hops/steps the shortest, by specifying depth (number of edges), applying filters on the nodes and edges.
K-Hop is a basic concept in graph theory, when the filtering conditions on nodes and edges are specified, the k-index of a node regarding to the subject node is either unique or non-existent. Ultipa implements K-Hop query using BFS (Breadth First Search) algorithm to guarantee the paths to reach the neighbors are the shortest.

K-Hop is a graph neighbor query function with an optimized and greatly improved performance, it is suggested to use K-Hop instead of other path query methods when querying neighbor nodes.
Syntax:
- Statement alias: supported (NODE)
- All parameters:
Parameter | Type | Specification | Description | Structure Type of Custom Alias |
---|---|---|---|---|
src() |
Filter | Mandatory | The filtering rules of the start 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 |
Not supported |
node_filter() |
Filter | The filtering rules that neighbor nodes other than the src need to satisfy |
Not supported | |
edge_filter() |
Filter | The filtering rules that all edges need to satisfy | Not supported | |
direction() |
String | left, right | To specify the direction of the edges | Not supported |
limit() |
Int | -1 or >=0 | Number of results to return for each subquery, -1 means to return all results | Not supported |
N Hops
Example: find 10 Card CA001's 3-Hop neighbors, return all node properties
khop().src({_id == "CA001"}).depth(3) as n
limit 10
return n{*}
1~N Hops
Example: find 10 Card CA001's neighbors within 3 hops, return all node properties
khop().src({_id == "CA001"}).depth(:3) as n
limit 10
return n{*}
M~N Hops
Example: find 10 Card CA001's neighbors within 2~3 hops, return all node properties
khop().src({_id == "CA001"}).depth(2:3) as n
limit 10
return n{*}
node_filter()
Example: find 10 Card CA001's neighbors within 3 hops, return all non-@card node properties
khop().src({_id == "CA001"}).depth(:3)
.node_filter({!@card}) as n
limit 10
return n{*}
edge_filter()
Example: find 10 Card CA001's neighbors within 3 hops, return all non-@transfer edge properties
khop().src({_id == "CA001"}).depth(:3)
.edge_filter({!@transfer}) as n
limit 10
return n{*}
direction(right)
Example: find Card CA001's neighbors within 3 hops, return 10 nodes that are connected by edges in the right direction (outbound) and their properties
khop().src({_id == "CA001"}).depth(:3)
.direction(right) as n
limit 10
return n{*}
direction(left)
Example: find Card CA001's neighbors within 3 hops, return 10 nodes that are connected by edges in the left direction (inbound) and their properties
khop().src({_id == "CA001"}).depth(:3)
.direction(left) as n
limit 10
return n{*}
limit
Example: find Card CA001 and Card CA002's neighbors within 3 hops, return 3 neighbors' properties for each node
uncollect ["CA001","CA002"] as n1
khop().src({_id == n1}).depth(3)
.limit(3) as n2
return n2{*}