Apologies for not having sample graph.
I’ve a directed acyclic graph mapping where Multiple nodes called "greatgrandparents" have edges to "grandparents" that has edges to multiple "parents" with edges to multiple "children".
Something like,
greatgrandparent1 -> grandparent1 -> parent1 -> child1
greatgrandparent1 -> grandparent1 -> parent1-> child2
greatgrandparent1 -> grandparent1 -> parent2 -> child3
greatgrandparent1 -> grandparent1 -> parent2 -> child4
… So on …
I am starting from grandparent, walking up to connected greatgrandparent. Also walking down from grandparent to parent and then to connected children’s.
I need a query that will return something like below.
{‘greatgrandparent’: ‘greatgrandparent1’ , ‘parent’ : ‘parent1’ , ‘children’ : '['child1', 'child2', …..]'}
{‘greatgrandparent’: ‘greatgrandparent1’ , ‘parent’ : ‘parent2’ , ‘children’ : '['child3', 'child4', …..]'}
…..
….
So far I am able to get each child on separate line like this,
{‘greatgrandparent’: ‘greatgrandparent1’ , ‘grandparent’ : ‘grandparent1’, ‘parent’ : ‘parent1’ , ‘children’ : ‘child1’}
{‘greatgrandparent’: ‘greatgrandparent1’ ‘grandparent’ : ‘grandparent1’, ,‘parent’ : ‘parent1’ , ‘children’ : ‘child2’}
{‘greatgrandparent’: ‘greatgrandparent1’ ‘grandparent’ : ‘grandparent1’, ‘parent’ : ‘parent2’ , ‘children’ : ‘child3’}
{‘greatgrandparent’: ‘greatgrandparent1’ ‘grandparent’ : ‘grandparent1’, ‘parent’ : ‘parent2’ , ‘children’ : ‘child4}
..
How can I get all children in a list?
I am using amazon Neptune with gremlin_python.
g.V()
.has('grandparent', 'name', 'grandparent1’).as_(‘grandparent')
.repeat(timeLimit(1000).in()).until(hasLabel('greatgrandparent'))
.dedup().order().by('name').as_(‘greatgrandparent')
.select('grandparent').repeat(timeLimit(1000).out()).until(hasLabel(‘parent))
.dedup().order().by('name').as_(‘parent')
.repeat(timeLimit(1000).out()).until(hasLabel('children'))
.dedup().order().by('name').as_(‘children')
.select(‘greatgrandparent’,grandparent','parent', 'children').by(‘name’)
One way to approach this is to investigate use of
path
andgroup
. To simulate the relationships (g-grandparent, grandparent, parent, children) I'm going to use airports from the air-routes data set. First of all lets look at a simple use ofpath
starting fromZKE
which will be playing the role of our great grandparent.when run we get
In order to preserve this relationship, but fold the children into a list, we can add a
group..by
to the query.which gives us
Perhaps using these building blocks you can achieve the results you are looking for,