3

I'm trying to return a partial set of results using range()for pagination in my app. I need to also return the total number of records in the overall result as part of the response. A response should look like:

{
  postcount: 239,
  posts: [
    {
      postID: 1,
      postTitle:'First post'
    },
    {
      postID: 2,
      postTitle:'Second post'
    },
    {
      postID: 3,
      postTitle:'Third post'
    }
  ]
}

I've been trying to make something like this work with no luck:

g.V().hasLabel('post').as('postcount','posts').select('postcount','posts').by(__.count()).by(__.range(0,2).valueMap())

That appends a postcount = 1 property to each item. How do I adjust this?

Fook
  • 5,320
  • 7
  • 35
  • 57

3 Answers3

2

You could break it up into two queries:

pCount = g.V().hasLabel("post").count().next();
pList = g.V().hasLabel("post").range(0,2).valueMap().toList();
map = ["postcount": pCount, "posts": pList]
Jason Plurad
  • 6,682
  • 2
  • 18
  • 37
1

You could use union step to achieve what you want to do. Example:

 g.V().hasLabel("post").union(
               __.select("postcount").by(__.count()), // postcount property
               __.range(0,2).valueMap() // your range
            ) //end of union

For more info, check docs

Mohamed Taher Alrefaie
  • 15,698
  • 9
  • 48
  • 66
  • `Union` seems like the right approach but I'm still struggling with the syntax. I tried your example but got 'The traversal strategies are complete and the traversal can no longer be modulated'. I also tried the following: `g.V().hasLabel("post").as("postcount","post").union(select("postcount").by(__.count()),select("post").by(__.range(0,2).valueMap()))` but it returns post, count = 1, post, count = 1 every line. – Fook May 23 '16 at 17:42
  • Ah, your example is missing a `_` before `.count()`. However it just outputs all my posts without regard to `range` and does not include a count. Not sure why this doesn't work, because I follow the thought process and it seems like it should. I'm running this in the console if it matters. – Fook May 23 '16 at 21:31
1

Answer found on the Gremlin-Users list per Daniel Kuppitz:

In 3.2.0:

> g.V().hasLabel("person").fold().project("users","userCount").by(range(local,
> 0, 2)).by(count(local))

Prior 3.2.0:

g.V().hasLabel("person").fold().as("users","userCount").select("users","userCount").by(range(local,
> 0, 2)).by(count(local))
Fook
  • 5,320
  • 7
  • 35
  • 57