问题描述
我想选择所有l
标记的顶点以及它们的t
标记的顶点(按邻居分组)。我也想对邻居的长度加以限制。
对于ex为邻居限制= 2,应输出类似以下内容。
[
{"l1",[t1,t2]},{"l2",[t3]},{"l3",[]}
]
[
{"l1",[t1]},[]}
]
关联链接https://gremlify.com/xun4v83y54/1
g.addV('Vertex').as('1').property(single,'name','l1').property(single,'label','l').
addV('Vertex').as('2').property(single,'l2').property(single,'l').
addV('Vertex').as('3').property(single,'l3').property(single,'l').
addV('Tag').as('4').property(single,'t1').property(single,'t').
addV('Tag').as('5').property(single,'t2').property(single,'t').
addV('Tag').as('6').property(single,'t3').property(single,'t').
addE('connected').from('1').to('4').
addE('connected').from('1').to('5').
addE('connected').from('2').to('6')
解决方法
我认为group()
在这里可以很好地工作:
gremlin> g.V().has('label','l').
......1> group().
......2> by('name').
......3> by(out().limit(2).values('name').fold())
==>[l1:[t1,t2],l2:[t3]]
gremlin> g.V().has('label','l').
......1> group().
......2> by('name').
......3> by(out().limit(1).values('name').fold())
==>[l1:[t1],l2:[t3]]
请注意,第二个by()
调制器是对您分组的收集项进行的归约运算。在那儿,您可以根据需要进一步操作该集合。为了证明这一点,我对您的数据做了一些修改:
g.addV('Vertex').as('1').property(single,'name','l1').property(single,'label','l').
addV('Vertex').as('2').property(single,'l2').property(single,'l').
addV('Vertex').as('3').property(single,'l3').property(single,'l').
addV('Tag').as('4').property(single,'t1').property(single,'t').
addV('Tag').as('5').property(single,'t2').property(single,'t').
addV('Tag').as('6').property(single,'t3').property(single,'t').
addV('Tag').as('7').property(single,'t4').property(single,'t').
addE('connected').from('1').to('4').
addE('connected').from('1').to('5').
addE('connected').from('2').to('6').
addE('next').from('2').to('7')
在以下情况下,我仅使用union()
创建两个列表-每个“类型”之一:
gremlin> g.V().has('label','l').
......1> group().
......2> by('name').
......3> by(union(out("connected").limit(1).values('name').fold(),......4> out("next").limit(1).values('name').fold()).
......5> fold())
==>[l1:[[t1],[]],l2:[[t3],[t4]],l3:[[],[]]]
但是您可以使用其他collection manipulation techniques将其用于其他形式的课程。