【问题标题】:AWS Neptune find all ancestors for a nodeAWS Neptune 查找节点的所有祖先
【发布时间】:2021-07-01 17:48:12
【问题描述】:

我使用 Jupyter notebook 将以下顶点和边插入到 Neptune 数据库中。

%%gremlin

g.addV('my').property(T.id, '1').next()
g.addV('my').property(T.id, '2').next()
g.addV('my').property(T.id, '3').next()
g.addV('my').property(T.id, '4').next()
g.addV('my').property(T.id, '5').next()
g.addV('my').property(T.id, '6').next()
g.addV('my').property(T.id, '7').next()
g.addV('my').property(T.id, '8').next()

g.V('1').addE('parent').to(g.V('2')).next()
g.V('2').addE('parent').to(g.V('3')).next()
g.V('3').addE('parent').to(g.V('4')).next()
g.V('4').addE('parent').to(g.V('5')).next()

g.V('1').addE('parent').to(g.V('6')).next()
g.V('6').addE('parent').to(g.V('7')).next()
g.V('7').addE('parent').to(g.V('8')).next()

然后我使用以下查询来查找节点“1”的所有祖先。但是,它只返回节点“5”和注释“8”,以及最高级别的祖先。

如何修改查询以获取所有中间祖先,例如“2”、“3”、“4”、“5”、“6”、“8”

%%gremlin -p v,oute,inv
g.V('1').repeat(out('parent')).until(outE('parent').count().is(0)).toList()

【问题讨论】:

  • 看起来这样可以解决问题:g.V('1').repeat(out('parent')).emit().until(outE('parent').count().is(0)).toList()

标签: amazon-web-services gremlin amazon-neptune


【解决方案1】:

您需要做的就是在repeat() 之后添加emit() step 或将path() step 添加到这样的查询末尾,具体取决于您是否需要顶点或路径返回:

g.V().hasLabel('a').repeat(out('parent')).until(outE('parent').count().is(0)).emit()

g.V().hasLabel('a').repeat(out('parent')).until(outE('parent').count().is(0)).path()

【讨论】:

    猜你喜欢
    • 2021-09-13
    • 2022-11-17
    • 1970-01-01
    • 2019-06-17
    • 1970-01-01
    • 2012-04-22
    • 1970-01-01
    • 1970-01-01
    • 2013-05-19
    相关资源
    最近更新 更多