TinkerPop Gremlin, how get child elements filtered and grouped

Viewed 64

I'm new to using Gremlin and I need help to set the best query to select unique and filtered results.

Starting from a team I would get player (note: each player can play for more than one team) of each team connected by is_friends_with

The result (I would like to get)

[
 {
   "Player": "Icardi",
   "Teams": ["Valladolid"]
 },
 {
   "Player": "Kroll",
   "Teams": ["Valladolid"]
 },

  {
   "Player": "Baggio",
   "Teams": ["Eagles"]
 },

 {
   "Player": "Papin",
   "Teams": ["Valladolid","Eagls"]
 },
]

The graph

enter image description here

The schema:

 g.addV('team').as('1').
 property(single, 'name', 'Eagles').
 addV('player').as('2').
 property(single, 'name', 'Zico').addV('team').
 as('3').
 property(single, 'name', 'team A').
 addV('team').as('4').
 property(single, 'name', 'Horses').
 addV('player').as('5').
 property(single, 'name', 'Papin').
 addV('player').as('6').
 property(single, 'name', 'Ronaldo').
 addV('player').as('7').
 property(single, 'name', 'Visco').
 addV('player').as('8').
 property(single, 'name', 'Baggio').
 addV('tournament').as('9').
 addV('team').as('10').
 property(single, 'name', 'Valladolid').
 addV('player').as('11').
 property(single, 'name', 'Kroll').
 addV('player').as('12').
 property(single, 'name', 'Icardi').
 addE('owned').from('1').to('5').addE('owned').
 from('1').to('6').addE('owned').from('1').
 to('8').addE('owned').from('3').to('6').
 addE('owned').from('3').to('7').
 addE('created').from('3').to('9').
 addE('is_friends_with').from('3').to('10').
 addE('is_friends_with').from('3').to('1').
 addE('owned').from('4').to('8').addE('owned').
 from('4').to('2').addE('owned').from('4').
 to('5').addE('owned').from('4').to('7').
 addE('invited').from('9').to('1').
 addE('invited').from('9').to('4').
 addE('owned').from('10').to('11').
 addE('owned').from('10').to('12').
 addE('owned').from('10').to('5')
2 Answers

Here is one way to do it using group

gremlin>  g.V().
......1>    has('name','team A').
......2>    out('is_friends_with').as('a').
......3>    out('owned').
......4>    group().
......5>      by('name').
......6>      by(select('a').values('name').fold()).
......7>    unfold()

==>Papin=[Valladolid, Eagles]
==>Icardi=[Valladolid]
==>Baggio=[Eagles]
==>Ronaldo=[Eagles]
==>Kroll=[Valladolid]  

To get the exact format that matches your JSON, we can just add aproject step to the query.

gremlin>  g.V().
......1>    has('name','team A').
......2>    out('is_friends_with').as('a').
......3>    out('owned').
......4>    group().
......5>      by('name').
......6>      by(select('a').values('name').fold()).
......7>    unfold().
......8>    project('player','teams').
......9>      by(keys).
.....10>      by(values)

==>[player:Papin,teams:[Valladolid,Eagles]]
==>[player:Icardi,teams:[Valladolid]]
==>[player:Baggio,teams:[Eagles]]
==>[player:Ronaldo,teams:[Eagles]]
==>[player:Kroll,teams:[Valladolid]] 

gremlin> g.V().has("name", "team A").sideEffect(__.out("owned").hasLabel("player").aggregate("my_player").limit(1)).both("is_friends_with").hasLabel("team").as("team2").out("owned").hasLabel("player").as("friends_player").where(without("my_player")).as("friends_player2").select("team2", "friends_player2").group().by(select("friends_player2")).by(select("team2").fold()).unfold().project("Player", "Teams").by(select(keys).values("name")).by(select(values).unfold().values("name").fold())

==>[Player:Baggio,Teams:[Eagles]]
==>[Player:Kroll,Teams:[Valladolid]]
==>[Player:Icardi,Teams:[Valladolid]]
==>[Player:Papin,Teams:[Valladolid,Eagles]]
Related