How to select optional graph structures with Gremlin?

三世轮回 提交于 2019-11-30 19:32:55

问题


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?


回答1:


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!