This site is not optimized for Internet Explorer 9 and lower. Please choose another browser or upgrade your existing browser in order get the best experience of this website.

Cypher is Awesome

January 20, 2016

Neo4j Cypher graph database query shortest pathCypher is a declarative pattern matching language created by Neo4j for the purposes of describing graph data representations effectively. Cypher is considered to be one of the most powerful features fore effectively expressing graph database traversals for reading and writing graph database data into Neo4j.

Cypher makes it capable for queries to do something like: “bring back my friends’ friends right now” or “give me back all pages this page is linked to within the last day” in the form of several code lines. As such, graph database queries and operations across all languages and integrations with Neo4j are able to query in a consistent manner.

The reception to Cypher has been so great that Neo4j launched the OpenCypher initiative to make Cypher the SQL for graph databases. The organizations that are joining OpenCypher are very important to the graph database movement because supporting a common graph database query language means there will be more utilization and commonality across graph database implementations.

Cypher Query Syntax

Cypher looks a lot like ASCII art because it uses textual pattern representations to express nodes and relationships. The nodes are surrounded with parenthesis which appear like circles and the relationships consist of dashes with square brackets. Here’s an example: (graphs)-[:ARE]-(everywhere)

If we want to find all people and their preferences, the query will involve identifiers for the person and thing. A pattern like “(person)-[:LIKES]->(thing)” could be utilized so it can be referred to later say, for instance, to gain access to properties such as “” and “thing.title.”

Writing and representing depth based queries is one place where cypher makes it really easy to do. To look at my friends of friends is as simple as “(me)-[:FRIEND*2]->(fof). It’s actually fun to create queries because its comprehensive nature follows direct patterns and relationships without having to think about what needs joining together to aggregate the required data.

Advantages of Neo4j with Cypher

In a traditional SQL DB environment, the queries mentioned above could involve a lot of code and memory for calculations. But with the native graph database, Neo4j, and Cypher, it’s easier to achieve a minimum code and optimized performance at the same time.

If you dig deeper into the workings of the Neo4j native graph database and computer systems on which it runs, you’ll find that Neo4j is a highly optimized product that is very system-friendly.

With Neo4j, nodes and relationship are housed in divided files. Every node is housed as a “constant length” record with a pointer to its initial property and initial relationship. And every relationship is also stored as a constant length with a pointer to its last and next relationship. Such constant length record promotes for a faster look-up.

Layering Cypher on top of any graph database from organizations that join the OpenCypher movement will be an advantage for representing the graph database models, but layering on a native graph database, like Neo4j, will provide an even greater advantage thanks to completely natural pairing of native graph database storage and a native graph database query language like Cypher.