Cypher & Cypher Queries

Shivam Bhatt
5 min readApr 25, 2023

Introduction

Neo4j and other graph databases are getting more and more well-liked as a result of their capacity to store and handle enormous and intricate data structures. In particular, working with graph data and performing complex operations on it is made simple by Neo4j’s query language, Cypher. We will examine some of the fundamental Neo4j Cypher queries in this blog article.

Cypher’s ability to perform complicated queries with ease is one of its primary advantages. The language provides a diverse set of operators, functions, and aggregation functions for performing complicated computations and manipulations on graph data. For example, you may use Cypher to discover the shortest path between two nodes, calculate the average value of a property across all nodes, or count the number of nodes that fulfil particular criteria.

Making Nodes and Connections

One of the fundamental operations available in Neo4j is the creation of nodes and associations. To build a node with the label “Person” and the values “name” and “age,” use the following query.

CREATE (p:Person {name: 'John Doe', age: 30})

A node with the label “Person” and the values “name” and “age” is created by the aforementioned query. The following expression can be used to establish a connection between two nodes:

MATCH (a:Person), (b: Person)
WHERE a.name ='John Doe' AND b.name = 'Jane Doe'
CREATE (a) - [:FRIENDS] -> (b)

The aforementioned query establishes a relationship between two nodes with the labels “Person” and gives the relationship the attribute “FRIENDS”.

Getting Nodes and Relationships Back

Another crucial Neo4j activity is retrieving nodes and associations. To find every node with the label “Person,” perform the following search:

MATCH (p: Person)
RETURN p

The aforementioned search retrieves all nodes with the label “Person”. We may use the following search to find every connection between nodes with the label “Person”:

MATCH (a:Person) - [r]->(b: Person)
RETURN r

The aforementioned query retrieves all connections between nodes that have the label “Person” in them.

Node and relationship updates

Another crucial function in Neo4j is updating nodes and connections. A node’s property can be updated by running the following query:

MATCH ( p:Person {name: 'John Doe'})
SET p.age = 40
RETURN p

The aforementioned query modifies the node with the name “John Doe”’s age attribute. Use the following formula to update a relationship property:

MATCH ( a: Person)-[r :FRIENDS]->(b:Person)
SET r.since = '2020'
RETURN r

The aforementioned query modifies the relationship’s since attribute for the ‘FRIENDS’ relationship type. Nodes and relationships can be deleted. Another crucial Neo4j action is the deletion of nodes and links. To remove a node with the label “Person” and the property “name” equal to “John Doe,” use the following query:

MATCH (p:Person {name: 'John Doe'})
DELETE p

The aforementioned query eliminates the node that has the property “name” equal to “John Doe” and the label “Person.” Using the following formula, we may remove a relationship:

MATCH (a:Person)-[r:FRIENDS]->(b: Person)
DELETE r

The aforementioned query removes the friendship association between two nodes with the label “Person” between them.

Finding the Shortest Paths in Graphs using Cypher

Cypher also supports a wide range of graph-specific operations and functions, such as pathfinding algorithms, node and relationship creation and deletion, and traversal algorithms. For example, the following Cypher query finds the shortest path between two nodes with the labels “Person” and “Movie”, and returns the nodes and relationships that form that path:

MATCH path = shortestPath((p:Person)-[*]-(m:Movie))
WHERE p.name = 'Tom Hanks' AND m.title = 'The Matrix'
RETURN path

In this query, the shortestPath function is used to find the shortest path between the two nodes that match the pattern (p:Person)-[*]-(m:Movie). The WHERE clause imposes extra limitations on those nodes (in this example, that the person node’s name must be “Tom Hanks” and the movie node’s title must be “The Matrix”). Finally, the RETURN clause indicates what data (in this example, the nodes and relationships that constitute the shortest path) should be returned.

The shortest pathways between two nodes in a graph may be found using Cypher in addition to asking for specific nodes and connections. Finding the shortest path between two ideas in a knowledge network or the fastest path between two places on a map are just two examples of the many uses for this.

Use Cypher’s shortestPath() method to determine the shortest route between any two nodes in a Neo4j network. Using the labels “Person” and a certain name property value, the following example search seeks the shortest path between two nodes:

MATCH (p1:Person {name: 'Alice '}), (p2:PersoN {name: 'Bob'7)
MATCH path = shortestPath((p1)-[*]-(p2))
RETURN path

In this query, the nodes with the labels “Person” and the provided name property values are first located using MATCH clauses. The shortest path between these two nodes is then determined using the shortestPath() method, and it is saved in the path variable. The shortest path is then returned as a series of nodes and relationships in the path variable.

Notably, the shortestPath() method employs a heuristic technique rather than fully investigating all potential pathways to rapidly provide an approximation of the shortest path. Even though it can miss some real shortest paths, this method can be much quicker than an exhaustive search, especially for big graphs.

Overall, finding the shortest pathways in Neo4j graphs using Cypher can be a useful method strong tool that serves as an example of Neo4j’s Cypher query language’s adaptability and flexibility.

Another key characteristic of Cypher is its transaction support. Transactions enable you to concatenate a series of operations into a single unit of work that may be done atomically. This signifies that either all or none of the transactions’ activities will succeed. Transactions are critical in a graph database for assuring data integrity and consistency, especially in a multi-user scenario where different users may access and edit the same data at the same time.

Conclusion

Finally, Cypher is a strong and user-friendly query language that makes working with graph data in Neo4j simple. Neo4j is a growingly popular option for businesses that work with graph data because to its capacity to store and handle enormous and complicated data structures. Developers and data scientists may gain useful insights from their graph data and enhance their decision-making skills by utilising Cypher queries. It provides a wide range of operations and functions that make complicated queries and manipulations on graph data straightforward. Cypher is also meant to be simple to use and understand, having a basic and straightforward syntax based on pattern matching. Cypher provides a simple and powerful way to interact with your data and get the most out of your graph database, whether it is little or vast.

--

--