Gremlin:根据一行代码中的两个计数计算分配



我有两个计数,计算如下:

1)g.v()。haslabel('brand')。其中(__。ine('client_brand')。count()。is(gt(gt(0)))。

2)g.v()。haslabel('brand')。count()

,我想获得一行代码,从而导致第一个计数除以第二个。

这是一种方法:

g.V().hasLabel('brand').
  fold().as('a','b').
  math('a/b').
    by(unfold().where(inE('client_brand')).count())
    by(unfold().count())

请注意,我仅将第一个遍历简化为.where(inE('client_brand')).count(),因为您只想计算至少有一个边缘,无需全部计算它们并进行比较。

您也可以union()喜欢:

g.V().hasLabel('brand').
  union(where(inE('client_brand')).count(),
        count())
  fold().as('a','b').
  math('a/b').
    by(limit(local,1))
    by(tail(local))

虽然第一个更容易阅读/关注,但我猜第二个是更好的我想记忆密集型。

丹尼尔·库皮茨(Daniel Kuppitz)提供的另一种方式以一种有趣的方式使用groupCount()

g.V().hasLabel('brand').
  groupCount().
    by(choose(inE('client_brand'),
                constant('a'),
                constant('b'))).
  math('a/(a+b)')

使用sack()步骤的以下解决方案显示了为什么我们有math()步骤:

g.V().hasLabel('brand').
  groupCount().
    by(choose(inE('client_brand'),
                constant('a'),
                constant('b'))).
  sack(assign).
    by(coalesce(select('a'), constant(0))).
  sack(mult).
    by(constant(1.0)). /* we need a double */
  sack(div).
    by(select(values).sum(local)).
  sack()

如果您可以使用lambdas,则:

g.V().hasLabel('brand').
  union(where(inE('client_brand')).count(),
        count())
  fold().
  map{ it.get()[0]/it.get()[1]} 

这对我有用:

g.V().limit(1).project('client_brand_count','total_brands')
.by(g.V().hasLabel('brand')
.where(__.inE('client_brand').count().is(gt(0))).count())
.by(g.V().hasLabel('brand').count())
.map{it.get().values()[0] / it.get().values()[1]}
.project('brand_client_pct')

最新更新