Search code examples
azure-cosmosdbgraph-databasesgremlintinkerpop3

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://learn.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.


Solution

  • 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]]]