繁体   English   中英

如果在 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小组的 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

我使用了@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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM