K-Hop template khop().<path_template>
takes a path template of fixed length as shortest path when querying for k-hop neighbors. It is also a batch processing of each initial node found by the 1st n()
in the path template, so its parameter limit()
is limiting the maximum returns of each initial node. Upon achieving the same query function, K-Hop template yields better performance than path template.
Syntax:
- Statement alias: supported (NODE)
- Prefix: OPTIOANL (returns a pseudo node for any subquery that finds no result, with empty string as
_id
and 0 as_uuid
) - The 1st
n()
for initial-node must have filtering rules [<steps>]
in multi-edge template must be a definite value, namely[N]
- Inter-step filtering of any type is not supported, no matter using
prev_n
,prev_e
or preceding custom alias defined in the current path template
Common Usage
Example: Find the cards that Customer CU001's card reaches in 2-step shortest transaction paths, return the count of card's owners with all properties
khop()
.n({_id == "CU001"}).re().n({@card})
.le({@transfer})[2].n({@card})
.le().n({@customer}) as n
return n{*}
Analysis: This example can be regarded as a K-Hop query as it queries the dest of the shortest path. For khop()
command itself cannot set different rules to filter the directions and properties for the edges that appear sequentially in the path, it's necessary to use template to implement the entire UQL statement.
Example: as the path structure shown in the graph below, find the cards using the same phone numbers used by Customer CU013 and CU020's cards, count the number of these cards
khop()
.n({_id in ["CU013","CU020"]}).re().n({@card})
.re().n({@phone})
.le().n({@card}) as n
return count(n)