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. Queries

Find Subgraphs

Overview

The subgraph([<path_template1>, <path_template2>, ...]) statement describes and searches subgraphs conform to a structure formed by multiple path template. These path templates typically intersect at nodes or edges, achieved through the reuse of aliases to establish shared elements within the subgraph.

Syntax

  • Statement alias: N/A
  • It is not permitted to declare aliases for the path templates involved, but aliases can be defined within the single-node templates n() and single-edge templates e().
Syntax
subgraph([
  <path_template1>, 
  <path_template2>, 
  ...
])

Example Graph

To create the graph, execute each of the following UQL queries sequentially in an empty graphset:

UQL
create().node_schema("user").node_schema("card").edge_schema("owns").edge_schema("transfers")
insert().into(@user).nodes([{_id:"user1"}, {_id:"user2"}, {_id:"user3"}, {_id:"user4"}, {_id:"user5"}])
insert().into(@card).nodes([{_id:"card14"}, {_id:"card22"}, {_id:"card37"}, {_id:"card45"}, {_id:"card63"}, {_id:"card65"}])
insert().into(@owns).edges([{_from:"user1", _to:"card45"}, {_from:"user2", _to:"card65"}, {_from:"user3", _to:"card37"}, {_from:"user4", _to:"card14"}, {_from:"user4", _to:"card22"}, {_from:"user5", _to:"card63"}])
insert().into(@transfers).edges([{_from:"card45", _to:"card14"}, {_from:"card65", _to:"card14"}, {_from:"card63", _to:"card14"}, {_from:"card65", _to:"card37"}, {_from:"card22", _to:"card65"}, {_from:"card37", _to:"card22"}, {_from:"card22", _to:"card37"}, {_from:"card22", _to:"card14"}])

Single Intersection

To find users who received direct transaction from both user1 and user2:

UQL
subgraph([
  n({_id == "user1"}).e().n({@card}).re({@transfers}).n({@card} as c).e().n({@user} as u),
  n({_id == "user2"}).e().n({@card}).re({@transfers}).n(c)
])
return u._id

Result:

u._id
user4

Multiple Intersections

To find 3-step single-direction circular transaction paths between different accounts, return those account owners:

UQL
subgraph([
  n({@user} as u1).e().n({@card} as c1),
  n({@user && _id > u1._id} as u2).e().n({@card} as c2),
  n({@user && _id > u2._id} as u3).e().n({@card} as c3),
  n(c1).re().n(c2).re().n(c3).re().n(c1)
])
return table(u1._id, u2._id, u3._id)
u1._idu2._idu3._id
user2user3user4

To find card pairs which both cards sent transactions to cards card14 and card37, and there are direct transactions between the card pair:

UQL
subgraph([
  n({@card} as c1).e().n({@card} as c2),
  n(c1 as c11).re().n({_id == "card14"}).le().n(c2 as c22),
  n(c11).re().n({_id == "card37"}).le().n(c22)
])
where c1._id > c2._id
return table(c11._id, c22._id)

Result:

c11._idc22._id
card65card22

If an alias needs to be referenced multiple times in different path templates within a subgraph(), it must be renamed after it is referenced the first time to keep it in scope. This ensures that the alias is available across multiple templates and avoids any potential conflicts or out-of-scope errors.