I am trying to create a graph where i can find all relationships with given person and list of their friends.
the problem statement my graph looks like this
bob ->friends -> jay
jay -> friends -> mack
jay -> friends - > john
mack -> friends -> trevor
given query where my original vertex is bob how can i show relation of
bob->jay->mack
bob->jay->john
if i do 3 hops it shold include relationship with mack-> trevor in result set
this is what i have this is the query i have thus far but it only produces resutls that are directly connected to bob and does not go out on otherV() as base selector and run 2 hops from connectedNode
results = g.V().has('person', 'bob').as('node').
union(
bothE().hasLabel('friend').as('edge').otherV().has('person').as('connectedNode'),
both().hasLabel('friend').dedup().as('connectedNode')
).
project('node', 'edge', 'connectedNode').
by(select('node').valueMap().unfold().dedup().fold()).
by(select('edge').valueMap().unfold().dedup().fold()).
by(select('connectedNode').valueMap().unfold().dedup().fold()).
toList()
what should be the correct way to get the results?
When asking questions about Gremlin it is best to include some sample data as a Gremlin script:
I think that the basics of this query are best accomplished with
path():I suppose that you have a situation where you might traverse in either direction on the "friends" edge, in which case you would probably want to use
simplePath()to filter out cycles:Perhaps an interesting case to consider is if you traverse from "jay":
In this case we can see that we don't get the "[jay,john]" path because traversing from "john" has no edges that have not already been traversed so that path is filtered. We can work around that issue with
repeat():The key is the
emit(__.not(outE()))where therepeat()will only produce vertices that have no further edges to traverse.