0

我想选择所有l标记的顶点以及t按其邻居分组的标记顶点。我还想对邻居的长度施加限制。对于 ex for neighbor limit = 2,应该输出如下内容。

[
{"l1",[t1,t2]},
{"l2",[t3]},
{"l3",[]}
]

对于 ex for neighbor limit = 1,应该输出如下内容。

[
{"l1",[t1]},
{"l2",[t3]},
{"l3",[]}
]

grelify 链接https://gremlify.com/xun4v83y54/1

g.addV('Vertex').as('1').property(single, 'name', 'l1').property(single, 'label', 'l').
  addV('Vertex').as('2').property(single, 'name', 'l2').property(single, 'label', 'l').
  addV('Vertex').as('3').property(single, 'name', 'l3').property(single, 'label', 'l').
  addV('Tag').as('4').property(single, 'name', 't1').property(single, 'label', 't').
  addV('Tag').as('5').property(single, 'name', 't2').property(single, 'label', 't').
  addV('Tag').as('6').property(single, 'name', 't3').property(single, 'label', 't').
  addE('connected').from('1').to('4').
  addE('connected').from('1').to('5').
  addE('connected').from('2').to('6')
4

1 回答 1

1

I think group() would work well here:

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]]

Note that the second by() modulator is a reducing operation for the collected items you grouped. In there you may manipulate that collection further as needed. To demonstrated I modified your data a bit:

g.addV('Vertex').as('1').property(single, 'name', 'l1').property(single, 'label', 'l').
  addV('Vertex').as('2').property(single, 'name', 'l2').property(single, 'label', 'l').
  addV('Vertex').as('3').property(single, 'name', 'l3').property(single, 'label', 'l').
  addV('Tag').as('4').property(single, 'name', 't1').property(single, 'label', 't').
  addV('Tag').as('5').property(single, 'name', 't2').property(single, 'label', 't').
  addV('Tag').as('6').property(single, 'name', 't3').property(single, 'label', 't').
  addV('Tag').as('7').property(single, 'name', 't4').property(single, 'label', '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')

In the following case I just used union() to create two lists - one of each "type":

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:[[],[]]]

but you could use other collection manipulation techniques to get it into other forms of course.

于 2020-08-25T10:30:49.207 回答