首页 文章

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

提问于
浏览
0

我正在尝试编写一个查询来获取所有传入和传出的顶点,包括它们的边和方向,然而这些顶点也会返回没有边的顶点 .

我现在能够通过强迫所有东西至少有一个边缘来解决这个问题,但这是我想要避免的 .

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

这是我用来返回所有顶点及其边和相关顶点的查询:

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

我得到了这个:Gremlin get all incoming and outgoing vertex, including their edges and directions

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

有任何想法吗?

编辑

我最接近的是:

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

但是,此方法不会显示 UserRelatedObjects 之间的边 . 我还需要边缘能够正确地将这些相关对象映射到父对象 .

4 回答

  • 3

    我想你可以删除所有步骤标签和副作用 - 只需使用 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]]]
    
  • 1

    试试这个

    GV() . hasLabel( “用户”) . 如( “用户”) . 博特() . 如(“边缘”) . 图(选择(“边缘”) . INV() . 折叠()) . 如(” RelatedObjects“) . select(”User“,”RelatedObjects“,”edges“)

  • 0

    我没有你用图表测试,但我认为这种一般模式对你有用 .

    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()
    
  • 1

    这是它与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库的支持,所以下面的方式,由Kelvin提出,似乎更优雅,不能使用:

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

相关问题