Graph Queries

Native graph traversals and shortest path algorithms for exploring connected data.

Graph Traversal Operations

SoliDB supports native graph traversals and shortest path algorithms using dedicated keywords.

TRAVERSAL Graph Traversal

Traverse the graph starting from a vertex.

FOR vertex[, edge] IN [min..max] DIRECTION startVertex edgeCollection
OUTBOUND
_from → _to
INBOUND
_to ← _from
ANY
Either direction

Direct (1 Hop)

FOR v IN OUTBOUND "users/alice" follows
  RETURN v.name

Variable Depth (1..2 Hops)

FOR v, e IN 1..2 ANY "users/alice" follows
  RETURN { user: v.name, type: e.type }

ANALYTICS Graph Analytics Functions

SoliDB includes built-in graph algorithms for centrality and importance ranking.

PAGERANK(edgeCollection, options?)

Computes PageRank over the graph defined by an edge collection. Returns [{node, score}, ...] sorted by score descending.

PAGERANK("follows", { damping: 0.85, limit: 20 })
// [{ "node": "users/bob", "score": 0.312 }, ...]
DEGREE_CENTRALITY(edgeCollection)

Returns degree (number of connections) for each node.

DEGREE_CENTRALITY("follows")

SHORTEST_PATH Shortest Path

Hop-count BFS by default. OPTIONS { weight: "cost" } runs Dijkstra on that numeric edge field (missing = 1; negatives error). Also ALL_SHORTEST_PATHS, K_SHORTEST_PATHS (k), K_PATHS (min/max/limit). Bind p for {vertices, edges, weight}. Cap: SOLIDB_MAX_PATHS (256).

FOR vertex[, edge] IN SHORTEST_PATH start TO end DIRECTION edgeCollection

Find Path

FOR v, e IN OUTBOUND SHORTEST_PATH
  "users/alice" TO "users/charlie"
  follows
  RETURN { vertex: v.name, edge: e }

PRUNE / path Path variable and PRUNE

A third identifier after FOR v, e, p binds the walk so far as { vertices, edges }. PRUNE expr is evaluated on the current vertex; if true, that branch is not expanded.

FOR v, e, p IN 1..3 OUTBOUND "users/alice" GRAPH follows
  PRUNE v.blocked == true
  RETURN { name: v.name, hops: LENGTH(p.vertices) }

MATCH (a:users {_key: "alice"})-[:follows*1..3]->(b)
  RETURN b.name

Practical Examples

Find All Friends of Friends

FOR friend IN 2..2 OUTBOUND "users/alice" follows
  RETURN DISTINCT friend.name

Find Mutual Followers

LET aliceFollows = (
  FOR v IN OUTBOUND "users/alice" follows
    RETURN v._key
)
LET bobFollows = (
  FOR v IN OUTBOUND "users/bob" follows
    RETURN v._key
)
RETURN INTERSECTION(aliceFollows, bobFollows)

Traverse with Edge Filtering

FOR v, e IN 1..3 OUTBOUND "users/alice" follows
  FILTER e.weight > 0.5
  RETURN { user: v.name, relationship: e.type }