微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

在 Gremlin 的单次遍历中向上插入顶点和边

如何解决在 Gremlin 的单次遍历中向上插入顶点和边

我已经尝试了几个小时来编写一个 gremlin 语句来处理向上插入 2 个顶点和它们之间的 1 个边,但运气不佳。

在伪小鬼中,我想做的很简单,如下所示:

g.V()
.hasLabel("person")
.has("custom_id","123")
.fold()
.coalesce(
  __.unfold().property(single,"name","Tom"),__.addV("person").property(single,"custom_id","123").property(single,"Tom"))
.as("fromStep")
.V()
.hasLabel("person")
.has("custom_id","654")
.fold()
.coalesce(
  __.unfold().property(single,"Sally"),"654").property(single,"Sally"))
.as("toStep")
.E()
.hasLabel("kNows")
.where(__.inV().is("fromStep"))
.where(__.outV().is("toStep"))
.fold()
.coalesce(
  __.unfold().property("since","2020-01-01"),__.addE("kNows").property("since","2020-01-01").from("fromStep").to("toStep")

代码的问题在于,作为屏障步骤的每个 fold 步骤都会“移除”之前 as 步骤的值。

有没有办法在单个语句中正确地做到这一点?

解决方

感谢 Kelvin 的回答,这是解决方案 :)

g.V()
.hasLabel("person")
.has("custom_id","123")
.fold()
.coalesce(
    __.unfold().property(single,"Tom"))
.store("a")
.V()
.hasLabel("person")
.has("custom_id","654")
.fold()
.coalesce(
    __.unfold().property(single,"Sally"))
.store("b")
.fold()
.select("a").unfold().as("from")
.select("b").unfold().coalesce(
    __.inE("kNows").where(__.outV().as("from")).property("since","2020-01-01").from("from")
)

解决方法

如您所见,使用 as 步骤应用的标签在使用 fold 等减少屏障步骤后丢失。但是,storeaggregate 批量集将完好无损。这是一个在 fold 之后工作的简单模式,您应该能够适应您的用例。正如我们在评论中所讨论的那样,如果您知道要搜索的顶点的 ID,并且未找到该 ID,那么您可以创建它,这将为您提供另一种方法来解决这个问题。无论如何,这是一种在存在 fold 步骤的情况下有效的方法。

gremlin> g.V('3').store('a').
......1>   V('4').store('b').
......2>   fold().
......3>   select('a').unfold().as('from').
......4>   select('b').unfold().as('to').
......5>   addE('test').
......6>     from('from').
......7>     to('to')    

==>e[60876][3-test->4] 

另外一个注意事项,一般不支持中间遍历 E 步。它真的应该写成V().outE()

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。