You are previewing Learning Cypher.
O'Reilly logo
Learning Cypher

Book Description

Write powerful and efficient queries for Neo4j with Cypher, its official query language

In Detail

Neo4j has been generating much interest among NoSQL database users, but utilizing Neo4j in a real-world project can be difficult compared to a traditional relational database. Cypher, a relatively simple but powerful language, allows you to focus on your domain instead of getting lost in the world of database access. As you will learn in this book, very complicated database queries can easily be expressed through Cypher.

This book is a practical, hands-on guide to designing, implementing, and querying a Neo4j database quickly and painlessly. Through a number of practical examples, this book uncovers all the behaviors that will help you to take advantage of Neo4j effectively, with tips and tricks to help you along the way. The book starts with basic clauses and patterns to perform read-only queries with Cypher. You will then learn about clauses and get some tips on using patterns to elaborate results coming from pattern matching. Next, you will master the clauses required to modify a graph. To finish off, the book covers Cypher operators and functions in detail.

What You Will Learn

  • Design and create flexible and fast graph databases using the Cypher declarative syntax
  • Write powerful, readable, and reusable queries with pattern matching and parameters
  • Develop fast applications using best practices to improve the performance of your Cypher queries
  • Transition smoothly from SQL to Neo4j
  • Migrate relational databases to the graph model, getting rid of O/R mismatch
  • Avoid the common mistakes and pitfalls in programming with Neo4j
  • Downloading the example code for this book. You can download the example code files for all Packt books you have purchased from your account at If you purchased this book elsewhere, you can visit and register to have the files e-mailed directly to you.

    Table of Contents

    1. Learning Cypher
      1. Table of Contents
      2. Learning Cypher
      3. Credits
      4. About the Author
      5. About the Reviewers
        1. Support files, eBooks, discount offers, and more
          1. Why subscribe?
          2. Free access for Packt account holders
      7. Preface
        1. What this book covers
        2. What you need for this book
        3. Who this book is for
        4. Conventions
        5. Reader feedback
        6. Customer support
          1. Downloading the example code
          2. Errata
          3. Piracy
          4. Questions
      8. 1. Querying Neo4j Effectively with Pattern Matching
        1. Setting up a new Neo4j database
          1. Neo4j running modes
            1. Neo4j Server
            2. An embedded database
              1. Preparing the development environment
              2. Creating an embedded database
              3. Configuration
        2. HR management tool – an example
          1. Creating nodes and relationships using the Java API
          2. A querying database
            1. Invoking Cypher from Java
            2. Finding nodes by relationships
            3. Filtering properties
            4. Filtering relationships
            5. Dealing with missing parts
            6. Working with paths
            7. Node IDs as starting points
          3. Query parameters
            1. Passing parameters with Java
        3. Summary
      9. 2. Filter, Aggregate, and Combine Results
        1. Filtering
          1. The book store – an example
          2. Text search
            1. Working with regular expressions
            2. Escaping the text
          3. Value comparisons
            1. The IN predicate
            2. Boolean operators
          4. Working with collections
          5. Paging results – LIMIT and SKIP
        2. Sorting
          1. A descending sort
          2. Dealing with null values using the COALESCE function
        3. Aggregating results
          1. Counting matching rows or non-null values
          2. Summation
          3. Average
          4. Maximum and minimum
          5. Standard deviation
          6. Collecting values in an array
          7. Grouping keys
          8. Conditional expressions
          9. Separating query parts using WITH
        4. The UNION statement
        5. Summary
      10. 3. Manipulating the Database
        1. Using Neo4j Browser
        2. Creating nodes and relationships
          1. Labels and properties
            1. Multiple labels
            2. Properties
            3. Creating multiple patterns
          2. Creating relationships
            1. Creating full paths
            2. Creating relationships between existing nodes using read-and-write queries
        3. Modifying existing data
          1. Creating unique patterns
            1. Complex patterns
          2. Setting properties and labels
            1. Cloning a node
            2. Adding labels to nodes
          3. Merging matched patterns
            1. Idempotent queries
        4. Deleting data
          1. Removing labels
          2. Removing properties
          3. Deleting nodes and relations
          4. Clearing the whole database
        5. Loops
          1. Working with collections
        6. Summary
      11. 4. Improving Performance
        1. Performance issues
        2. Best practices and recommendations
          1. Using parameterized queries
            1. Parameterized queries with the REST API
          2. Reusing ExecutionEngine
          3. Finding the optimum transaction size
          4. Avoiding unnecessary clauses
          5. Specifying the direction of relationships and variable length paths
        3. Profiling queries
          1. Profiling using the Java API
          2. Inside the execution plan description
          3. Profiling with Neo4j Shell
          4. Profiling with the REST API
        4. Indexes and constraints
          1. SCAN hints
          2. Index hints
          3. Constraints
        5. Summary
      12. 5. Migrating from SQL
        1. Our example
        2. Migrating the schema
          1. Labels
          2. Indexes and constraints
          3. Relationships
        3. Migrating the data
          1. Entities
          2. Relationships
        4. Migrating queries
          1. CRUD
          2. Searching queries
          3. Grouping queries
        5. Summary
      13. A. Operators and Functions
        1. Operators
          1. Comparison operators
            1. Ordering operators
            2. Equality operators
            3. NULL equality operators
          2. Mathematical operators
          3. The concatenation operator
          4. The IN operator
          5. Regular expressions
        2. Functions
          1. COALESCE
          2. TIMESTAMP
          3. ID
          4. Working with nodes
            1. NODES
            2. LABELS
          5. Working with paths and relationships
            1. TYPE
            2. ENDNODE and STARTNODE
            4. RELATIONSHIPS
          6. Working with collections
            1. HEAD, TAIL, and LAST
            2. LENGTH
            3. EXTRACT
            4. FILTER
            5. REDUCE
            6. RANGE
          7. Working with strings
            1. SUBSTRING, LEFT, and RIGHT
            2. STR
            3. REPLACE
            4. Trimming functions
            5. LOWER and UPPER
          8. Aggregation functions
            1. COUNT
            2. SUM
            3. AVG
            5. STDEV and STDEVP
            6. MIN and MAX
          9. Mathematical functions
      14. Index