Pagination with range and count

194 views
Skip to first unread message

BDP

unread,
May 26, 2016, 4:17:34 AM5/26/16
to Gremlin-users
I'm building a system that needs paging; the ability to fetch records X through Y. As part of this I need to return the count of the total recordset I'm operating on. What I'm after is a result set like this:

{ userCount: 4,
  users: [
{name:marko, age:29},
{name:vadas, age:27}
]}

I'm trying to use a union to first fetch the count and then the users with a range:
g.V().hasLabel("person").union(__.select("userCount").by(__.count()),__.range(0,1).valueMap())

However that yields:

[
{name:marko, age:29},
{name:vadas, age:27},
{name:josh, age:32},
{name:peter, age:35}
]

... all the persons with no count. I'm not sure why it doesn't respect the range or output the count. How do I construct such a traversal?

Daniel Kuppitz

unread,
May 26, 2016, 5:48:11 AM5/26/16
to gremli...@googlegroups.com
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))

Cheers,
Daniel


--
You received this message because you are subscribed to the Google Groups "Gremlin-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gremlin-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/gremlin-users/391f2166-0395-4004-9966-4e21620ed62b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

BDP

unread,
May 26, 2016, 8:21:41 AM5/26/16
to Gremlin-users
Thank you Daniel! This is perfect.

BDP

unread,
May 29, 2016, 9:00:06 AM5/29/16
to Gremlin-users
Let's say I now need to fetch software that each person created as part of the result. It seems like the fold() step precludes me from traversing edges. I'm trying something like this:

g.V().hasLabel("person").fold().as("users","userCount").select("users","userCount").by(range(local, 0, 2).as("persons","created").select("persons","created").by().by(out("created"))).by(count(local))

java.util.ArrayList cannot be cast to org.apache.tinkerpop.gremlin.structure.Vertex

... which I understand is because fold() yielded an ArrayList. Do I need to unfold() at some point? I've tried but can't figure out how to convert back to vertices that I can traverse from.

BDP

unread,
May 29, 2016, 12:28:13 PM5/29/16
to Gremlin-users
I should mention that I'm using 3.0.1-incubating. Thanks for any help you can offer.

Daniel Kuppitz

unread,
May 29, 2016, 4:14:58 PM5/29/16
to gremli...@googlegroups.com
Yea, you'e almost had it. This should be the first by() traversal:

range(local, 0, 2).unfold().as("persons","created").select("persons","created").by().by(out("created").fold()).fold()

Cheers,
Daniel


BDP

unread,
May 29, 2016, 6:41:00 PM5/29/16
to Gremlin-users
Thank you, that's the solution. Here is the working version for anyone following along. I updated it a little to dump the users with valueMap() and emit the language name.

g.V().hasLabel("person").fold().as("users","userCount").select("users","userCount").by(range(local, 0, 2).unfold().as("persons","created").select("persons","created").by(valueMap()).by(out("created").values("lang").fold()).fold()).by(count(local))
==>[users:[[persons:[name:[marko], age:[29]], created:[java]], [persons:[name:[vadas], age:[27]], created:[]]], userCount:4]
Reply all
Reply to author
Forward
0 new messages