I'm trying to write a function to generate a Gremlin query. The input of the function is an array of array of strings, with the names of relationships we want to return from the graph. The graph contains information on TV and movies. So an example input would be:
[[seasons, episodes, talent], [studios, movies, images]]
The strings refer to edge names.
I need to return a JSON object containing the IDs for the vertices labelled by their edge names but I'm finding the Germlin query very difficult.
So far I've managed to write this query:
g.V('network_1').out().where(__.inE().
hasLabel('seasons')).
group().
by(__.inE().label()).
by(__.group().by(T.id).
by(__.out().where(__.inE().
hasLabel('episodes')).
group().
by(__.inE().label()).
by(__.group().by(T.id).
by(__.out().where(__.inE().
hasLabel('talent')).
group().
by(__.inE().label()).by(T.id))))).
next()
Which gives this output:
{
"seasons": {
"season_2": {
"episodes": {
"episode_4": {
"talent": [
"talent_8",
"talent_6",
"talent_7"
]
}
}
},
"season_1": {
"episodes": {
"episode_2": {
"talent": [
"talent_2",
"talent_3"
]
},
"episode_3": {
"talent": [
"talent_4",
"talent_5"
]
},
"episode_1": {
"talent": [
"talent_1"
]
}
}
}
}
}
That output is exactly the kind of thing I'm looking for however the problems are:
- That query seems hugely over complicated
- The array of edges to query could be any size. In my example its 3, but it could be anything.
- In the example there are 2 arrays of edges to query, which ideally I could combine into one query
I'm writing this in Python, and would be hugely appreciative of any help or pointers.
Example content:
g.addV('show').property('id', 'show_1').as('show_1').
addV('season').property('id', 'season_1').as('season_1').
addV('season').property('id', 'season_2').as('season_2').
addV('episode').property('id', 'episode_1').as('episode_1').
addV('episode').property('id', 'episode_2').as('episode_2').
addV('episode').property('id', 'episode_3').as('episode_3').
addV('episode').property('id', 'episode_4').as('episode_4').
addV('talent').property('id', 'talent_1').as('talent_1').
addV('talent').property('id', 'talent_2').as('talent_2').
addV('talent').property('id', 'talent_3').as('talent_3').
addV('talent').property('id', 'talent_4').as('talent_4').
addV('talent').property('id', 'talent_5').as('talent_5').
addV('talent').property('id', 'talent_6').as('talent_6').
addV('talent').property('id', 'talent_7').as('talent_7').
addV('talent').property('id', 'talent_8').as('talent_8').
addE('seasons').from('show_1').to('season_1').
addE('seasons').from('show_1').to('season_2').
addE('episodes').from('season_1').to('episode_1').
addE('episodes').from('season_1').to('episode_2').
addE('episodes').from('season_1').to('episode_3').
addE('episodes').from('season_2').to('episode_4').
addE('talent').from('episode_1').to('talent_1').
addE('talent').from('episode_2').to('talent_2').
addE('talent').from('episode_2').to('talent_3').
addE('talent').from('episode_3').to('talent_4').
addE('talent').from('episode_3').to('talent_5').
addE('talent').from('episode_4').to('talent_6').
addE('talent').from('episode_4').to('talent_7').
addE('talent').from('episode_4').to('talent_8').iterate()