如何在一个查询中遍历回到 gremlin 中的根顶点
How to traverse back to root vertex in gremlin in one single query
我在 amazon-neptune 中使用 Gremlin。我有顶点用户、国家、订单
我有从用户到国家的边'lives_in',从用户到订单的边'purchased',从订单到国家的边'delivered'
目标:找到向国外采购最多订单的最高国家,而不是按降序排列的 live_in 国家
gremlin> g.V().hasLabel("user").outE('purchased').inV().hasLabel("order").
......1> outE("delivered").inV().hasLabel("country").
......2> has('name').neq(outE('lives_in').inV().hasLabel("country").values()).
......3> groupCount().by(values)
我无法从步骤 neq(outE("lives_in")) 遍历回根顶点
删除最后一步后我得到了相同的结果
gremlin> g.V().hasLabel("user").outE('purchased').inV().hasLabel("order").
......1> outE("delivered").inV().hasLabel("country")
这意味着我的最后一步没有执行。
结果样本 - {v[country_GB]=38,v[country_NZ]=6,v[country_AU]=3}
在您的问题中包含一个像这样的小示例图总是有帮助的:
g.addV('user').as('u1').
addV('user').as('u2').
addV('order').as('o1').
addV('order').as('o2').
addV('order').as('o3').
addV('order').as('o4').
addV('order').as('o5').
addV('order').as('o6').
addV('country').property('name','usa').as('usa').
addV('country').property('name','candada').as('can').
addV('country').property('name','mexico').as('mex').
addE('lives_in').from('u1').to('usa').
addE('lives_in').from('u2').to('mex').
addE('purchased').from('u1').to('o1').
addE('purchased').from('u1').to('o2').
addE('purchased').from('u1').to('o3').
addE('purchased').from('u1').to('o4').
addE('purchased').from('u2').to('o5').
addE('purchased').from('u2').to('o6').
addE('delivered').from('o1').to('usa').
addE('delivered').from('o2').to('mex').
addE('delivered').from('o3').to('mex').
addE('delivered').from('o4').to('can').
addE('delivered').from('o5').to('mex').
addE('delivered').from('o6').to('can').iterate()
基于此,您可以采用以下一种方法:
gremlin> g.V().hasLabel("user").as('u').
......1> out('lives_in').hasLabel("country").as('c').
......2> select('u').
......3> out('purchased').hasLabel("order").
......4> out("delivered").hasLabel("country").
......5> where(neq('c')).
......6> groupCount().
......7> by('name')
==>[mexico:2,candada:2]
注意几点:
- 如果您不对边做任何操作,请将
inE().outV()
和 outE().inV()
分别简化为 in()
和 out()
。
- 在标记为 1 的行中,标记了 "lives_in" 个国家/地区顶点,以便稍后与第 5 行 "delivered" 个国家/地区进行比较
- 结果不包括标签为 "o1" 的订单和标签为 "o5" 的订单,因为这两个订单都源自其运送到的国家/地区。
我在 amazon-neptune 中使用 Gremlin。我有顶点用户、国家、订单
我有从用户到国家的边'lives_in',从用户到订单的边'purchased',从订单到国家的边'delivered'
目标:找到向国外采购最多订单的最高国家,而不是按降序排列的 live_in 国家
gremlin> g.V().hasLabel("user").outE('purchased').inV().hasLabel("order").
......1> outE("delivered").inV().hasLabel("country").
......2> has('name').neq(outE('lives_in').inV().hasLabel("country").values()).
......3> groupCount().by(values)
我无法从步骤 neq(outE("lives_in")) 遍历回根顶点
删除最后一步后我得到了相同的结果
gremlin> g.V().hasLabel("user").outE('purchased').inV().hasLabel("order").
......1> outE("delivered").inV().hasLabel("country")
这意味着我的最后一步没有执行。
结果样本 - {v[country_GB]=38,v[country_NZ]=6,v[country_AU]=3}
在您的问题中包含一个像这样的小示例图总是有帮助的:
g.addV('user').as('u1').
addV('user').as('u2').
addV('order').as('o1').
addV('order').as('o2').
addV('order').as('o3').
addV('order').as('o4').
addV('order').as('o5').
addV('order').as('o6').
addV('country').property('name','usa').as('usa').
addV('country').property('name','candada').as('can').
addV('country').property('name','mexico').as('mex').
addE('lives_in').from('u1').to('usa').
addE('lives_in').from('u2').to('mex').
addE('purchased').from('u1').to('o1').
addE('purchased').from('u1').to('o2').
addE('purchased').from('u1').to('o3').
addE('purchased').from('u1').to('o4').
addE('purchased').from('u2').to('o5').
addE('purchased').from('u2').to('o6').
addE('delivered').from('o1').to('usa').
addE('delivered').from('o2').to('mex').
addE('delivered').from('o3').to('mex').
addE('delivered').from('o4').to('can').
addE('delivered').from('o5').to('mex').
addE('delivered').from('o6').to('can').iterate()
基于此,您可以采用以下一种方法:
gremlin> g.V().hasLabel("user").as('u').
......1> out('lives_in').hasLabel("country").as('c').
......2> select('u').
......3> out('purchased').hasLabel("order").
......4> out("delivered").hasLabel("country").
......5> where(neq('c')).
......6> groupCount().
......7> by('name')
==>[mexico:2,candada:2]
注意几点:
- 如果您不对边做任何操作,请将
inE().outV()
和outE().inV()
分别简化为in()
和out()
。 - 在标记为 1 的行中,标记了 "lives_in" 个国家/地区顶点,以便稍后与第 5 行 "delivered" 个国家/地区进行比较
- 结果不包括标签为 "o1" 的订单和标签为 "o5" 的订单,因为这两个订单都源自其运送到的国家/地区。