0
votes

When I execute the following in a gremlin console I get the expected result.

g.V('name', 'a').next().query().has('b', GREATER_THAN_EQUAL, 100).orderBy('timestamp', Order.DESC).edges()

Now I am trying to execute the same from Java (following this guide) however I cannot get it to work.

I've tried this

Pipe pipe = Gremlin.compile("_().query().has('b', GREATER_THAN_EQUAL, 100).orderBy('timestamp', Order.DESC).edges()");
pipe.setStarts(new SingleIterator<Vertex>(graph.getVertices("name", 'a').iterator().next()));
for(Object name : pipe) {

}

javax.script.ScriptException: javax.script.ScriptException: groovy.lang.MissingMethodException: No signature of method: com.tinkerpop.gremlin.groovy.GremlinGroovyPipeline.query() is applicable for argument types: () values: [] Possible solutions: every(), every(groovy.lang.Closure), grep(), tree([Lcom.tinkerpop.pipes.PipeFunction;), tree([Lgroovy.lang.Closure;), tree(com.tinkerpop.pipes.util.structures.Tree)

And this

Pipe pipe = Gremlin.compile("_().next().query().has('b', GREATER_THAN_EQUAL, 100).orderBy('timestamp', Order.DESC).edges()");
pipe.setStarts(new SingleIterator<Vertex>(graph.getVertices("name", 'a').iterator().next()));
for(Object name : pipe) {

}

javax.script.ScriptException: javax.script.ScriptException: groovy.lang.MissingMethodException: No signature of method: com.tinkerpop.gremlin.groovy.jsr223.GremlinGroovyScriptEngine.query() is applicable for argument types: () values: [] Possible solutions: every(), every(groovy.lang.Closure), grep(), grep(java.lang.Object), any(), dump()

Any ideas?

2

2 Answers

0
votes

Okay so I have decided to use GremlinGroovyScriptEngine instead of Gremlin.compile(). This approach is also described on the same guide and I actually prefer this because it gives me parameterisation and I don't need to modify the original query (replace g. with _()).

ScriptEngine engine = new GremlinGroovyScriptEngine();

Bindings bindings = engine.createBindings();
bindings.put("g", graph);
bindings.put("value", 100); 
bindings.put("DESC", Order.DESC);

engine.eval("g.V('name', 'a').next().query().has('b', Compare.GREATER_THAN_EQUAL, value).orderBy('timestamp', DESC).edges()", bindings);

I would still be interested in knowing why Gremlin.compile did not work, hopefully the above will be helpful to someone else.

0
votes

This line looks suspicious to me:

Pipe pipe = Gremlin.compile("_().next().query().has('b', GREATER_THAN_EQUAL, 100).orderBy('timestamp', Order.DESC).edges()");

You are trying to compile a Pipe out of something that doesn't evaluate to a Pipeline. In other words, you start with the identity pipe (_()) but then you next() it out and drop down into a vertex query which returns edges() edges() returns a Iterator and not a Pipeline. If you look at the example of Gremlin.compile the evaluated code of the Gremlin string returns a pipeline.

Pipe pipe = Gremlin.compile("_().out('knows').name");

My guess is that if you instead changed your code to something like (untested):

Pipe pipe = Gremlin.compile("_().outE.has('b', GREATER_THAN_EQUAL, 100).order{it.b.timestamp <=> it.a.timestamp}");
pipe.setStarts(new SingleIterator<Vertex>(graph.getVertices("name", 'a').iterator().next()));
for(Object name : pipe) {

}

you might have some success. I suppose that if that worked, then you would want to figure out how to re-optimize your query as I guess that some backends could take optimize the orderBy of the Vertex query, whereas the order Gremlin step is just an in-memory sort.