DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Data Engineering
  3. Databases
  4. Using Cypher with Neography

Using Cypher with Neography

Max De Marzi user avatar by
Max De Marzi
·
Feb. 12, 12 · Interview
Like (0)
Save
Tweet
Share
4.25K Views

Join the DZone community and get the full member experience.

Join For Free

Cypher is the query language of Neo4j, 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 cypher query to Neo4j via the REST API and neography using the execute_query command. Let’s implement suggestions_for so it sends a cypher query to the server:

	def suggestions_for(node)
	  node_id = node["self"].split('/').last
	  @neo.execute_query("START me = node({node_id})
	                      MATCH (me)-[:friends]->(friend)-[:friends]->(foaf)
	                      RETURN foaf.name", {:node_id => node_id})["data"]
	end
	 
	puts "Johnathan should become friends with #{suggestions_for(johnathan).join(', ')}"
	 
	# RESULT
	# Johnathan should become friends with Mary, Phil

Let’s go through the query:

	START me = node({node_id})

  is the place in the graph where we will begin. We are naming this node “me” and will reference it later. The

	{node_id}

part defines a parameter which we will fill in later. Like before, we want to find nodes that are two friends relationships away

	MATCH (me)-[:friends]->(friend)-[:friends]->(foaf)

  and we want to return the name of these friends of a friend which we are labeling “foaf”, skipping over our known friends which were labeled “friend”

	RETURN foaf.name

  We then pass in our parameter

	{:node_id => node_id}

This last bit just grabs the name data, ignoring the names of the columns which are also returned 

	["data"]

 How about degrees of separation?

	def degrees_of_separation(start_node, destination_node)
	  start_node_id = start_node["self"].split('/').last
	  destination_node_id = destination_node["self"].split('/').last
	  @neo.execute_query("START me=node({start_node_id}),
	                            them=node({destination_node_id})
	                      MATCH path = allShortestPaths( me-[?*]->them )
	                      RETURN length(path), extract(person in nodes(path) : person.name)",
	                      {:start_node_id => start_node_id,
	                       :destination_node_id => destination_node_id })["data"]
	end
	 
	 
	degrees_of_separation(johnathan, mary).each do |path|
	  nodes = path.last[1..(path.last.size - 2)].split(", ")
	  puts "#{path.first} degrees: " + nodes.join(' => friends => ')
	end
	 
	# RESULT
	# 2 degrees: Johnathan => friends => Mark => friends => Mary

We are once again using some of the built in graph functions of Neo4j. In this case we are using allShortestPaths. We start with “me” as the start node and “them” as the destination node

	START me=node({start_node_id}),
	      them=node({destination_node_id})

  We want to match any relationship between me and them, and get the shortest paths

MATCH path = allShortestPaths( me-[?*]->them )
	MATCH path = allShortestPaths( me-[?*]->them )

then we want to return the length of the paths and the names of the nodes. We use the extract function to pull node data from the paths. You can think of extract like map or collect in Ruby 

	RETURN length(path), extract(person in nodes(path) : person.name)

  We pass in our parameters

	{:start_node_id => start_node_id,
	 :destination_node_id => destination_node_id } 

  and we just want the data returned, ignoring the column names

	["data"]

We do a bit of string wrangling at the end to get the format we want and that’s all there is too it.

Cypher is a fairly new language and is undergoing constant additions and improvements. Contribute and stay up to date by checking the Neo4j Google Group.

 Source: http://maxdemarzi.com/2012/01/07/cypher-with-neography/

 

 

Database Neo4j Query language Data (computing) Pass (software) Extract Graph (Unix) Strings Google (verb) Web Protocols

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Simulating and Troubleshooting BLOCKED Threads in Kotlin [Video]
  • How To Select Multiple Checkboxes in Selenium WebDriver Using Java
  • All the Cloud’s a Stage and All the WebAssembly Modules Merely Actors
  • Collaborative Development of New Features With Microservices

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: