简体   繁体   中英

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

I'm trying to compose one query to get all incoming and outgoing vertices, including their edges and directions, which would however return those vertices without edges as well.

I was able to work around the problem for now by forcing everything to have at least one edge, but it's something I'd like to avoid.

Maybe worth noting that I use the Graph API of Azure CosmosDB: https://docs.microsoft.com/en-us/azure/cosmos-db/gremlin-support

This is the query I'm using to return all vertices with their edges and related vertices:

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

I got this from: Gremlin get all incoming and outgoing vertex, including their edges and directions

This query produces the result which I am able to easily parse in a C# application later, however this query doesn't return vertices which don't have edges.

Any ideas?

EDIT

The closest I've got is this:

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

However this approach doesn't display edges between User and RelatedObjects . I also need edges to be able to map these related objects to parent object properly.

I think you can drop all the step labels and use of side-effects - just use 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]]]

Try this

gV().hasLabel("User").as("User").bothE().as(“edges”).map(select(“edges”).inV().fold()).as("RelatedObjects").select("User", "RelatedObjects",”edges”)

I don't have you graph to test with but this general pattern should work for you I think.

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

Edited to not use Set

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

This was the way it works with 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')

Unfortunately, the withSideEffect doesn't seem to be supported by Microsoft.Azure.Graphs library, so the below way, proposed by Kelvin which seems more elegant can't be used:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM