Gremlin is a domain specific language for traversing property graphs. Neo4j is one of the databases that can speak the gremlin language, and as promised I’ll show you how you can use it to implement friend recommendations as well as degrees of separation.
We can send any gremlin script to Neo4j via the REST API and neography using the execute_script command. Let’s implement suggestions_for so it sends a gremlin script to the server:
def suggestions_for(node) node_id = node["self"].split('/').last.to_i @neo.execute_script("g.v(node_id). in('friends'). in('friends'). dedup. filter{it != g.v(node_id)}. name", {:node_id => node_id}) end puts "Johnathan should become friends with #{suggestions_for(johnathan).join(', ')}" # RESULT # Johnathan should become friends with Mary, Phil