query for multiple edges

447 views
Skip to first unread message

SS

unread,
Mar 7, 2017, 1:29:00 AM3/7/17
to Gremlin-users
Hi .
 
   I have some experience with the cypher query language and neo4j but I am trying to learn gremlin and am new to it. I have a multi-graph where some of the vertices have 100-200 edges between them. I would like to know how to query for these multiple edges in gremlin.  I need the query to return the vertices that have multiple edges between them and the respective edges too along with the sum of the properties on the edges and order them on the basis of the number of edges. In cypher the query would look something like this:

MATCH (n:Entity)-[r]->(m:Entity)
WITH n as From, m as To, Collect(r) as edges, COUNT(r) as number, SUM(r.prop) as total
WHERE number > 1
RETURN From, To, total, edges, number
ORDER BY number desc LIMIT 10
 
How do I go about doing the same in gremlin?

Thanks.
SS

Daniel Kuppitz

unread,
Mar 8, 2017, 5:25:41 AM3/8/17
to gremli...@googlegroups.com
Here's my best guess (not sure if I interpreted the Cypher query correctly):

g.V().hasLabel("Entity").as("n").
  outE().as("r").inV().hasLabel("Entity").as("m").
  group().by(select("n","m")).by(select("r").by("prop").fold()).unfold().
  project("from","to","total","edges","number").
    by(select(keys).select("n")).
    by(select(keys).select("m")).
    by(select(values).sum(local)).
    by(select(values).count(local)).
  order().by(select("number", decr)).
  limit(10)

Cheers,
Daniel


--
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-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/gremlin-users/8721ece8-da7f-4aa2-b926-92391fbf9c9a%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Robert Dale

unread,
Mar 8, 2017, 8:33:35 AM3/8/17
to gremli...@googlegroups.com

On Wed, Mar 8, 2017 at 5:25 AM, Daniel Kuppitz <m...@gremlin.guru> wrote:
g.V().hasLabel("Entity").as("n").
  outE().as("r").inV().hasLabel("Entity").as("m").
  group().by(select("n","m")).by(select("r").by("prop").fold()).unfold().
  project("from","to","total","edges","number").
    by(select(keys).select("n")).
    by(select(keys).select("m")).
    by(select(values).sum(local)).
    by(select(values).count(local)).
  order().by(select("number", decr)).
  limit(10)


Awesome start. I think the desired output is more like this.  This works on TinkerGraph Modern after adding a few more edges between 'person's:

g.V().hasLabel("person").as("n").
  outE().as("r").inV().hasLabel("person").as("m").
  group().by(select("n","m")).by(select("r").fold()).unfold().
  project("from","to","edges","total","number").
    by(select(keys).select("n")).
    by(select(keys).select("m")).
    by(select(values)).
    by(select(values).unfold().values('weight').sum()).
    by(select(values).count(local)).
  where(select("number").is(gt(1))).
  order().by(select("number"), decr).
  limit(10)


Robert Dale

Marko Rodriguez

unread,
Mar 8, 2017, 10:13:11 AM3/8/17
to gremli...@googlegroups.com
Geez. Gremlin is clunky here. Is there a more high-level/generally-useful step we could introduce that accomplishes the same effect with less effort?

Marko.
--
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.
To view this discussion on the web visit https://groups.google.com/d/msgid/gremlin-users/CABed_4pkK93gsQROLk8QMH0RwTx2%2B_rvKJzmAh9M0X6Q9ffiKw%40mail.gmail.com.

Daniel Kuppitz

unread,
Mar 8, 2017, 3:33:03 PM3/8/17
to gremli...@googlegroups.com
group() with value projections would help. Something like:

group().
  by(select("n","m")).
    by("edges", select("r").fold()).
    by("number", select("r").count()).
    by("total", select("r").by("prop").sum())

Not sure if I really like that though.

Cheers,
Daniel


To unsubscribe from this group and stop receiving emails from it, send an email to gremlin-users+unsubscribe@googlegroups.com.

--
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-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/gremlin-users/E2978FF7-88C6-4965-B211-DDA53B17621E%40gmail.com.

SS

unread,
Mar 10, 2017, 6:22:16 AM3/10/17
to Gremlin-users
Thanks a lot everybody. Robert the query gives the desired result.

Thanks
SS
Reply all
Reply to author
Forward
0 new messages