K-Hop query is to find the neighbor nodes that a node can reach in K hops/steps the shortest, and return the list of the neighbors.
In the field of graph, the concept of K-Hop query is easily misunderstood. Ultipa defines K-Hop query as follows:
- K represents the number of steps from the start node, with a minimum value of 1;
- K-Hop neighbors are the nodes that can be reached from the start node at step K through the shortest path (set the weight of edges to 1);
- K-Hop neighbors will not appear in the neighbors of other steps, for instance, the nodes of 2-hop neighbor do not appear in the set of neighbor nodes of the 3-hop or other hops;
- The number of K-Hop neighbors refers to the number of all neighbors at step K (after deduplication).
- In finite graph, when the value of K is big enough, the number of K-Hop neighbors must be 0. Let's say a graph with diameter of 5, the 6-Hop neighbors of all nodes must all be 0.
K-Hop query is executed in a BFS (Breadth First Search) fashion 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:
- Command:
khop()
- Parameter: (see the table below)
- Statement Alias: Custom alias supported, structure type is NODE
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, -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{*}