Wolfgang Jagomir
Wolfgang Jagomir

Reputation: 37

Gremlin loop query that returns a list of neighbors' edge counts and ids

I'm trying to write a Gremlin loop that returns something like the following for a vertex:

nodeId:1, edgeCount: 2
nodeId:2, edgeCount: 1
nodeId:3, edgeCount: 3
nodeId:4, edgeCount: 4

In other words, a list of its neighbors' ids along with each neighbor's edge count. The closest I've got is this (for the vertex 'svpol'):

g.v('svpol').in.loop(1){it.loops<2}.path{it.id}{g.v(it.id).bothE.count()}

which produces the following output:

==>[svpol, 2]
==>[svpol, 4]
==>[svpol, 4]
==>[svpol, 6]

I have confirmed that the edge counts are correct, but instead of 'svpol' I would want it to produce the id of the neighbor. It needs to be done in a single query if possible.

Any ideas?

Upvotes: 2

Views: 1117

Answers (2)

Daniel Kuppitz
Daniel Kuppitz

Reputation: 10904

I guess Faber was close and what you're looking for is this:

g.v('svpol').both().path() {it} {[it.id, it.bothE().count()]}

Upvotes: 1

Faber
Faber

Reputation: 1552

Not sure if I understand your question correctly but you write:

a list of its neighbors' ids along with each neighbor's edge count

Such a list of lists could be obtained with the following simple query using transform (in gremlin 2):

l = g.v(id).both.transform{[it.id,it.bothE.count()]}.toList()

With the sample graph for id=1, the query will produce:

[3, 3]
[2, 1]
[4, 3]

I don't see why you want to use loop.

Upvotes: 1

Related Questions