使用 as() 和 coalesce() 时更新插入失败

Upsert fails when using as() and coalesce()

我正在尝试在 Gremlin 中创建一个 upsert 遍历。如果存在则更新一条边,否则添加一条新边。

g.V("123")
.as("user")
.V("456")
.as("post")
.inE("like")
.fold()
.coalesce(
  __.unfold()
  .property("likeCount", 1),
  __.addE("like")
  .from("user")
  .to("post")
)

这returns一个错误。

The provided traverser does not map to a value: []->[SelectOneStep(last,post)]

我已将其缩小到 to("post") 步骤。从 coalesce 中看不到 as("post") 中的 post。也看不到user.

这对我来说很奇怪,因为以下确实有效:

g.V("123")
.as("user")
.V("456")
.as("post")
.choose(
  __.inE("like"),
  __.inE("like")
    .property("likeCount", 1),
  __.addE("like")
    .from("user")
    .to("post")
)

choose() 步骤中,我可以访问 userpost

我想使用更高效的更新插入模式,但无法解决这个问题。我可以像这样从 coalesce 中查找 userpost

g.V("123")
.as("user")
.V("456")
.as("post")
.inE("like")
.fold()
.coalesce(
  __.unfold()
  .property("likeCount", 1),
  __.V("456")
  .as("post")
  .V("123")
  .addE("like")
  .to("post")
)

但重复遍历似乎效率低下。由于其他原因,我在外部遍历中需要postuser

为什么我无法在第一个示例中从 coalesce 中访问 userpost

您 运行 遇到的问题是,一旦您点击代码中的 fold() 步骤,您 lose the path history,这意味着它不会知道 userpost 指的是。 fold() 是所谓的 ReducingBarrierStep,这意味着许多结果被收集到一个结果中。我的想法是,因为您已将许多结果转换为一个结果,所以添加的别名(例如 userpost)不再真正有意义,因为它们都已收集到一个单个元素。

但是您可以按照此处所示重写查询以获得所需的结果:

g.V("456")
.inE("like")
.fold()
.coalesce(
  __.unfold()
  .property("likeCount", 1),
  __.addE("like")
  .from(V("123"))
  .to(V("456"))
)

我也不确定您是否只想在现有边上添加相似计数,或者您是否想在任何一种情况下都将相似计数添加到边缘,就像这样:

g.V("456")
.inE("like")
.fold()
.coalesce(
  __.unfold(),
  __.addE("like")
  .from(V("123"))
  .to(V("456"))
).property("likeCount", 1)