Passing dynamic value inside sample method in gremlin is not working

Viewed 251

I am a newbie to the gremlin. I am trying to get some random vertices based on some condition. But if I am using sample method with some mathematical computation it is not working. But if I give integer values it is working fine.

The following query is working fine:

g.V().hasLabel('Person').sample(1)

The following query is not working:

g.V().hasLabel('Person').sample(math('4/2'))

Actually, based on the number of persons I should perform some mathematical computations inside sample method. Thanks in advance

2 Answers

That's an interesting bit of Gremlin you are trying to run. I've not seen anyone try that before. Sadly, it won't work because sample() does not take a Traversal as an argument. It only takes a number. There is no workaround for this issue aside from doing the computation outside of Gremlin which isn't helpful if you need to choose your sample size based on data in your query. sample() isn't the only step that behaves this way. I've created TINKERPOP-2414 to track this issue.

All you need to do is: g.V().hasLabel('Person').sample(Math.round(4/2).intValue())

Gremlin is running in a groovy shell. When in doubt, you can check how to convert a Long into Integer so that the sample function which takes Integer as the argument can work.

Related