Search code examples
gremlintinkerpop

Gremlin filter by max property


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.


Solution

  • 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