Search code examples
paginationgremlintinkerpoptinkerpop3janusgraph

Pagination in Gremlin


I'm trying to do pagination in Gremlin. I've followed the solution provided on gremlin recipes. So right now I am using the range() step to truncate results. This works well because when users asks for x results id only queries for x of them and doesn't perform full search which is significantly faster.

However in Gremlin docs it states that:

A Traversal’s result are never ordered unless explicitly by means of order()-step. Thus, never rely on the iteration order between TinkerPop3 releases and even within a release (as traversal optimizations may alter the flow).

And order is really important for pagination (we don't want user to have same results on different pages).

Adding order() would really slow down querying, since it will have to query all vertices that apply to the search and then truncate it with range().

Any ideas how this can be solved to have consistency between queries and still have smaller query time for not-full search?


Solution

  • The documentation is correct, but could use some additional context. Without order() you aren't guaranteed a specific order, unless the underlying graph database guarantees that order. TinkerPop should preserve that guarantee if it exists. So, ultimately you need to consider what the underlying graph will do.

    As you are using JanusGraph, I'm pretty sure that it does not guarantee order of result iteration but there are caveats depending on the type of indexing that you do. You can read more about that here but may want to ask specific questions on the JanusGraph user list.