Search icon
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletters
Free Learning
Arrow right icon
Graph Data Processing with Cypher

You're reading from  Graph Data Processing with Cypher

Product type Book
Published in Dec 2022
Publisher Packt
ISBN-13 9781804611074
Pages 332 pages
Edition 1st Edition
Languages
Concepts
Author (1):
Ravindranatha Anthapu Ravindranatha Anthapu
Profile icon Ravindranatha Anthapu

Table of Contents (18) Chapters

Preface Part 1: Cypher Introduction
Chapter 1: Introduction to Neo4j and Cypher Chapter 2: Components of Cypher Part 2: Working with Cypher
Chapter 3: Loading Data with Cypher Chapter 4: Querying Graph Chapter 5: Filtering, Sorting, and Aggregations Chapter 6: List Expressions, UNION, and Subqueries Part 3: Advanced Cypher Concepts
Chapter 7: Working with Lists and Maps Chapter 8: Advanced Query Patterns Chapter 9: Query Tuning Chapter 10: Using APOC Utilities Chapter 11: Cypher Ecosystem Chapter 12: Tips and Tricks Index Other Books You May Enjoy

Components of Cypher

Cypher is a declarative query language that makes it easy to write expressive and efficient queries to traverse, update, and administer a graph database. It provides a powerful way to express even complex graph traversals more simply so that you can concentrate on the domain aspects of your query instead of worrying about syntax.

Cypher expands upon a variety of established practices for querying. For example, the WHERE, ORDER BY, and CASE keywords are inspired by SQL syntax. Some of the list semantics are borrowed from Haskell and Python.

This chapter introduces the Cypher syntax with some examples. We will review the important aspects of the Cypher syntax and semantics in building graph traversal queries. We will discuss important keywords and the role they play in building the queries. We will take a look at the graph data model and how Cypher queries follow the data connections.

In this chapter, we will cover these aspects:

  • Graph storage in...

Technical requirements

We should have Neo4j Desktop installed and have created a local instance. There are no special technical skills required to follow the concepts discussed in this chapter. Having knowledge of SQL can be useful in understanding Cypher, but is not required.

Graph storage in Neo4j

Before we look into the Cypher syntax to query the data, it is important to understand how the data is persisted as a graph. Data diagram representations give a good idea about how the Cypher queries can be written using the data model. We will take a look at a data diagram and see how it helps us with querying:

Figure 2.1 – Sample graph data diagram

This diagram shows how the data is stored in the database. Each node represents one entity that knows what relationship entities it is connected to and whether they are incoming or outgoing; each property is an entity that is associated with a node or relationship. Each relationship entity knows what nodes it is connected to and the direction of the relationship.

The preceding diagram tells us that a person named Tom owns two addresses. This person lives at one address and rents the other one. A person named Shelley lives at an address that is rented. If you read this diagram from...

Using the Cypher syntax

Cypher is like American Standard Code for Information Interchange (ASCII) art. A simple Cypher traversal query can look like this:

(A)-[:LIKES]->(B), (B)-[:LIKES]->(C), (A)-[:LIKES]->(C)

This also can be written as follows:

(A)-[:LIKES]->(B)-[:LIKES]->(C)<-[:LIKES]-(A)

If you notice the syntax, it reads more like a simple statement. A likes B, who likes C, who is also liked by A. Nouns represent nodes and verbs represent relationships.

Cypher supports various data types, which fall into three different categories.

Property types

The following are the different property types available in Cypher:

  • Number:
    • Integer
    • Float
  • String
  • Boolean
  • Spatial:
    • Point
  • Temporal:
    • Date
    • Time
    • LocalTime
    • DateTime
    • LocalDateTime
    • Duration

Property types can have the following characteristics.

  • Can be a part of data returned by queries
  • Can be used as input parameters
  • Can be stored as properties on nodes or relationships...

Using the nodes syntax

In Cypher, a node is surrounded by parentheses, (), making it resemble a circle in a diagram. Here are some example usages in Cypher:

  • (p) – This represents a node identified with the p variable/alias. It can be of any type.
  • () – This represents a node that is not assigned a variable or an alias. This is normally called an anonymous node, as it cannot be referenced later, except as part of a path.
  • (:Person) – This represents a node with the Person label, but is not assigned to a variable or an alias.
  • (p:Person) – This represents a node with a Person label, identified with the p variable/alias.
  • (l:Location:Work) – This represents a node with multiple labels, Location and Work, identified with the l variable/alias.

Let’s move on to the relationships syntax.

Using the relationships syntax

In Cypher, a relationship can be represented using -->, which resembles an arrow on a diagram. Here are some example usages in Cypher:

  • (p)-[:LIVES_AT]->(a) – This represents that the node identified by p is connected to another node, a, with a LIVES_AT relationship type. The direction of the relationship is from p to a.
  • (p)-[r]->(a) – This represents that the node identified by p is connected to another node, a, with the direction of the relationship going from p to a. They can be connected via any relationship type. The relationship is assigned to the r variable/alias.
  • (p)<-[r]-(a) – This represents that the node identified by p is connected to another node, a, with the direction of the relationship going from a to p. The nodes can be connected via any relationship type and the relationship is assigned to the r variable/alias.
  • (p)-[r]-(a) – This represents that the node identified by p is connected...

Working with Cypher keywords

In this section, we will introduce the Cypher keywords and their syntax. Detailed usage of these keywords will be covered in upcoming sections of the book.

Let us start by using the MATCH and OPTIONAL MATCH keywords.

Using MATCH and OPTIONAL MATCH

The MATCH keyword allows you to specify the graph traversal patterns to find and return the data from Neo4j. It is most often coupled with a WHERE clause to filter out the results, and a RETURN clause to return the results.

  • This shows a basic MATCH query that will find all nodes in the database and return them. It will also return all the node properties shown as follows:
    MATCH (n)
    RETURN n
  • This query finds all the nodes that have the Movie label and returns the title property of that node. If there are no nodes with the Movie label, it does not return any results:
    MATCH (n:Movie)
    RETURN n.title
  • This query finds any node that has the title property, checks whether its value is My Movie, and...

Summary

In this chapter, we have covered these aspects: basic Cypher syntax, nodes syntax, relationships syntax, data types available in Cypher, keywords available in Cypher, working with indexes, working with Constraints, and working with full-text indexes.

By now, you should be aware of the basic Cypher aspects and should be able to build basic Cypher queries.

Certain advanced aspects, such as subqueries and so on, are covered in later chapters, and built-in functions are introduced as we learn more about Cypher.

You can find the latest Cypher documentation in the Neo4j Cypher Manual (https://neo4j.com/docs/cypher-manual/current/).

You can also find a quick reference guide at https://neo4j.com/docs/cypher-refcard/current/.

In the next chapter, we will start building graph models and using Cypher to start loading data. We will be leveraging the concepts learned in this chapter to achieve that.

lock icon The rest of the chapter is locked
You have been reading a chapter from
Graph Data Processing with Cypher
Published in: Dec 2022 Publisher: Packt ISBN-13: 9781804611074
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime}