How to sort by inV()
or outE()
from the edge?
I need to get the roles that are assigned to the user
.
Users can have multiple roles.
g.addV('project').as('p').
.addV('user').property(T.id, 'u1').as('u1')
.addV('user').property(T.id, 'u2').as('u2')
.addE('role').from('p').to('u1').property('role', 'role1')
.addE('role').from('p').to('u1').property('role', 'role2')
.addE('role').from('p').to('u1').property('role', 'role3')
.addE('role').from('p').to('u2').property('role', 'role1')
In, the above graph, the project has 2 users and user u1
has 3 roles and u2
has 1 role.
How to retrieve all edges/roles with range
but by the user.
g.V('p').outE().range(1, 2).inV().elementMap()
The above query returns few edges of the user, not all edges/roles belong to the user.
How to query it?
Firstly, I would make sure that you're not confusing labels and IDs when it comes to how you are defining user vertices. In the generated graph query that you provide, the addV()
step is setting the label for the vertices that you want to add. So instead of creating to user
vertices, you are creating two vertices with labels of u1
and u2
. Instead, you would want to do something like:
g.addV('project').property(id,'p1').as('p')
.addV('user').property(id,'u1').as('u1')
.addV('user').property(id,'u2').as('u2')
.addE('role').from('p').to('u1').property('role', 'role1')
.addE('role').from('p').to('u1').property('role', 'role2')
.addE('role').from('p').to('u1').property('role', 'role3')
.addE('role').from('p').to('u2').property('role', 'role1')
To fetch a mapping of user to role from a given project, you would start from the selected project vertex (p1
, in this case) and then traverse along the graph until you reached the user vertices. You would use as()
labels along the way to denote portions of the traversal that you want to output later. Using a select()
step, you can then build the output that you desire:
g.V('p1').outE('role').as('r').
inV().as('u').
select('u','r').
by(id()).
by('role')
This will provide the following output:
[{'u': 'u1', 'r': 'role1'},
{'u': 'u1', 'r': 'role2'},
{'u': 'u1', 'r': 'role3'},
{'u': 'u2', 'r': 'role1'}]