如果 1 个 Gremlin 查询中不存在顶点和边,则创建

tha*_*c94 2 graph gremlin janusgraph

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

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")
)
Run Code Online (Sandbox Code Playgroud)

它工作正常,但我想创建顶点和边(如果它们不存在于 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")
)
Run Code Online (Sandbox Code Playgroud)

tha*_*c94 7

感谢JanusGraph google 小组中的 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"))
Run Code Online (Sandbox Code Playgroud)

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