代码之家  ›  专栏  ›  技术社区  ›  thangdc94

在1个查询gremlin中创建不存在的顶点和边

  •  0
  • thangdc94  · 技术社区  · 6 年前

    如果边缘还不存在,我会找到下面的代码来创建它。

    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")
    )
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   thangdc94    6 年前

    多亏了丹尼尔·库皮茨 JanusGraph google group . 我找到了解决办法。我把它重新贴在这里给任何需要它的人。

    您的查询中有两个问题。第一个是它不能按预期工作的原因: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"))
    

    第二个问题是bothe和outv的结合。你应该使用 bothE/otherV , outE/inV inE/outV .