如果在 1 个 Gremlin 查询中不存在顶点和边则创建
Create if not exist Vertex and Edge in 1 Gremlin Query
如果边还不存在,我找到了下面的代码来创建边。
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")
)
感谢 JanusGraph google group 中的 Daniel Kuppitz。我找到了解决方案。我在这里为任何需要它的人重新post它。
您的查询中有两个问题。第一个是它没有按预期工作的原因: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
.
我使用了 @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
如果边还不存在,我找到了下面的代码来创建边。
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")
)
感谢 JanusGraph google group 中的 Daniel Kuppitz。我找到了解决方案。我在这里为任何需要它的人重新post它。
您的查询中有两个问题。第一个是它没有按预期工作的原因: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
.
我使用了 @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