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

Gremlin路径遍历:获取站点间路径及关联announcement顶点

解决方案

你可以通过project()步骤同时保留完整路径,并收集路径中所有站点关联的公告顶点。以下是两种实现方式,按需选择:

方式1:收集所有公告到一个数组

这种方式会把路径中所有站点的公告顶点合并成一个数组返回:

g.V()
  .has("station", "code", "ABC")
  .repeat(out("travelsTo").simplePath())
  .until(has("station", "code", "XYZ"))
  .project("path", "announcements")
    .by(path()) // 保留包含顶点和边的完整路径
    .by(
      path().unfold() // 展开路径中的每个元素(顶点+边)
        .hasLabel("station") // 仅筛选路径里的站点顶点
        .in("announcedOn") // 获取当前站点关联的所有公告顶点
        .fold() // 将所有公告合并为一个数组
    )

方式2:按站点分组展示公告

如果需要清晰看到每个站点对应的公告,可以按站点分组:

g.V()
  .has("station", "code", "ABC")
  .repeat(out("travelsTo").simplePath())
  .until(has("station", "code", "XYZ"))
  .project("path", "stationAnnouncements")
    .by(path())
    .by(
      path().unfold()
        .hasLabel("station")
        .project("stationCode", "announcements")
          .by("code") // 输出站点编码
          .by(in("announcedOn").fold()) // 输出当前站点的公告数组
        .fold()
    )

关键逻辑说明

  • project():用来构造包含多个字段的结果,这里同时存路径和公告信息
  • path().unfold():把路径里的顶点和边逐个展开,方便筛选处理
  • hasLabel("station"):过滤出路径中的站点顶点(排除路径里的边)
  • in("announcedOn"):因为announcement是通过入边announcedOn关联到station,所以用in()方向遍历获取公告顶点

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.27 03:55:00