【问题标题】:Create if not exist Vertex and Edge in 1 Gremlin Query在 1 个 Gremlin 查询中创建(如果不存在)顶点和边
【发布时间】:2019-06-13 10:15:31
【问题描述】:

如果边缘尚不存在,我会找到以下代码来创建边缘。

g.V().hasLabel("V1")
.has("userId", userId).as("a")
.V().hasLabel("V1").has("userId", userId2)
.coalesce(
        bothE("link").where(outV().as("a")),
        addE("link").from("a")
)

它工作正常,但如果顶点和边在 1 个查询中不存在,我想创建它们。

我用新图尝试以下代码,它只是创建新顶点但它们之间没有关系。

g.V().hasLabel("V1")
.has("userId", userId).fold()
.coalesce(
        unfold(),
        addV("V1").property("userId", userId1)
).as("a")
.V().hasLabel("V1").has("userId", userId2).fold()
.coalesce(
        unfold(),
        addV("V1").property("userId", userId2)
)
.coalesce(
        bothE("link").where(outV().as("a")),
        addE("link").from("a")
)

【问题讨论】:

    标签: graph gremlin janusgraph


    【解决方案1】:

    我使用了@thangdc94 建议的方法(谢谢!),发现“映射”步骤需要很长时间,这个查询对我来说工作得更快(X20):

    g.V().has("V1","userId", userId1).fold().
      coalesce(unfold(),
               addV("V1").property("userId", userId1)).as("a").iterate();
      g.V().has("V1","userId", userId2).fold().
      coalesce(unfold(),
               addV("V1").property("userId", userId2)).as("b").
      V().has("V1","userId", userId1).
      coalesce(outE("link").where(inV().as("b")),
               addE("link").to("b"))
    

    评论:我使用了 Neptune DB

    【讨论】:

      【解决方案2】:

      感谢JanusGraph google group 中的 Daniel Kuppitz。我找到了解决方案。我在这里重新发布给需要的人。

      您的查询中有两个问题。第一个是它不能按预期工作的原因: fold() 步骤。使用 fold() 将破坏路径历史记录,但您可以通过在子遍历中执行该部分轻松解决它:

      g.V().has("V1","userId", userId1).fold().
        coalesce(unfold(),
                 addV("V1").property("userId", userId1)).as("a").
        map(V().has("V1","userId", userId2).fold()).
        coalesce(unfold(),
                 addV("V1").property("userId", userId2))
        coalesce(inE("link").where(outV().as("a")),
                 addE("link").from("a"))
      

      第二个问题是E和outV的结合。您应该使用bothE/otherVoutE/inVinE/outV

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-12-07
        • 2022-01-21
        • 2015-04-19
        • 2021-12-13
        • 2020-07-01
        • 2015-07-06
        • 1970-01-01
        • 2022-01-06
        相关资源
        最近更新 更多