how to aggregate by edge property

590 views
Skip to first unread message

Martin Junghanns

unread,
Oct 11, 2013, 8:59:57 AM10/11/13
to gremli...@googlegroups.com
Hi,

I'm currently looking into Cypher and Gremlin and I need a little help with grouping in gremlin

in Cypher I write

START n=node(42)  
MATCH n-[:FRIEND_OF*1..2]-()<-[r:REVIEWED_BY]-p  
RETURN p.title, count(p), sum(r.rating) as h  
ORDER BY h DESC;

So, I'd like to know which products where reviewed by my friends and their friends and order them by their rating (which is an edge property).

In Gremlin my first guess was:

m=[:]
v.both('FRIEND_OF').both('FRIEND_OF').inE('REVIEWED_BY').as('r').outV.groupBy(m){it.title}{r.rating.sum()}

But as may already see, the second closure doesn't work as expected because I cannot refer to 'r' again. I also tried to get back to the edges by using {it.outE('REVIEWED_BY').retain(r).rating.sum()} but this didn't work because of the same reason.

A little hint would be very appreciated. Thank you guys in advance.

Martin

Martin Junghanns

unread,
Oct 11, 2013, 9:31:44 AM10/11/13
to gremli...@googlegroups.com
It works when I store the edges

> r =[]
> m=[:]
> v.both('FRIEND_OF').both('FRIEND_OF').inE('REVIEWED_BY').aggregate(r)
> v.both('FRIEND_OF').both('FRIEND_OF').in('REVIEWED_BY').groupBy(m){it.title}{it.outE('REVIEWED_BY').retain(r).rating}{it.sum()}

is it possible to do this in a single statement?

Martin

Stephen Mallette

unread,
Oct 12, 2013, 6:04:43 AM10/12/13
to gremli...@googlegroups.com
I tried to convert your problem to the toy graph:

gremlin> g = TinkerGraphFactory.createTinkerGraph()                                
==>tinkergraph[vertices:6 edges:6]
gremlin> m=[:];g.V.outE.groupBy(m){it.inV.next().name}{it.weight}{it.sum()}
==>e[7][1-knows->2]
==>e[8][1-knows->4]
==>e[9][1-created->3]
==>e[12][6-created->3]
==>e[10][4-created->5]
==>e[11][4-created->3]
gremlin> m
==>lop=1.0000000149011612
==>ripple=1.0
==>josh=1.0
==>vadas=0.5

The difference in my approach is to groupBy on the edge and traverse to inV in the first groupBy closure.  Then to order you could just orderMap:

gremlin> g.V.outE.groupBy{it.inV.next().name}{it.weight}{it.sum().doubleValue()}.cap.orderMap(T.decr)
==>lop
==>ripple
==>josh
==>vadas

HTH,

Stephen





--
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.

Martin Junghanns

unread,
Oct 12, 2013, 6:18:24 AM10/12/13
to gremli...@googlegroups.com
Nice solution! Thank you.

Martin
Reply all
Reply to author
Forward
0 new messages