Selecting vertexes that has more than one edge to a given set of vertexes

2,126 views
Skip to first unread message

Erik Bakstad

unread,
Mar 26, 2017, 1:02:09 PM3/26/17
to Gremlin-users
gremlin> graph = TinkerFactory.createModern()
gremlin> g = graph.traversal(standard())
gremlin> g.V().hasLabel("software").in()
==>v[1]
==>v[4]
==>v[6]
==>v[4]
gremlin> g.V().hasLabel("software").in().groupCount()
==>{v[1]=1, v[4]=2, v[6]=1}
gremlin> g.V().hasLabel("software").in().groupCount().unfold().filter(select(values).is(gt(1))).select(keys)
==>v[4]


I have a graph where I want to find vertexes that has more than one edge to a particular set of vertexes. In the simplified example above I basically want to select v[4] since it has two edges to vertexes of type Software.


g.V().hasLabel("software").in().groupCount().unfold().filter(select(values).is(gt(1))).select(keys)

The above solution works, but it seems a bit verbose. Is there a better way to do it?

Thanks!

Jean-Baptiste Musso

unread,
Mar 26, 2017, 6:14:10 PM3/26/17
to gremli...@googlegroups.com
You're probably looking for something like this:

g.V().hasLabel("software").where(__.in().count().is(gt(1)))


Jean-Baptiste

--
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/be1361ce-fc35-407a-9901-8424e01f58e3%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Robert Dale

unread,
Mar 26, 2017, 6:30:35 PM3/26/17
to gremli...@googlegroups.com
That selects vertices that have more than one vertex pointing at it.

Probably more like:

g.V().where(__.out().hasLabel('software').count().is(gt(1)))

But then some traversals may be more optimal depending on the dataset:

If you know you are starting with a person:

g.V().hasLabel('person').where(__.out().hasLabel('software').count().is(gt(1)))

If you can rely on the edge label and don't need the vertex label:

g.V().hasLabel('person').where(__.outE('created').count().is(gt(1)))



Robert Dale

Erik Bakstad

unread,
Mar 27, 2017, 3:34:26 AM3/27/17
to Gremlin-users
Thanks for the answers. I see that I probably simplified this too much. The problem I have is that I have a set of vertices, and given that set, I want to find the vertices that has more than one inE to that set. So more like this:

g.V().and(
  hasLabel("FooBar"),
  has("someProperty", eq("SomeValue")),
  outE("Some additonal edge that helps defines the set"))
  .in().hasLabel("RecordType").groupCount()
  .unfold()
  .filter(select(values).is(gt(1))).select(keys)

I tried out some variations of your proposed solutions like:

g.V().and(...).in().where(__.hasLabel("RecordType").count().is(gt(1)))

and 

g.V().and(...).inE().where(__.outV().count().is(gt(1)))

But those all returned an empty result.

When I run

g.V().and(
  hasLabel("FooBar"),
  has("someProperty", eq("SomeValue")),
  outE("Some additonal edge that helps defines the set"))
  .in()

It returns something like this:
v[4]
v[4]
v[3]

So that means that v[4] has more than one edge to something in the set, and I'm trying to limit the results to only those with multiple edges to the set (i.e. v[4] in this example).

Thank you all for the help so far, makes getting started with Gremlin a lot easier!

- Erik

Robert Dale

Jean-Baptiste

To unsubscribe from this group and stop receiving emails from it, send an email to gremlin-user...@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-user...@googlegroups.com.

Daniel Kuppitz

unread,
Mar 27, 2017, 6:45:52 AM3/27/17
to gremli...@googlegroups.com
The problem I have is that I have a set of vertices, and given that set, ...

s = g.V(4,5,6).toSet()

I want to find the vertices that has more than one inE to that set.

g.V().filter(__.in().is(within(s)).count().is(gt(1)))

Is this a good solution? No, it's just 1:1 translation of your description, that ends up doing a full scan. We can use a different approach: Start at one vertex in the given set, traverse into the out direction and then check if there's another vertex from the set found in the in direction.

g.V(s).as("s").out().filter(__.in().where(neq("s")).is(within(s)))

Cheers,
Daniel




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/fea2ad9f-e114-4a38-bc66-4dcc62a7f4ae%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages