Graph/Gremlinpython:从一个顶点向上插入两个顶点和一条边

Graph/Gremlinpython:从一个顶点向上插入两个顶点和一条边,graph,gremlin,tinkerpop,amazon-neptune,gremlinpython,Graph,Gremlin,Tinkerpop,Amazon Neptune,Gremlinpython,我尝试添加(如果它们不存在)两个顶点,并在单个查询中添加从第一个顶点到另一个顶点的边。 它看起来像: g.V().has("label1", "property1", "value1").fold().coalesce( __.unfold(), g.addV("label1").property("property1", "value1") ).as_("a").V().has("label2", "property2", "value2").fold(

我尝试添加(如果它们不存在)两个顶点,并在单个查询中添加从第一个顶点到另一个顶点的边。 它看起来像:

g.V().has("label1", "property1", "value1").fold().coalesce(
        __.unfold(),
        g.addV("label1").property("property1", "value1")
    ).as_("a").V().has("label2", "property2", "value2").fold().coalesce(
        __.unfold(),
        g.addV("label2").property("property2", value2)
    ).coalesce(
        __.inE("link").where(__.outV().as_("a")),
        __.addE("link").from_("a")
    ).next()
但是由于
fold()
起到了屏障的作用,它删除了我在第一个顶点上放置的标记“a”。 我的解决办法如下:

g.V().has("label1", "property1", "value1").fold().coalesce(
        __.unfold(),
        g.addV("label1").property("property1", "value1")
    ).as_("a").V().has("label2", "property2", "value2").fold().coalesce(
        __.unfold(),
        g.addV("label2").property("property2", value2)
    ).coalesce(
        __.inE("link").where(__.outV().has("label1", "property1", value1)),
        __.addE("link").from_(__.V().has("label1", "property1", value1))
    ).next()
我能做得更好吗?这是。。。丑陋


好的,
商店
解决方案有效:) 但我对上次合并的第一部分有问题。 我这样做:

g.V().has("label1", "property1", "value1").fold().coalesce(
    __.unfold(),
    g.addV("label1").property("property1", "value1")
).as_("a").V().has("label2", "property2", "value2").fold().coalesce(
    __.unfold(),
    g.addV("label2").property("property2", value2)
).coalesce(
    __.inE("link").where(__.outV().where(within("a"))),
    __.addE("link").from_(select("a").unfold())
).next()

多亏了Kelvin Lawrence:)

也许使用
存储
而不是
作为
可以满足您的需求:

下面是一个使用Gremlin控制台的虚构示例

gremlin> g.V().hasLabel('notyet').
......1>       fold().
......2>       coalesce(unfold(),addV('notyet')).
......3>       store('a').
......4>       V().
......5>       hasLabel('notyet2').
......6>       fold().
......7>       coalesce(unfold(),addV('notyet2')).
......8>       coalesce(__.in('link').where(within('a')),
                        addE('link').to(select('a').unfold())) 
==>e[61330][61329-link->61327]  

存储
部分工作,但合并的第一部分不工作。它会创建重复的边。你知道为什么吗?你能更新你的问题,包括一些构建样本图的
addV
addE
步骤吗?这将有助于给你最好的答案。在运行此查询之前,是否可能存在多个满足合并步骤条件的顶点?下面是一个创建测试图的简单方法的示例:我能够在我的上下文中找到一些有用的东西,谢谢你:D