What's the most efficient way of starting a Gremlin query from a list?

1,183 views
Skip to first unread message

James Thornton

unread,
Aug 27, 2011, 2:21:41 AM8/27/11
to gremli...@googlegroups.com
You can start with a range of vertices like this:

gremlin> g.V[1..2]
==>v[2]
==>v[1]

But how do you start it with a list?

gremlin> g.V[1,2,3,5,8,13,21,34,55,89,144] 
No signature of method: com.tinkerpop.gremlin.pipes.GremlinPipeline.getAt() is applicable for argument types: (java.util.ArrayList) values: [[1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144]]
Possible solutions: getAt(java.lang.String), getAt(groovy.lang.Range), getAt(java.util.Map), getAt(java.lang.String), getAt(int), getAt(java.lang.Integer)
Display stack trace? [yN] 


Pierre De Wilde

unread,
Aug 27, 2011, 2:51:40 AM8/27/11
to gremli...@googlegroups.com
Hey,

In your examples, it is not clear if you specify the index value or the vertex id. 

I don't see an use case with the index value since g.V returns all vertices in an undefined order.

Anyway, to start a Gremlin statement with a list of vertices using index value:

i=0
g.V.filter{i++ in [1,2,3,5,8,13,21,34,55,89,144]}


To start a Gremlin statement with a list of vertices using vertex id:

g.V.filter{it.id in [1,2,3,5,8,13,21,34,55,89,144]}


A far more efficient version using vertex id is

[ g.[1],g.v[2],g.v[3],g.v[5],g.v[8],g.v[13],g.v[21],g.v[34],g.v[55],g.v[89],g.v[144] ]._()

because instead of sequential filtering, you only get what you want.


HTH,
Pierre

James Thornton

unread,
Aug 27, 2011, 3:00:00 AM8/27/11
to gremli...@googlegroups.com
Hi Pierre -

Thanks for help. A use case would be a search engine like Solr returning a list of 1000 IDs that you want to rank and sort.

- James

Pierre De Wilde

unread,
Aug 27, 2011, 3:06:22 AM8/27/11
to gremli...@googlegroups.com
> A use case would be a search engine like Solr returning a list of 1000 IDs that you want to rank and sort.

Why do you want to apply Fibonacci indices at that ?

James Thornton

unread,
Aug 27, 2011, 3:06:44 AM8/27/11
to gremli...@googlegroups.com
A use case would be a search engine like Solr returning a list of 1000 IDs that you want to rank and sort.

I'm thinking of ways to apply Marko's ideas on "local rank" to search results (http://markorodriguez.com/2011/03/30/global-vs-local-graph-ranking/), where local rank is a subset of all the vertices. Global rank is all vertices, and ego rank is one.

- James

James Thornton

unread,
Aug 27, 2011, 3:09:44 AM8/27/11
to gremli...@googlegroups.com
Why do you want to apply Fibonacci indices at that ?

No particular reason -- I was just using it as an example of a non-sequential list :)

Marko Rodriguez

unread,
Aug 27, 2011, 10:54:05 AM8/27/11
to gremli...@googlegroups.com
Hi,

As Pierre says, you can do:

[g.v(1), g.v(2), g.v(10)]._().out.in....

Perhaps a bit verbose? I can add this method to Gremlin:

g.v(1,2,10)._().out.in...

Would that be cool?

I could also turn it into a Pipe and thus:

g.v(1,2,10).out.in...

...however, problem with the last is that then g.v() would have mixed mappings so thats probably not a good idea.

Thoughts?,
Marko.

James Thornton

unread,
Aug 27, 2011, 11:01:26 AM8/27/11
to gremli...@googlegroups.com
> I can add this method to Gremlin:

Hey Marko -

Yeah, that would be totally cool -- whatever you think would be the most performant for a large number of vertices. I'm still exploring this idea, but I think this might be a good way to build ranking algorithms into search.  

- James


Marko Rodriguez

unread,
Aug 27, 2011, 11:35:50 AM8/27/11
to gremli...@googlegroups.com
Hi,


You can now do:

gremlin> g.v(1) ==>v[1] gremlin> g.v(1,2,3) ==>v[1] ==>v[2] ==>v[3] gremlin> g.v(1,2,1000) ==>v[1] ==>v[2] ==>null gremlin> g.e(7,8) ==>e[7][1-knows->2] ==>e[8][1-knows->4]

If you pass in one id (e.g. g.v(1)), you will get back a single element. If you pass in more than one id (e.g. g.v(1,2)), you will get back a list of elements.

This has been pushed to GitHub in Gremlin 1.3-SNAPSHOT.

Enjoy,
Marko.

James Thornton

unread,
Aug 27, 2011, 11:59:17 AM8/27/11
to gremli...@googlegroups.com
> If you pass in one id (e.g. g.v(1)), you will get back a single element. 
> If you pass in more than one id (e.g. g.v(1,2)), you will get back a list of elements.
> This has been pushed to GitHub in Gremlin 1.3-SNAPSHOT.

That was fast! :) Thanks man.

- James

Pierre De Wilde

unread,
Aug 28, 2011, 3:33:32 AM8/28/11
to gremli...@googlegroups.com
Hey,

Cool.

Why g.v(id,id) does return LinkedList instead of FluentPipeline ?

g.v(1).out but g.v(1,2)._().out but g.idx(T.v)[[name:'josh']].out

Thanks,
Pierre

Marko Rodriguez

unread,
Aug 28, 2011, 8:23:58 AM8/28/11
to gremli...@googlegroups.com
Hi,

gremlin> g.v(1).class
==>class com.tinkerpop.blueprints.pgm.impls.tg.TinkerVertex
gremlin> g.v(1,2).class
==>class java.util.LinkedList
gremlin> g.idx(T.v)[[name:'josh']].class
==>class com.tinkerpop.pipes.util.FluentPipeline

g.v() are  not pipelines. I can make g.v(1,2) a pipeline... ? .. .? . ... The reason I didn't was for stuff like this:

g.v(1).out.out.except(g.v(2,3,4)).in

In other words, g.v() is just a "getter" of vertices, not a pipeline construct.

Marko.

Pierre De Wilde

unread,
Aug 28, 2011, 8:51:45 AM8/28/11
to gremli...@googlegroups.com
Hi Marko,

Both makes sense but I concur by your last argument: g.v() is just a "getter" of vertices.

g.v(2,3,4)._() implicit notation is definitively not my cup of (Oolong) tea.

Therefore, I reiterate an old request to use a more explicit .toPipe() instead of / in complement to  ._() notation.

Thanks,
Pierre

Peter Neubauer

unread,
Aug 28, 2011, 8:58:25 AM8/28/11
to gremli...@googlegroups.com
+1

On toPipe
Also, I would love to make >> explicit like emit() or so. These two constructs lead to a lot of errors and confusion...

/Peter

On Sunday, August 28, 2011, Pierre De Wilde <pierre...@gmail.com> wrote:
> Hi Marko,
> Both makes sense but I concur by your last argument: g.v() is just a "getter" of vertices.
> g.v(2,3,4)._() implicit notation is definitively not my cup of (Oolong <http://en.wikipedia.org/wiki/Oolong>) tea.
> Therefore, I reiterate an old request <https://github.com/tinkerpop/gremlin/issues/203> to use a more explicit .toPipe() instead of / in complement to  ._() notation.

> Thanks,
> Pierre
>
>
> On Sun, Aug 28, 2011 at 2:23 PM, Marko Rodriguez <okram...@gmail.com> wrote:
>>
>> Hi,
>> gremlin> g.v(1).class
>> ==>class com.tinkerpop.blueprints.pgm.impls.tg.TinkerVertex
>> gremlin> g.v(1,2).class
>> ==>class java.util.LinkedList
>> gremlin> g.idx(T.v)[[name:'josh']].class
>> ==>class com.tinkerpop.pipes.util.FluentPipeline
>> g.v() are  not pipelines. I can make g.v(1,2) a pipeline... ? .. .? . ... The reason I didn't was for stuff like this:
>> g.v(1).out.out.except(g.v(2,3,4)).in
>> In other words, g.v() is just a "getter" of vertices, not a pipeline construct.
>> Marko.
>> http://markorodriguez.com <http://markorodriguez.com>

>> On Aug 28, 2011, at 1:33 AM, Pierre De Wilde wrote:
>>
>> Hey,
>> Cool.
>> Why g.v(id,id) does return LinkedList instead of FluentPipeline ?
>> g.v(1).out but g.v(1,2)._().out but g.idx(T.v)[[name:'josh']].out
>> Thanks,
>> Pierre
>>
>> On Sat, Aug 27, 2011 at 5:59 PM, James Thornton <james.t...@gmail.com> wrote:
>>>
>>> > If you pass in one id (e.g. g.v(1)), you will get back a single element. 
>>> > If you pass in more than one id (e.g. g.v(1,2)), you will get back a list of elements.
>>> > This has been pushed to GitHub in Gremlin 1.3-SNAPSHOT.
>>> That was fast! :) Thanks man.
>>> - James
>>
>
>

--

Cheers,

/peter neubauer

GTalk:      neubauer.peter
Skype       peter.neubauer
Phone       +46 704 106975
LinkedIn   http://www.linkedin.com/in/neubauer
Twitter      http://twitter.com/peterneubauer

http://www.neo4j.org               - Your high performance graph database.
http://startupbootcamp.org/    - Öresund - Innovation happens HERE.
http://www.thoughtmade.com - Scandinavia's coolest Bring-a-Thing party.

James Thornton

unread,
Aug 28, 2011, 9:19:39 AM8/28/11
to gremli...@googlegroups.com
> I would love to make >> explicit like emit() or so. These two constructs lead to a lot of errors and confusion...

I agree, the >> too often leads to confusion. You can use toList() in its place sometimes, but a more explicit alternative for >> would help.

- James

Marko Rodriguez

unread,
Aug 28, 2011, 9:25:58 AM8/28/11
to gremli...@googlegroups.com
Hi,

I am not a fan of toPipe(). I could do something like:

start(g.v(2,3,4)).out.in.out...

Which would be a static method reference to:

new FluentPipeline().start(g.v(2,3,4)).out.in.out...

Marko.

Marko Rodriguez

unread,
Aug 28, 2011, 12:37:52 PM8/28/11
to gremli...@googlegroups.com
Hi Peter,

You can do:

g.v(1).out.out.iterate()  <=> g.v(1).out.out >> -1
g.v(1).out.out.toList() <=> g.v(1).out.out >> []
g.v(1).out.out.next() <=> g.v(1).out.out >> 1
g.v(1).out.out.next(5) <=> g.v(1).out.out >> 5

*** NOTE: next(int number) was recently implemented, but not pushed GitHub or Maven2.

Hope that is sufficient for your cares,
Marko.

Peter Neubauer

unread,
Aug 28, 2011, 12:55:43 PM8/28/11
to gremli...@googlegroups.com
Yup,
Looks good!

/peter
>>> http://markorodriguez.com <http://markorodriguez.com/> <http://markorodriguez.com <http://markorodriguez.com/>>

>>> On Aug 28, 2011, at 1:33 AM, Pierre De Wilde wrote:
>>>
>>> Hey,
>>> Cool.
>>> Why g.v(id,id) does return LinkedList instead of FluentPipeline ?
>>> g.v(1).out but g.v(1,2)._().out but g.idx(T.v)[[name:'josh']].out
>>> Thanks,
>>> Pierre
>>>
>>> On Sat, Aug 27, 2011 at 5:59 PM, James Thornton <james.t...@gmail.com> wrote:
>>>>
>>>> > If you pass in one id (e.g. g.v(1)), you will get back a single element. 
>>>> > If you pass in more than one id (e.g. g.v(1,2)), you will get back a list of elements.
>>>> > This has been pushed to GitHub in Gremlin 1.3-SNAPSHOT.
>>>> That was fast! :) Thanks man.
>>>> - James
>>>
>>
>>
>
> --
>
> Cheers,
>
> /peter neubauer
>
> GTalk:      neubauer.peter
> Skype       peter.neubauer
> Phone       +46 704 106975 <tel:%2B46%20704%20106975>> http://www.neo4j.org <http://www.neo4j.org/>               - Your high performance graph database.

> http://startupbootcamp.org/    - Öresund - Innovation happens HERE.
> http://www.thoughtmade.com <http://www.thoughtmade.com/> - Scandinavia's coolest Bring-a-Thing party.

Pierre De Wilde

unread,
Aug 28, 2011, 3:19:57 PM8/28/11
to gremli...@googlegroups.com
Hey,

Thanks for those good alternatives to >> x.

Maybe .next(-1) as an alternative to .iterate() ?

Regarding _() issues, devil advocate put the shoes of newbie and try:

gremlin> g.v(1).out
==>v[2]
==>v[3]
==>v[4]

gremlin> g.v(1,2).out
==>[StartPipe, OutPipe]
==>[StartPipe, OutPipe]

gremlin> g.V.filter{it.id in [1,2]}.out   // no results because ids are strings in TinkerGraph example
gremlin> g.V.filter{it.id in ['1','2']}.out
==>v[2]
==>v[3]
==>v[4]

Newbie is confused, as devil advocate. But not for the same reasons: 

- newbie uses try-and-error to figure out how Gremlin works 
and is confused because he doesn't understand g.v(1,2).out results.

- devil advocate knows that g.v(1,2) returns LinkedList that needs to be converted 
into FluentPipeline using the ._() notation and is confused by this weird syntax.

I definitely need a rest.

Pierre
Reply all
Reply to author
Forward
0 new messages