indexOf Vertex with given property in a sorted list
Hi all!
I've been trying to create a paginated GET REST api and sometimes I need to enforce a
range
on the resulting vertices starting from a vertex matching a given ID.
Let's say my gremlin query so far returns the following list of vertices, sorted based on some property comparison:
Is there a way of asking gremlin what is the index of vertex with id K
in the above list? Can all of this be done in a single query so that my final result simply returns the following, given K
and a range of 3?
Some pseudocode for clarity:
Thank you in advance6 Replies
Couldn't you just use the range step? https://tinkerpop.apache.org/docs/current/reference/#range-step
I think you would have to know the range before hand. Accoridng to the docs, if you use -1 as the 2nd argument in
.range()
it will emit the reamining verticies
So maybe something like .range(3, -1)
That's what I have in mind too, but I need to know the index of
V{id:K}
in advance
so it would be something like
Hmm im not sure
no worries, appreciate the help 🙏
@spmallette @KelvinL is this even possible with gremlin? I know this can be done with 2 queries: get the list and find the index of vertex with
id
, then submit another query with the correct range applied. But this is not really ACID compliant, that's why I'm looking for a single-query solutionIf you have the results in a list, the Gremlin
index
step can give you the list back with each cell having an index as well as the value. So for example:
Using that building block you should be able to build what you need. Often the limit
and tail
steps are useful when working with indexed lists.
So if we have
we might do
and then
Similarly
As usual, this is brilliant Kelvin. I'll put this into work and report back 🙏