Gremlin: from groupCount back to nodes

292 views
Skip to first unread message

Alexei Tenitski

unread,
Apr 28, 2012, 5:37:35 PM4/28/12
to ne...@googlegroups.com
Hi there,

Can someone please help me with groupCount.

Here is a query which gets 10 most used tags

g.V.filter{it.type=='TAG'}.in('tagged').groupCount().cap().next().sort{a,b
-> b.value <=> a.value}[0..9]

Response is

==> v[6815]=6
==> v[6789]=5
==> v[6778]=3
==> v[6269]=2
==> v[6791]=2
==> v[5991]=2
==> v[6281]=1
==> v[1028]=1


How can I go back to nodes from here in order to retrieve additional data?

Cheers,
Alexei

Peter Neubauer

unread,
Apr 29, 2012, 6:37:37 AM4/29/12
to ne...@googlegroups.com
Mmh.
this is a normal groovy map you are getting back, have the nodes as
keys. So you should be able to do something like
http://stackoverflow.com/questions/4898062/get-key-in-groovy-maps

map.keySet() and then continue.

Cheers,

/peter neubauer

G:  neubauer.peter
S:  peter.neubauer
P:  +46 704 106975
L:   http://www.linkedin.com/in/neubauer
T:   @peterneubauer

If you can write, you can code - @coderdojomalmo
If you can sketch, you can use a graph database - @neo4j

James Thornton

unread,
Apr 29, 2012, 7:11:40 AM4/29/12
to ne...@googlegroups.com
Yes, the map's keys are the nodes, but you should be using indexes to lookup the vertex type, not using g.V because it does a table scale:

g.idx(index_name)[['type':'tag']].in('tagged')....

- James

Marko Rodriguez

unread,
Apr 29, 2012, 2:33:37 PM4/29/12
to ne...@googlegroups.com
Hi,

To get the keys (vertices), simply do:

...groupCount().cap().next().sort{a,b -> b.value <=> a.value}[0..9].scatter.transform{it.key}

1. scatter will "unroll" your map into its entries (assuming, I believe Gremlin 1.5+)
2. transform will then emit the key of the entry, thus, separating the key from its entry value.

If you don't want it as a one line, you can do:

m = ...groupCount().cap().next().sort{a,b -> b.value <=> a.value}[0..9]
m.each { k, v ->
// do what you want with each key and value
}

HTH,
Marko.

Alexei Tenitski

unread,
May 1, 2012, 6:11:39 AM5/1/12
to ne...@googlegroups.com
Thanks Peter, that was exactly what I needed.

Cheers,
Alexei

Alexei Tenitski

unread,
May 1, 2012, 6:13:06 AM5/1/12
to ne...@googlegroups.com
Yes, figuring out how to use indexes is next in my things to learn about
Neo4j :)

Alexei

Alexei Tenitski

unread,
May 1, 2012, 6:19:03 AM5/1/12
to ne...@googlegroups.com
Marco, for some reason, .scatter.transform{it.keys} just returns undefined.
Not sure which gremlin I have here but Neo4j itself is 1.6.

I've just tried with parenthesis .scatter():

No signature of method: java.util.LinkedHashMap.scatter() is applicable
for argument types: () values: []

Cheers,
Alexei

Peter Neubauer

unread,
May 1, 2012, 8:40:27 AM5/1/12
to ne...@googlegroups.com
Alexei,
can you do Gremlin.VERSION to get the version? I believe it is 1.4 though ...

Cheers,

/peter neubauer

G:  neubauer.peter
S:  peter.neubauer
P:  +46 704 106975
L:   http://www.linkedin.com/in/neubauer
T:   @peterneubauer

If you can write, you can code - @coderdojomalmo
If you can sketch, you can use a graph database - @neo4j


Marko A. Rodriguez

unread,
May 1, 2012, 10:13:40 AM5/1/12
to ne...@googlegroups.com
Hi,

Apologies. There is a bug in the expression I gave you. Do:

...[0..9]._().scatter.transform....

The _() turns the map back into a pipe. Also, I believe you need Gremlin 1.5+ to scatter a Map's entries. This might be getting a little crazy. Best to just use Peter's m.keySet() solution as it is the most straightforward.

Good luck,
Marko.

http://markorodriguez.com

Alexei Tenitski

unread,
May 1, 2012, 4:56:36 PM5/1/12
to ne...@googlegroups.com
Hi Marco,

Yep, _() did the trick, thanks!

Regards,
Alexei
Reply all
Reply to author
Forward
0 new messages