[Neo4j] Gremlin help

Marko Rodriguez okrammarko at gmail.com
Sat Oct 22 18:40:16 CEST 2011


> 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").

The query below is to warm you up. It will delete all vertices with same property value as source vertex that are 'foo' related to source vertex. Given that you are mutating the graph, you will want to deal with transaction buffers so you don't do one transaction per mutations:

g.v(1).sideEffect{x = it.getProperty('name')}.out('foo').filter{it.getProperty('name').equals(x)}.sideEffect{g.removeVertex(it)}


To do the stuff with the smaller counts, etc. You can do:

g.v(1).sideEffect{x = it.getProperty('name')}.out('foo').filter{it.getProperty('name').equals(x)}.transform{[it, it.outE('bar').count()]}.filter{it[1] > 0}.aggregate.cap.next().sort{a,b -> b[1] <=> a[1]}.eachWithIndex{a,i -> if(i > 0) g.removeVertex(a[0])}

There you go! One big fatty Gremlin query to solve your problem. 

I would recommend going through each step and seeing what it returns so you understand what is going on.... Again, given that you are mutating the graph, be sure to be wise about transactions.



More information about the User mailing list