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

如何在Amazon Neptune中用Gremlin实现基于Right类型交集的动态图遍历?

Gremlin遍历问题:基于Right顶点types交集的路径过滤(Amazon Neptune)

我的图结构

图结构

图构建代码

// 可直接复制粘贴到Gremlin控制台执行
g\
.addV("Person").property(T.id, "A").as("a")\
.addV("Person").property(T.id, "B").as("b")\
.addV("Person").property(T.id, "C").as("c")\
.addV("Person").property(T.id, "D").as("d")\
.addV("Person").property(T.id, "E").as("e")\
.addV("Right").property(T.id, "1").property(set, "types", 1).property(set, "types", 2).as("r1")\
.addV("Right").property(T.id, "2").property(set, "types", 2).property(set, "types", 3).as("r2")\
.addV("Right").property(T.id, "3").property(set, "types", 4).property(set, "types", 5).as("r3")\
.addV("Right").property(T.id, "4").property(set, "types", 2).as("r4")\
.addE("receives").from("r1").to("a").property(T.id, "a")\
.addE("receives").from("b").to("r1").property(T.id, "b")\
.addE("receives").from("r2").to("b").property(T.id, "c")\
.addE("receives").from("r3").to("b").property(T.id, "d")\
.addE("receives").from("d").to("r3").property(T.id, "e")\
.addE("receives").from("c").to("r2").property(T.id, "f")\
.addE("receives").from("r4").to("c").property(T.id, "g")\
.addE("receives").from("e").to("r4").property(T.id, "h")

需求说明

需要从顶点A遍历到顶点E,途中检查每个Right顶点的types属性是否与上一步的types存在交集:

  • 存在交集则继续沿该边遍历
  • 无交集则停止,忽略该Right顶点

已尝试的查询

g.withSack([2]).V("A").
repeat(
  inE("receives").outV().
    filter(values("types").fold().intersect(sack()).unfold().aggregate("inter")).
  inE("receives").outV().as("current").
  choose(inE().hasLabel("receives").count().is(0), select("current"), inE("receives").
    outV().filter(values("types").fold().intersect(select("inter")).unfold()).inE("receives").outV()).
  sack(assign).by(select("inter"))).
until(inE().hasLabel("receives").count().is(0))

存在的问题

  1. Sack初始值硬编码:尝试将Sack初始化为A关联的Right 1的types时,intersect()函数无法正常工作,示例代码:
g.withSack([]).V("A").as("start").inE("receives").outV()
.sack(assign).by(values("types").fold())
.select("start").repeat(
  1. 全局折叠types:values("types").fold()会将当前遍历前沿所有节点的types折叠,希望能按单个节点局部评估types,是否需要使用local?

疑问

能否在Amazon Neptune中通过Gremlin实现这种基于交集的动态、步骤范围的过滤遍历?

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.22 11:50:24