[Neo4j] Gremlin help

Nuo Yan yan.nuo at gmail.com
Fri Oct 21 22:38:30 CEST 2011

Hi Marko and Gremlin gurus:

Currently I'm doing the following in my own code with multiple requests to
the standalone neo4j server. I wonder if it's possible to achieve in one
gremlin query/script so that I can post the gremlin query to the server as 1
request and done. What I'm trying to achieve is:

Start from one given node (e.g. v1), get all of the nodes connected through
a given type of relationship (e.g. relationship "foo"), within all of these
nodes, see if their "name" property has the same value, and if so, delete
the node (and the "foo" relationship connected to it) with smaller outgoing
degree (on a specific type of relationship, say, "bar"). If there are more
than two nodes with the same "name" property, only keep the one with biggest
outgoing degree (on type "bar").

For example, for the following graph:

v1 --foo--> v2("name" => "abc") --"bar"--> (15 nodes)
v1 --foo--> v3("name" => "abc") --"bar"--> (5 nodes)
v1 --foo--> v4("name" => "abc") --"bar"--> (8 nodes)
v1 --foo--> v5("name" => "xyz")--"bar"-->(16 nodes)
v1 --foo--> v6("name" => "abc")--"not_bar"--> (20 nodes)

Ideally, after running the gremlin script, it should be:

v1 --foo--> v2("name" => "abc") --"bar"--> (15 nodes)
v1 --foo--> v5("name" => "xyz")--"bar"-->(16 nodes)
v1 --foo--> v6("name" => "abc")--"not_bar"--> (20 nodes)

with v3 and v4 (and the "foo" relationships connecting them to v1) deleted
because they have the same "name" attributes with v2 but a smaller degree
with outgoing "bar" relationship.

It this possible to achieve relatively easily with Gremlin?

More information about the User mailing list