0

I Need to return some groups and people in that group, like this:

Group A
-----Person A
-----Person B
-----Person C

Group B
-----Person D
-----Person E
-----Person F

How can I do that with gremlin. They are connected to group with a edge.

Augusto Will
  • 562
  • 7
  • 20

1 Answers1

6

It is always helpful to include a sample graph with your questions on Gremlin preferably as a something easily pasted to the Gremlin Console as follows:

g.addV('group').property('name','Group A').as('ga').
  addV('group').property('name','Group B').as('gb').
  addV('person').property('name','Person A').as('pa').
  addV('person').property('name','Person B').as('pb').
  addV('person').property('name','Person C').as('pc').
  addV('person').property('name','Person D').as('pd').
  addV('person').property('name','Person E').as('pe').
  addV('person').property('name','Person F').as('pf').
  addE('contains').from('ga').to('pa').
  addE('contains').from('ga').to('pb').
  addE('contains').from('ga').to('pc').
  addE('contains').from('gb').to('pd').
  addE('contains').from('gb').to('pe').
  addE('contains').from('gb').to('pf').iterate()

A solution to your problem is to use group() step:

gremlin> g.V().has('group', 'name', within('Group A','Group B')).
......1>   group().
......2>     by('name').
......3>     by(out('contains').values('name').fold())
==>[Group B:[Person D,Person E,Person F],Group A:[Person A,Person B,Person C]]
stephen mallette
  • 45,298
  • 5
  • 67
  • 135
  • stephen, in this case, is possible to return all the groups, without the "within('Group A','Group B')" and a valueMap of all groups and users? But in the same way, with users nested inside groups. – Augusto Will Oct 11 '17 at 19:58
  • Is possible to retrieve that vertex "groups" with them valueMaps and nest to them all the vertex with edge to that group, with all the ValueMaps of these vertex too? – Augusto Will Oct 11 '17 at 23:25
  • 1
    why not try and see? :) If your graph is small enough then you could replace the `has()` using `within()` with a simple `hasLabel("group")`. And with a small or large graph, sure, you could replace `values('name')` with `valueMap()`. Just note that if you use a GLV like gremlin-python in 3.2.x, it won't be able to serialize a result like that properly because the key in the returned `Map` won't be a string. You would need to use 3.3.x which included GraphSON 3.0 and supports non-string keys. – stephen mallette Oct 12 '17 at 11:16