Gremlin 3 - 获取所有传入和传出顶点,包括它们的边和方向,包括没有边的顶点

Gremlin 3 - get all incoming and outgoing vertices, including their edges and directions and including vertices without edges

我正在尝试编写一个查询来获取所有传入和传出顶点,包括它们的边和方向,但是 return 那些没有边的顶点也是如此。

我现在可以通过强制所有东西至少有一个边来解决这个问题,但这是我想避免的事情。

也许值得注意的是我使用了 Azure CosmosDB 的图 API:https://docs.microsoft.com/en-us/azure/cosmos-db/gremlin-support

这是我用于 return 所有顶点及其边和相关顶点的查询:

g.V().hasLabel('User').as('User').bothE().as('Edge').otherV().as('RelatedObject').path()

我从:

此查询生成的结果我稍后可以在 C# 应用程序中轻松解析,但是此查询没有 return 没有边的顶点。

有什么想法吗?

编辑

我得到的最接近的是:

g.V().hasLabel("User").as("User").map(bothE().otherV().fold()).as("RelatedObjects").select("User", "RelatedObjects")

但是这种方法不显示 UserRelatedObjects 之间的边缘。我还需要边缘才能将这些相关对象正确映射到父对象。

试试这个

g.V().hasLabel("User").as("User").bothE().as(“边”).map(select( “边缘”).i​​nV().fold()).as("RelatedObjects").select("User", "RelatedObjects",”边缘”)

我没有你要测试的图表,但我认为这个一般模式应该适合你。

g.withSideEffect('x', [] as Set).                        
     V().hasLabel("user").store('x').                                    
     bothE().store('x').                                  
     otherV().store('x').                                 
     cap('x')

编辑为不使用 Set

g.withSideEffect('x', []).                        
     V().has('code',"AUS").store('x').                                    
     bothE().store('x').                                  
     otherV().store('x').                                 
     cap('x').unfold().dedup().fold() 

这是它与 CosmosDB 一起工作的方式:

g.V().hasLabel("User")
    .as('Vertex') <------------------- alias for all users
    .map(bothE().fold())
    .as('Edges')
    .map(select('Vertex') <----------- this was the key. Map from the first step
        .bothE().otherV().fold())
    .as('RelatedVertices')
    .select('User', 'Edges', 'RelatedVertices')

不幸的是,withSideEffect 似乎不被 Microsoft.Azure.Graphs 库支持,所以不能使用下面由 提出的看起来更优雅的方法:

g.withSideEffect('x', [] as Set).                        
     V().hasLabel("user").store('x').                                    
     bothE().store('x').                                  
     otherV().store('x').                                 
     cap('x')

我认为你可以放弃所有步骤标签和使用副作用 - 只需使用 project():

gremlin> g.V().hasLabel('person').
......1>   project('user','edges','relatedVertices').
......2>     by().
......3>     by(bothE().fold()).
......4>     by(both().fold())
==>[user:v[1],edges:[e[9][1-created->3],e[7][1-knows->2],e[8][1-knows->4]],relatedVertices:[v[3],v[2],v[4]]]
==>[user:v[2],edges:[e[7][1-knows->2]],relatedVertices:[v[1]]]
==>[user:v[4],edges:[e[10][4-created->5],e[11][4-created->3],e[8][1-knows->4]],relatedVertices:[v[5],v[3],v[1]]]
==>[user:v[6],edges:[e[12][6-created->3]],relatedVertices:[v[3]]]