There are multiple ways to write the query.
Option 1: Start from console
g.V().has('console', 'ps3').in('uses').where(out('plays').has('game', 'warcraft')).valueMap('name')
Let me explain the structure here:
g.V().has('console', 'ps3')
--> Select all vertices which have a property with key as console
and value as ps3
in('uses')
--> From the set of previous vertices, jump to incoming vertices via an edge that has the label uses
. At this stage, we would have player vertices in our solution.
where(out('plays').has('game', 'warcraft'))
--> Apply a filter on existing solutions. Since we are using where
we would not jump/traverse to the next step of vertices.
valueMap('name')
--> Project one or more properties if existing solutions which are player vertices.
Option 2: Another way to write above query
g.V().has('console', 'ps3').in('uses').as('myusers').out('plays').has('game', 'warcraft').select('myusers').by('name')
as('myusers')
--> Provides a reference/alias to the vertices at this stage. Note that it does not store all the results at this stage instead it just provides a reference to the type of vertices at this point in the query.
out('plays').has('game', 'warcraft')
--> Unlike previous time when we did not jump since we were using where
, this time we jump onto the game
vertices.
select('myusers').by('name')
--> since we want to project the users but the current solutions are game vertices, we need to select the user vertices which we do using the reference we stored earlier.
Option 3: Start from user
g.V().hasLabel('user').where(out('plays').has('game','warcraft')).where(out('uses').has('console','ps3')).valueMap('name')
There are more ways to write this query such as using path()
but I won't go into details here.
Since you are beginning to learn Gremlin, I would recommend that you start with https://kelvinlawrence.net/book/Gremlin-Graph-Guide.html