I am using Gremlin to query a graph stored in TitanDB.
The graph contains user vertices with properties, e.g., "description", and edges denoting relationships between users.
I want to use Gremlin to obtain 1) users by properties and 2) possible relationships to other users. I can use, for example, the following query to obtain all users whose description contains the word 'developer' and the edges with label 'relationship' originating from or targeting these users:
g.V('description',CONTAINS,'developer').as('user').bothE.as('relationship').select
So far, so good. The problem is, however, that some users do not (yet) have any relationships. The above query will neglect these users (despite their description containing 'developer') and will only return users that do have at least one relationship.
Is there a way to select ALL users whose description contains 'developer', and optionally their relationships in addition if they exist?
You could do:
g.V('description',CONTAINS,'developer').as('user').transform{it.bothE.toList()}.as('relationship').select
in this way, you should get an empty list for those developers who don't have edges.
In TinkerPop 3.x, using the TinkerPop modern graph where I dropped edge with id 12, you would do:
gremlin> g.E(12).drop()
gremlin> g.V().hasLabel('person').as('u').
......1> map(bothE().fold()).as('r').
......2> select('u','r')
==>[u:v[1],r:[e[9][1-created->3],e[7][1-knows->2],e[8][1-knows->4]]]
==>[u:v[2],r:[e[7][1-knows->2]]]
==>[u:v[4],r:[e[10][4-created->5],e[11][4-created->3],e[8][1-knows->4]]]
==>[u:v[6],r:[]]
来源:https://stackoverflow.com/questions/27704043/how-to-select-optional-graph-structures-with-gremlin