From whiteboard to world stage, Neo4j works the way you think.
Neo4j has a trio of programming interfaces, focused on different kinds of interaction.
As always, pick the right tool for the job.
Neo4j's Cypher language is purpose built for working with graph data. Inspired by SQL syntax, embracing pattern matching for describing paths, Cypher is the primary tool for building graph applications.
Let's use Cypher to generate the graph described in the Learn Guide. Starting with Emil:
CREATE (ee { name: "Emil", from: "Sweden" }) RETURN ee.name;
CREATE
clause to create data()
parenthesis to indicate a nodeee
a variable for the new node{}
brackets to add properties to the nodeRETURN
clause indicates what data to returnee.name
return the name property from the 'ee' nodeNow find the node representing Emil:
START ee=node(*) WHERE ee.name! = "Emil" RETURN ee;
START
clause to begin a queryee=node(*)
to search through all nodesWHERE
clause to constrain the resultsee.name!
indicates the name property must exist= "Emil"
compares an existing name to the value EmilRETURN
clause requests particular results
CREATE
clauses can create many nodes and relationships at once.
CREATE (ee { name: "Emil", from: "Sweden", klout: 99 }), (js { name: "Johan", from: "Sweden", learn: "surfing" }), (ir { name: "Ian", from: "England", title: "author" }), (rvb { name: "Rik", from: "Belgium", pet: "Orval" }), (ally { name: "Allison", from: "California", hobby: "surfing" }), (ee)-[:KNOWS {since: 2001}]->(js),(ee)-[:KNOWS {rating: 5}]->(ir), (js)-[:KNOWS]->(ir),(js)-[:KNOWS]->(rvb), (ir)-[:KNOWS]->(js),(ir)-[:KNOWS]->(ally), (rvb)-[:KNOWS]->(ally);
Cypher uses patterns to describe how to find things in the graph. For instance, to find Emil's friends:
START ee=node(*) MATCH (ee)-[:KNOWS]->(friends) WHERE ee.name! = "Emil" RETURN friends;
MATCH
clause to describe the pattern from known Nodes to found Nodes(ee)
starts the pattern with Emil-[:KNOWS]->
matches outgoing "KNOWS" relationships(friends)
will be bound to Emil's friendsPattern matching can be used to make recommendations. Johan is learning to surf, so he may want to find a new friend who already does:
START js=node(*) MATCH (js)-[:KNOWS]->()-[:KNOWS]->(surfer) WHERE js.name! = "Johan" AND surfer.hobby! = "surfing" RETURN DISTINCT surfer;
()
empty parenthesis to ignore these nodesDISTINCT
because more than one path will match the patternsurfer
will contain Allison, a friend of a friend who surfsOnce you've created a graph with Cypher, the REST interface can be useful for interactively walking the graph in a hypermedia way. This interface is also useful for checking system statistics and settings.
The web is a graph. Why not navigate a graph like browsing the web.
Cypher is an expressive and powerful language, intended to cover the vast majority of operations. When you have specialized needs, plugins allow you to "get under the hood."
With great power comes great responsibility.
Start your application using Cypher to create and query graph data. Use the REST API to monitor the database. In special cases, consider a plugin.
Now, go get started on your own application.
Next steps:
Or, see other guides.