首页 > 解决方案 > 使用 Gremlin 查询递归获取节点

问题描述

我想定义一个 Gremlin 查询,它递归地返回所有节点,直到没有更多可用节点:

Node: ProductionEvent
Node: Product

到目前为止我尝试过的是以下内容......但是,我不确定输出是否正确。另外,是否可以使用 tree() 函数将其打印出来?我可以使用times函数执行repeat函数来获取所有节点吗?

g.V().hasLabel('ProductionEvent').
                repeat(__.outE('consumes').simplePath()).times(3). 
                emit().dedup()  

我期待这样的输出

Product1: consumed <--- ProductionEvent1 --> produced :Product2: consumed <--- ProductionEvent2 --> produced :Product3

标签: gremlinrecursive-queryjanusgraphamazon-neptune

解决方案


我取得了令我满意的结果。查询是

 g.V().hasLabel('ProductionEvent').has('orderID', 'XYZ')
      .repeat(__.out('consumes').in_('produces').simplePath()).emit().times(5)
      .dedup().limit(6).next()

据我了解,如果事先知道结果的数量,那么为了提高查询执行时间, limit()and是必要的。times()并且simplepath()确保不会再次访问已经访问过的路径。


推荐阅读