You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何通过Gremlin获取遍历过程中经过的边?基于TinkerPop Modern示例

Gremlin Query to Extract Traversed Edges & Vertex Sequences (TinkerPop Modern)

Got it, let's break down how to get exactly the output format you need for the TinkerPop Modern graph. The goal is to capture every traversal path, extract the edge labels (your relation field), vertex IDs, and vertex names (your title field) in that structured dictionary format.

Core Query Solution

Here's a Gremlin query that does exactly what you're asking for. It works by traversing all possible paths (with at least one edge), then projecting the required fields from each path:

g.V()
  .repeat(outE().inV())
  .emit()
  .path()
  .project('relation', 'id', 'title')
    .by(path().objects().is(Edge).label().fold())
    .by(path().objects().is(Vertex).id().fold())
    .by(path().objects().is(Vertex).values('name').fold())

Let's Walk Through What This Does

  1. g.V(): Starts traversal from every vertex in the TinkerPop Modern graph. If you want to start from a specific vertex (e.g., Vadas), replace this with g.V(<vertex-id>) (like g.V(2) for Vadas).
  2. repeat(outE().inV()).emit(): Repeatedly traverses outgoing edges to adjacent vertices, and emits every path that has at least one edge (so we don't get empty paths). Add .times(N) inside repeat() if you want to limit path length (e.g., .times(4) for max 4 edges in a path).
  3. path(): Captures the full traversal path, which includes both vertices and edges in the order they were traversed.
  4. project('relation', 'id', 'title'): Builds the output structure with your three required fields:
    • by(path().objects().is(Edge).label().fold()): Extracts all edges from the path, grabs their labels (like knows or created), and folds them into a list for the relation field.
    • by(path().objects().is(Vertex).id().fold()): Extracts all vertices from the path, grabs their unique IDs, and folds them into a list for the id field.
    • by(path().objects().is(Vertex).values('name').fold()): Extracts all vertices from the path, grabs their name property (Marko, Vadas, etc.), and folds them into a list for the title field.

Example Output (Matching Your Format)

For the path Vadas → Marko → Josh → Ripple, the query will return:

{"relation": ["knows", "knows", "created"], "id": [2, 1, 3, 5], "title": ["vadas", "marko", "josh", "ripple"]}

(Note: The IDs here match TinkerPop Modern's default IDs; your example uses different numeric IDs, which just reflects your graph's actual storage IDs—this query will pull whatever IDs exist in your graph.)

Alternative for Graphs That Don't Support is(Vertex)/is(Edge)

If your graph database doesn't support checking element types directly, you can filter by label instead (since TinkerPop Modern uses person/software for vertices and knows/created for edges):

g.V()
  .repeat(outE().inV())
  .emit()
  .path()
  .project('relation', 'id', 'title')
    .by(path().objects().hasLabel(within('knows','created')).label().fold())
    .by(path().objects().hasLabel(within('person','software')).id().fold())
    .by(path().objects().hasLabel(within('person','software')).values('name').fold())

内容的提问来源于stack exchange,提问作者Srinath Ganesh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.21 04:27:18