UltipaDocs
Try Playground
  • Introduction
  • Terminologies
    • Reserved Words
    • Data Types
    • Alias
    • Operators
    • Expression
    • Filter
    • Prefix
    • Node and Edge Templates
    • Homologous and Heterologous Data
    • Clause Execution Times
    • Graphset
    • Schema
    • Property
    • Insert
    • Overwrite
    • Upsert
    • Update
    • Delete
    • Find Nodes
    • Find Edges
      • AB
      • Autonet
      • Spread
      • Path Template
      • K-Hop
      • K-Hop Template
    • Find Subgraphs
    • GROUP BY
    • ORDER BY
    • SKIP
    • LIMIT
    • WHERE
    • RETURN
    • WITH
    • UNCOLLECT
    • UNION
    • UNION ALL
    • CALL
    • BATCH
      • Schema Checker
      • Equal
      • Not Equal
      • Less Than
      • Greater Than
      • Less Than or Equal
      • Greater Than or Equal
      • Between
      • Between or Equal
      • Beong to
      • Not Belong To
      • CONTAINS | String
      • CONTAINS | Full-Text
      • Regular Match
      • IS NULL
      • IS NOT NULL
      • And
      • Or
      • Not
      • Exclusive OR
      • DISTINCT
      • toString()
      • toInteger()
      • toFloat()
      • toDouble()
      • toDecimal()
      • toSet()
      • castToRaw()
      • now()
      • dateAdd()
      • dateDiff()
      • year()
      • month()
      • day()
      • dayOfWeek()
      • dateFormat()
      • point()
      • distance()
      • pointInPolygon()
      • lower()
      • upper()
      • reverse()
      • startsWith()
      • endsWith()
      • JSON_decode()
      • JSON_merge()
      • trim()
      • ltrim()
      • rtrim()
      • left()
      • right()
      • substring()
      • replace()
      • split()
      • intersection()
      • difference()
      • listUnion()
      • size()
      • head()
      • reduce()
      • listContains()
      • append()
      • pi()
      • pow()
      • sqrt()
      • abs()
      • floor()
      • ceil()
      • round()
      • sin()
      • cos()
      • tan()
      • cot()
      • asin()
      • acos()
      • atan()
      • length()
      • pnodes()
      • pedges()
      • count()
      • sum()
      • max()
      • min()
      • avg()
      • stddev()
      • collect()
      • dedup()
      • CASE
      • table()
      • coalesce()
      • ifnull()
    • Acceleration
    • Index
    • Full-text
    • LTE
    • Real-time Process
    • Backend Task
    • Analytics Node
    • Server Statistics
    • Server Backup
    • Privilege
    • Policy
    • User
  • Trigger
  1. Docs
  2. /
  3. UQL
  4. /
  5. Find K-Hop Neighbors

K-Hop

Overview

The k-hop clause khop().src().depth() retrieves the neighbor nodes a node can reach in K hops (through K edges) the shortest. These neighbors are commonly referred to as the k-hop neighbors of the source node.

K-hop neighbor is one of the fundamental concepts in graph theory. In the graph above, nodes B, C and D are the 1-hop neighbors of node A, nodes E, F and G are the 2-hop neighbors of node A, and node H is the 3-hop neighbor of node A.

The value of k is unique and depends on the length of the shortest paths between two nodes. For example, although there are many paths exist between nodes A and C (A-C, A-D-C, A-D-E-C), the shortest distance is 1. Node C shouldn't appear in the results other than 1-hop neighbors.

The results of k-hop queries are deduplicated. For example, there are two shortest paths exist between nodes A and E, but E should only appear once in the 2-hop query of node A.

Ultipa's k-hop query adopts the BFS traversal technique to find the shortest paths and the k-hop neighbors. Optimizations have been applied to improve the performance of the k-hop query. It's recommended to use the k-hop query instead of other path query methods for the same purpose.

Syntax

  • Clause alias: NODE type
  • Methods:
Method
Param Type
Param Spec
Required
Description
Alias
src()Filter/YesThe conditions to specify the one and only one source nodeNODE
depth()Range/YesDepth for search (N≥1):
depth(N): N hops
depth(:N): 1~N hops
depth(M:N): M~N hops (M≥0)

When a range is set, the clause returns neighbor nodes in order from nearest to farthest
N/A
node_filter()Filter/NoThe conditions to specify all nodes (other than the source node) in the querying pathsN/A
edge_filter()Filter/NoThe conditions to specify all edges in the querying pathsN/A
direction()Stringleft, rightNoDirection of all edges in the querying pathsN/A
limit()Integer≥-1NoNumber of results to return for each subquery, -1 signifies returning allN/A
NOTE

The exclusion of certain nodes or edges through node_filter() or edge_filter() might induce structural change to the graph, potentially influencing the query outcomes. See examples under Node Filtering and Edge Filtering.

Examples

Example Graph

Run these UQLs row by row in an empty graphset to create this graph:

UQL
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}])
Click to expand

Set Depth

Find the 3-hop neighbors of node D.

UQL
khop().src({_id == "D"}).depth(3) as n
return n{*}

Result:

_id_uuid
F6

Find 1-hop to 3-hop neighbors of node D.

UQL
khop().src({_id == "D"}).depth(:3) as n
return n{*}

Result:

_id_uuid
F6
B2
A1
C3
E5

Return Source Node

Find the 1- to 2-hop neighbors of node D. Return node D at the same time.

UQL
khop().src({_id == "D"}).depth(0:2) as n
return n{*}

Result:

_id_uuid
B2
A1
C3
E5
D4

Node Filtering

Find the 3-hop neighbors of node D while excluding node E.

UQL
khop().src({_id == "D"}).depth(3).node_filter({_id != "E"}) as n
return n{*}

Result:

_id_uuid
F6
B2

When node E (and its adjacent edges) is excluded, node B becomes the 3-hop neighbor of node D.

Edge Filtering

Find the 3-hop neighbors of node D while excluding edge 5.

UQL
khop().src({_id == "D"}).depth(3).edge_filter({_uuid != 5}) as n
return n{*}

Result:

_id_uuid
E5
F6
B2

When edge 5 is excluded, node E and B become the 3-hop neighbor of node D.

Set Edge Direction

Find the 1- to 2-hop neighbors of node D while ensuring that all edges that pass through point to the right.

UQL
khop().src({_id == "D"}).depth(:2).direction(right) as n
return n{*}

Result:

_id_uuid
C3

Call Alias in src()

Find the 1-hop neighbors of nodes D and F.

UQL
find().nodes({_id in ["D", "F"]}) as start
khop().src(start).depth(1).direction(right) as n
return table(start._id, n._id)

Result:

start._idn._id
DC
FA

Use limit()

Find three 1- to 3-hop neighbors of node D.

UQL
khop().src({_id == "D"}).depth(:3).limit(3) as n
return n{*}

Result:

_id_uuid
A1
C3
E5

The k-hop clause returns neighbor nodes in order from nearest to farthest, starting with 1-hop, followed by 2-hop, and then 3-hop.

Use OPTIONAL

Find the 2-hop neighbors of nodes A and D while ensuring that all edges that pass through point to the right. Return null if no neighbors are found.

UQL
find().nodes({_id in ["A", "D"]}) as start
optional khop().src(start).depth(2).direction(right) as n
return table(start._id, n._id)

Result:

start._idn._id
AD
AB
Dnull