Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to sort AND limit Noe4j result using Gremlin?

This is how you can sort (order) results from Neo4j graph using Gremlin:

g.v(id).out('knows').sort{it.name}

or

g.v(id).out('knows').sort{a,b -> a.name <=> b.name}

This is how to limit result using offset/skip and limit:

g.v(id).out('knows')[0..9]

However if you combine both sort and limit

g.v(id).out('knows').sort{it.name}[0..9]

it would throw an error...

javax.script.ScriptException: groovy.lang.MissingMethodException: No signature of method: java.util.ArrayList$ListItr.getAt() is applicable for argument types: (groovy.lang.IntRange) values: [0..9]
Possible solutions: getAt(java.lang.String), getAt(int), next(), mean(), set(java.lang.Object), putAt(java.lang.String, java.lang.Object)
like image 752
Alexei Tenitski Avatar asked Apr 28 '12 20:04

Alexei Tenitski


1 Answers

It took me a while to figure out that native Groovy methods like sort do not return Pipes, but iterators, iterables, etc. As such, to convert one of these objects back into a Pipeline flow you need to use _():

g.v(id).out('knows').sort{it.name}._()[0..9]
like image 76
Alexei Tenitski Avatar answered Nov 18 '22 17:11

Alexei Tenitski