JohnKoz
JohnKoz

Reputation: 978

Find all vertices with no out edges

I'm new to Gremlin and I can't figure out a simple query which will return all vertices of my graph which do not have any edges (ie: orphaned Vertex). Ideally I'd like those without any 'out' edge.

I've been reading and some questions/articles say I can interpret an out edge as a property, but that didn't work for me either. I've been looking at hasNot and filtering.

Any ideas?

Thanks

-John

Upvotes: 0

Views: 1135

Answers (2)

Kelvin Lawrence
Kelvin Lawrence

Reputation: 14371

You can simply do this:

g.V().not(outE())

Or if you want to find total orphans:

g.V().not(bothE())

Upvotes: 3

Jayanta Mondal
Jayanta Mondal

Reputation: 456

Try this: g.V().as('a').where(out().count().is(0)).select('a')

But, depending on how many vertices you have, you can run into request rate too large exception (aka 429).

To avoid that you can do the query in ranges, if you know the id ranges of the vertices, or it can be some other property ranges. An id range based example is below:

g.V().has('id', gt(0)).has('id', lt(100)).as('a').where(out().count().is(0)).select('a')
g.V().has('id', gt(99)).has('id', lt(200)).as('a').where(out().count().is(0)).select('a')
....

and so on

Upvotes: 0

Related Questions