gremlintinkerpoptinkerpop3gremlin-servertinkergraph

Tinkerpop Select neighbours grouped by the vertex they are neighbour with range step


I want to select all l labelled vertices along with their t labelled vertices grouped by their neighbours. Also I want to apply a limit on the length of the neighbours. For ex for neighbour limit = 2 something like below should be output.

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

For ex for neighbour limit = 1 something like below should be output.

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

grelify link 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')

Solution

  • 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.