Explore nearby connections
The language tabs use earlier client interfaces. Check SDK versions and connection setup before running them.
Breadth-first search visits nearby records before more distant ones. A hop is one relationship edge.
Before you run it: This traversal counts hops; it does not use edge weights.
First load the order-to-product sample dataset. It creates shop.orders.bought_product with the exact IDs used below.
With this one-link sample dataset, product p001 is one hop from order o001. More distant results need more linked rows; increasing max_depth does not create those links.
- Start with o001Treat the order as depth zero.
- Expand its relationFollow bought_product up to the requested depth.
- Read p001 at depth oneUse hop counts to see how far each match is.
Follow the example
- Prepare the relationship and rows.
- Choose a starting key.
- Set max_depth in hops.
- Read each returned key and depth.
Set up the relationship
The relation must be declared in the schema's [[relations]] block. See schemas/reference#relations.
Run the request
curl "https://$OC_HOST/v1/tenants/$OC_TENANT/graph/shop.orders/bfs?rel=bought_product&pk=o001&max_depth=3" \
-H "Authorization: Bearer $OC_TOKEN"Check the result
Each reachable node has its shortest hop distance. The start node is excluded.
[
{ "pk": "p001", "depth": 1 },
{ "pk": "p014", "depth": 1 },
{ "pk": "u091", "depth": 2 },
{ "pk": "u117", "depth": 2 },
{ "pk": "c003", "depth": 3 }
]An array of { pk, depth } objects. Depth is the shortest hop-count from the source; the source itself isn't in the result.
Understand the behavior
- The engine maintains a visited set and a frontier queue, expanding one depth at a time.
- Each ring is built by a batched adjacency lookup against the previous frontier - one point-read per node, no scans.
- Traversal stops when the frontier is empty or depth reaches
max_depth.
Important limits and checks
- Forgetting
max_depth. On dense graphs, a missing or very large depth can return millions of nodes. Set a real bound - 2 or 3 is usually enough. - Treating depth as cost. bfs counts hops, not weights. If your edges have meaningful weights, use dijkstra.