【问题标题】:Get edge properties as well as source and target vertex ID in Gremlin query language在 Gremlin 查询语言中获取边属性以及源和目标顶点 ID
【发布时间】:2018-09-14 15:00:40
【问题描述】:

我正在尝试检索边缘属性作为值以及目标和源节点 ID。

我当前的数据库是这样的:

边缘:

_id _label _outV _inV name ID
0   edge   0     1    E    0

节点:

_id _label _name ID
0   node   A     0
1   node   B     1

我试过这个查询:

>g.V().as('a').outE('edge').as('b').inV().values('ID').as('to').
 select('b').valueMap().as('edge').
 select('a').values('ID').as('from').
 select('to','edge','from')
==>[to:0,edge:[ID:0,name:E],from:1]

我想要得到的是

[to:0,ID:0,name:E,from:1]

Edge 元素也可以包含任意数量的属性。

有没有办法做到这一点?

谢谢!

编辑: 最终查询:

gremlin> g.V().outE('edge').limit(1).
......1>   project('weight','id','from','to').
......2>     by(coalesce(values('weight'),constant(''))).
......3>     by(id).
......4>     by(outV().id()).
......5>     by(inV().id())
==>[weight:,id:0,from:0,to:1]

【问题讨论】:

    标签: gremlin tinkerpop tinkerpop3


    【解决方案1】:

    使用project():

    gremlin> g.V().has('name','marko').
    ......1>   outE().limit(1).
    ......2>   project('weight','id','from','to').
    ......3>     by('weight').
    ......4>     by(id).
    ......5>     by(outV().id()).
    ......6>     by(inV().id())
    ==>[weight:0.4,id:9,from:1,to:3]
    

    【讨论】:

    • 谢谢!这就是我一直在寻找的。​​span>
    • 这个查询有一个小问题,如果属性'weight'为空,我无法得到结果。有没有解决办法。我正在考虑使用 choose() 进行有无投影。
    • 只需在by() 中使用coalesce() 来表示“重量”——而不是by('weight') 使用by(coalesce(values('weight'),constant(0.0)))
    • 谢谢斯蒂芬!这是完美的工作!我只是用 '' 更改了 0.0 值,因为 0.0 是一个有效值。我已经用最终查询更新了我的帖子。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-06
    • 2019-06-24
    • 2017-10-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-08-24
    相关资源
    最近更新 更多