Graph 如果不存在顶点和边,则创建1查询小精灵

Graph 如果不存在顶点和边,则创建1查询小精灵,graph,gremlin,janusgraph,Graph,Gremlin,Janusgraph,我发现下面的代码可以创建edge,如果它还不存在的话 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个查询中 我用新的图形尝试

我发现下面的代码可以创建edge,如果它还不存在的话

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")
)

感谢Daniel Kuppitz的参与。我找到了解决办法。我把它重新贴在这里给任何需要它的人

您的查询中有两个问题。第一个原因是它没有按预期工作的原因:折叠步骤。使用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"))

第二个问题是两者的结合。您应该同时使用/otherV、outE/inV或inE/outV。

多亏了Daniel Kuppitz的帮助。我找到了解决办法。我把它重新贴在这里给任何需要它的人

您的查询中有两个问题。第一个原因是它没有按预期工作的原因:折叠步骤。使用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"))
第二个问题是两者的结合。您应该同时使用/otherV、outE/inV或inE/outV