Let's assume we have a graph of persons, with properties
name
(String)age
(int)
... and friend
edges in between some of them. Let's further assume we have a complex gremlin query which eventually produces person vertices.
Now, for each of the resulting persons, we want to find the three oldest friends in a subquery.
This is the rough query structure:
g.traversal.V()
// complex filtering and navigation here, produces person vertices
.flatMap(
__.out(friend)
.order().by("age", desc)
.limit(3)
)
// complex query continues with the three oldest friends per input vertex here
The problem with this query is that the order().by(...)
step is global. I get three persons in total for all person vertices which enter the flatMap(...)
.
What I would like to receive is (up to) three persons per vertex entering the flatMap(...) step.
How would you do that in gremlin? I've read that there is "local" scoping of sorts, but I was unable to make it work.