Search code examples
orientdbgraph-databasesgremlintraversal

Merge vertex list in gremlin orientDb


I am a newbie in the graph databases world, and I made a query to get leaves of the tree, and I also have a list of Ids. I want to merge both lists of leaves and remove duplicates in a new one to sum property of each. I cannot merge the first 2 sets of vertex

g.V().hasLabel('Group').has('GroupId','G001').repeat(
    outE().inV()
).emit().hasLabel('User').as('UsersList1')

.V().has('UserId', within('001','002')).as('UsersList2')

.select('UsersList1','UsersList2').dedup().values('petitions').sum().unfold()

Regards


Solution

  • There are several things wrong in your query:

    • you call V().has('UserId', within('001','002')) for every user that was found by the first part of the traversal
    • the traversal could emit more than just the leafs
    • select('UsersList1','UsersList2') creates pairs of users
    • values('petitions') tries to access the property petitions of each pair, this will always fail

    The correct approach would be:

    g.V().has('User', 'UserId', within('001','002')).fold().
      union(unfold(),
            V().has('Group','GroupId','G001').
                repeat(out()).until(hasLabel('User'))).
      dedup().
      values('petitions').sum()