3

new to gremlin and I've been having trouble filtering vertices by a max value.

the simple graph looks something like this:

source.addV("x").property("id", "1").property("version", 1.0)
.addV("x").property("id", "1").property("version", 1.1)
.addV("x").property("id", "2").property("version", 1.0)

My query looks like this:

 source.V()
        .has(T.label, "x")
        .group()
        .by("id").unfold().where(select(Column.values).unfold().values("version").max())

Output i'm looking for would be

[{type:x, id:1, version:1.1}, {type:x, id:2, version:1.0}]

my issue is its throwing:

org.apache.tinkerpop.gremlin.driver.exception.ResponseException: {"requestId":"x","code":"InternalFailureException","detailedMessage":"null:select([null])"}

any help would be appreciated. Thanks.

noam621
  • 2,766
  • 1
  • 16
  • 26
sean loyola
  • 1,739
  • 1
  • 11
  • 11

2 Answers2

1

There are a couple of things going on here.

First, you are adding vertices without a label which while allowed is not a very common practice. You usually want to group like items together by the vertex label. You can add a vertex label to your traversal by putting the label name inside the addV() step like this:

g.addV("labelname")

Second, you are adding the version properties as strings instead of as numbers, which is what I assume you actually want as max() of a string is not really meaningful. To change this remove the quotes from around the version number like this:

g.addV().property("type", "x").property("id", "1").property("version", 1.0)

Third, the way to think about this problem is a bit different then you might expect. Instead of trying to find the max version number and comparing all the traversers against it the way to think about this problem is "Find me the vertex with the highest version, then for each vertex see if it's version number is equal". It is a bit difficult to wrap your head around this but here is a traversal that demonstrates how to do this:

g.V().
  order().
    by('version', desc).
  limit(1).as('b').
  V().as('a').
  where('a', eq('b')).
    by('version')
bechbd
  • 6,206
  • 3
  • 28
  • 47
  • Thanks @bechbd. that solves half of it. My issue is I need to find max version for each id. I'm trying to group by id but i run into the original problem. one i group i have a map with id as key and referenceVertex (or a value map if I add the .by(valueMap())) as the values. Once i try and use project or filter i get null:select([null]). – sean loyola May 21 '20 at 04:24
0

Since Id is a unique value I assume you meant custom ID property. Let's name it _id.

You can group by the _id and then order the answer by version:

g.V().hasLabel('x').
  group().by('_id').
    by(fold().order(local).by('version', desc).unfold()
      limit(1).valueMap().
        with(WithOptions.tokens)).select(values)

example: https://gremlify.com/ah

noam621
  • 2,766
  • 1
  • 16
  • 26