首页 > 解决方案 > gremlin - 使用不同的 where 子句将列表与单个列表分开

问题描述

如果我有这个小图:

addV('location').property('name','loc 1').property('active',true).property('size',2000).as('a')
addV('location').property('name','loc 2').property('active',true).property('size',1200).as('b')
addV('location').property('name','loc 3').property('active',true).property('size',1800).as('c')
addV('location').property('name','loc 4').property('active',true).property('size',2400).as('d')
addV('location').property('name','loc 5').property('active',true).property('size',2800).as('e')
addV('location').property('name','loc 6').property('active',true).property('size',4200).as('f')
addV('owner').property('name','john doe').as('o')

addV('building').property('name','building').property('active',true).as('building')
addE('on-property').from('a').to('building')

addE('owns').from('o').to('a')
addE('owns').from('o').to('b')
addE('owns').from('o').to('c')
addE('owns').from('o').to('d')
addE('owns').from('o').to('e')
addE('owns').from('o').to('f')

如果我从这样的基本查询开始:

//if 1 is the id of "john doe"
g.V(1).out('owns').has('active',true)

我想做额外的 where 子句,但将它们分开,因此使用或放入单个列表将不起作用。

另一个考虑因素是,这是真实图表的一小部分,所以我真的不想多次循环遍历它们......我认为一个项目会循环遍历每个投影的 1000 多个位置,而不是做一次循环

 g.V(1)
      .project('sizeLimit','withBuildings')
         .by(out('my-locations').where(and(has('active',true),has('size',lt(2000)))))
         .by(out('my-locations').and(
               coalesce(
                  out('on-property').count(local),
                  constant(0)).is(gt(0))
               ,has('active',true)))

我尝试先执行查询的相关部分,而不是必须在每个部分中执行“输出”,例如:

 g.V(1).out('my-locations')
        .has('active',true)
      .project('sizeLimit','withBuildings')
         .by(has('size',lt(2000)))
         .by(
               coalesce(
                  out('on-property').count(local),
                  constant(0)).is(gt(0))
               )

但是投影没有正确进行,最终每次投影只做一个项目——即使我做了折叠或展开

所以我想知道是否将第一种方法放在 by 而不是 before 是唯一的选择,或者我是否可以聚合或存储列表,然后在哪里关闭聚合或以某种方式循环并推送到单独的列表?

我正在使用 azure cosmos graph db。

最终的外观有望像这样的 json:

"sizeLimit":[
  locationVertex,
  locationVertex,
  locationVertex
],
"withBuildings":[
  locationVertex,
  locationVertex
]

“locationVertex”是过滤器为项目集返回的顶点。

标签: azure-cosmosdbgremlin

解决方案


我不确定我是否完全遵循您的问题,但听起来您最感兴趣的是保留调制器,因为对于您添加的每一个out('owns'),您都将重新遍历图表的相同部分。您与第二个遍历示例非常接近(尽管您说它工作不正常)-经过一些小的修改,它似乎对我来说表现得很好:by()by()

gremlin> g.V().has('name','john doe').
......1>   out('owns').has('active',true).
......2>   fold().
......3>   project('sizeLimit','withBuildings').
......4>     by(unfold().has('size',lt(2000)).fold()).
......5>     by(unfold().filter(out('on-property').count().is(gt(0))).fold())
==>[sizeLimit:[v[8],v[4]],withBuildings:[v[0]]]

我认为您不需要,coalesce()因为count()当没有outE()边缘时会将流减少到零。


推荐阅读