Search code examples
gremlintinkerpoptinkerpop3gremlin-server

Gremlin: select().count() always returns 1 when called inside choose()


select("anything").count() always returns 1 when called inside choose()

Why this happens? Is there any elegant and not slow to execute workaround for this problem? With "elegant and not slow" I mean a solution where I don't have to write the search 2 times because I cannot use select() to go back.

You can test by yourself this on the gremlin console with these lines:

g.addV("test1")
g.addV("test2")
g.addV("test3")

count works because not using select:

g.V().as("result").choose(V().count().is(gt(1)), constant("greater than 1"), constant("not greater than 1"))

count not working because the elements being counted comes from select:

g.V().as("result").choose(select("result").count().is(gt(1)), constant("greater than 1"), constant("not greater than 1"))

Solution

  • The select step is used to return to a point in your traversal. since each vertex as its own traversal, you will always select only 1 vertex.

    you should fold the "result" values and then count them.

    g.V().fold().as('result')
      choose(
        select('result').
        count(local).is(gt(1)),
        constant('greater than 1'),
        constant('not greater than 1')
      )
    

    example: https://gremlify.com/i0z0zqucgz