gremlintinkerpoptinkerpop3gremlin-servertinkergraph

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


I want to select all l labelled vertices along with their t,n 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]}, {[n1]}},
{"l3",{[]}, {[n2,n3]}}
]

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

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

grelify link https://gremlify.com/xun4v83y54/2

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('neighbour1').as('7').property(single, 'name', 'n1').property(single, 'label', 'n').
  addV('neighbour2').as('8').property(single, 'name', 'n2').property(single, 'label', 'n').
  addV('neighbour3').as('9').property(single, 'name', 'n3').property(single, 'label', 'n').
  addE('connected').from('1').to('4').
  addE('connected').from('1').to('5').
  addE('connected').from('2').to('6').
  addE('connected').from('2').to('7').
  addE('connected').from('3').to('8')
  addE('connected').from('3').to('9')

Solution

  • For this output you can try doing something like this:

    g.V().hasLabel('l').
      group().by('name').
        by(out('connected').fold()
          project('t', 'n').
            by(unfold().hasLabel('t').limit(1).values('name').fold()).
            by(unfold().hasLabel('n').limit(1).values('name').fold())
            .unfold().select(values).fold())
    

    You can change the limit to be the max number of neighbors you want from each type.

    example: https://gremlify.com/ecw6j2ajh5p