首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >图/Gremlinpython:向上插入两个顶点和一个从一个到另一个的边

图/Gremlinpython:向上插入两个顶点和一个从一个到另一个的边
EN

Stack Overflow用户
提问于 2020-06-15 00:08:31
回答 1查看 285关注 0票数 1

我尝试在一个查询中添加(如果不存在)2个顶点,并从第一个到另一个添加一个边。看起来应该是:

代码语言:javascript
复制
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“。我的解决办法如下:

代码语言:javascript
复制
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()

我能做得更好吗?这是..。丑?

好的,store解决方案起作用了:)但是我在最后一部分的合并中遇到了问题。我做了这个:

代码语言:javascript
复制
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()

谢谢凯尔文·劳伦斯:)

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-06-15 00:45:25

也许使用store而不是as可以满足您的需要:

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

代码语言:javascript
复制
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]  
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/62379583

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档