Search code examples
gremlingraph-databasestinkerpopgremlin-servergremlinpython

How to use pagination in Apache TinkerPop Gremlin?


How to use pagination in apache tinker pop gremlin? Trying to get both count and the vertex properties

g.V().hasLabel('person').fold().as('persons','count').
       select('persons','count').
         by(range(local, 2, 4)).
         by(count(local))

output:-

{'persons': [v[2e6279e5-ad7c-48e3-9527-4972e55ecd24],
v[bb01581c-de6f-4c57-92f5-6fe090011279]], 'count': 36}

How to get the properties of vertex instead of just T.id?


Solution

  • Adding another answer just to point out a possible performance optimization. If there are a lot of people in the graph, materializing all the properties and then selecting just a few is potentially going to give the query engine a lot wasted work to do. It's often better to materialize results as late in the query as possible. Here's a minor rewrite to the other answer that does just that.

    g.V().hasLabel('persons').fold().as('persons','count').
           select('persons','count').
             by(range(local, 2, 4).unfold().elementMap().fold()).
             by(count(local))   
    

    Of course, I assume you have a scenario that is more complex as the example shown does not need the fold at all. You could just use a union step or something else that aggregates results.

    g.V().hasLabel('person').
          union(range(2,4).elementMap(),count()).
          fold()