Max() function equivalent in gremlin

1,101 views
Skip to first unread message

Laksh Krishna

unread,
May 8, 2013, 5:13:08 PM5/8/13
to gremli...@googlegroups.com
Is there a equivalent of max() function in gremlin ? I am having to run gremlin queries that need to return an edge that has the max value (say an long value) which I then have to evaluate using filters.

Marko Rodriguez

unread,
May 8, 2013, 5:51:31 PM5/8/13
to gremli...@googlegroups.com
Hi,

You can do this:
gremlin> g.v(1).transform{x = it.outE.weight.max(); it.outE.find{it.weight == x}}
==>e[8][1-knows->4]

Its not the prettiest, but does the trick. You might want to wrap that into a function and make it more efficient as well (no need to iterate over the edge list twice).

def maxEdge(v) {
x = v.outE.weight.max()
v.outE.find{it.weight == x}
}

gremlin> g.v(1).transform{maxEdge(it)}
==>e[8][1-knows->4]

HTH,
Marko.
On May 8, 2013, at 3:13 PM, Laksh Krishna <lkri...@gmail.com> wrote:

Is there a equivalent of max() function in gremlin ? I am having to run gremlin queries that need to return an edge that has the max value (say an long value) which I then have to evaluate using filters.

--
You received this message because you are subscribed to the Google Groups "Gremlin-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gremlin-user...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Laksh Krishna

unread,
May 8, 2013, 6:19:41 PM5/8/13
to gremli...@googlegroups.com
Marko,

Thanks. That works. 

How do I do this in a loop? Instead on running this on a specific vertex I want to get all vertices that have maximum weights (given each of them may have different weight recordings).

Thanks,
Laksh

Laksh Krishna

unread,
May 9, 2013, 2:29:32 PM5/9/13
to gremli...@googlegroups.com
I tried this inside Rexster Doghouse and the function always returns a null in the gremlin shell. When i proceeded with the actual call to the function I got a MissingMethodException. 

>javax.script.ScriptException: groovy.lang.MissingMethodException: No signature of method: com.tinkerpop.gremlin.groovy.GremlinGroovyPipeline.tranform() is applicable for argument types: (Script587$_run_closure1) values: [Script587$_run_closure1@ed80ed8]
Possible solutions: transform(com.tinkerpop.pipes.PipeFunction), transform(groovy.lang.Closure), random(java.lang.Double)

Noticed that in one of the thread someone reported the same problem with function defs. Wanted to know if this is a groovyengine issue or has it been fixed in the latest gremlin version.

On Wednesday, May 8, 2013 4:51:31 PM UTC-5, Marko A. Rodriguez wrote:

Marko Rodriguez

unread,
May 9, 2013, 2:35:08 PM5/9/13
to gremli...@googlegroups.com
javax.script.ScriptException: groovy.lang.MissingMethodException: No signature of method: com.tinkerpop.gremlin.groovy.GremlinGroovyPipeline.tranform() is applicable for argument types: (Script587$_run_closure1) values: [Script587$_run_closure1@ed80ed8]

You misspelled the method.

You have 'tranform'

Its 'transform' (note the 's').

Marko.

Laksh Krishna

unread,
May 9, 2013, 3:51:42 PM5/9/13
to gremli...@googlegroups.com
Marko,

Thanks. My bad. I fixed the typo and it looks like the function is being called.

However, I run into this problem with variable 'it' being null inside the find() function. I tried having all in one call (like you had provided in the first example) as well as embedded within the function. Either case resulted in 'it' being null.

gremlin>> Cannot get property 'therapy|date' on null object

This is what's being defined in gremlin using the Doghouse tool:

def maxEdge(v)
{ x = v.outE('patient|hastherapy').property('therapy|date').max;
 return v.outE('patient|hastherapy').find{it.getProperty('therapy|date') == x}
}

g.V('case|id',XXXXXX).outE('case|haspatient').inV.transform{maxEdge(it)}

I tried using this within a JUnit test case using Gremlin APIs and got a similar result. Not sure if find() function is loosing handle on the iterator.

Thanks,
Laksh

Laksh Krishna

unread,
May 9, 2013, 5:42:29 PM5/9/13
to gremli...@googlegroups.com
Turned out that I had something going on in Rexster. Tried restarting Rexster and submitted the same query and it works now within DogHouse as well as in my unit test. 
Reply all
Reply to author
Forward
Message has been deleted
0 new messages