Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
All Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
timer SALE ENDS IN
0 Days
:
00 Hours
:
00 Minutes
:
00 Seconds
Arrow up icon
GO TO TOP
Learning Neo4j

You're reading from   Learning Neo4j Run blazingly fast queries on complex graph datasets with the power of the Neo4j graph database

Arrow left icon
Product type Paperback
Published in Aug 2014
Publisher
ISBN-13 9781849517164
Length 222 pages
Edition 1st Edition
Languages
Tools
Concepts
Arrow right icon
Author (1):
Arrow left icon
 Van Bruggen Van Bruggen
Author Profile Icon Van Bruggen
Van Bruggen
Arrow right icon
View More author details
Toc

Table of Contents (18) Chapters Close

Learning Neo4j
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
1. Graphs and Graph Theory – an Introduction FREE CHAPTER 2. Graph Databases – Overview 3. Getting Started with Neo4j 4. Modeling Data for Neo4j 5. Importing Data into Neo4j 6. Use Case Example – Recommendations 7. Use Case Example – Impact Analysis and Simulation 8. Visualizations for Neo4j 9. Other Tools Related to Neo4j Where to Find More Information Related to Neo4j Getting Started with Cypher Index

Key operative words in Cypher


Like every database query language, there are a few operative words that have an important meaning in the composition of every query. It's useful for you to know these since you will be using them to compose your specific queries on your specific datasets.

Keyword

Function

Example

MATCH

This describes a pattern that the database should match. This is probably the most important piece of the query as it is a structural component that always starts your queries.

MATCH (me:Person)-[:KNOWS]->(friend)

WHERE

This filters results that are found in the match for specific criteria.

WHERE me.name = "My Name" AND me.age > 18

RETURN

This returns results. You can either return paths, nodes, relationships, or their properties—or an aggregate of the mentioned parameters. This is another structural component, as all read queries and most write queries will return some data.

RETURN me.name, collect(friend), count(*) as friends

WITH

This passes results from one query part to the next. Much like RETURN, but instead of including data in the result set, it will be passed to the following part of the query. It transforms and aggregates results and also separates READ and UPDATE statements.

 

ORDER BY

SKIP

LIMIT:

This sorts and paginates the results.

ORDER BY friends DESC SKIP 10 LIMIT 10

CREATE

This creates nodes and relationships with their properties.

CREATE (p:Person), (p)-[:KNOWS {since: 2010}]-> (me:Person {name:"My Name"})

CREATE UNIQUE

This fixes graph structures by only creating structures if they do not yet exist.

 

MERGE

This matches or creates semantics by using indexes and locks. You can specify different operations in case of a MATCH (part of the pattern already existed) or on CREATE (pattern did not exist yet).

MERGE (me:Person {name:"My Name"})

ON MATCH me SET me.accessed = timestamp()

ON CREATE me SET me.age = 42

SET, REMOVE

This updates properties and labels on nodes and/or relationships.

SET me.age = 42SET me:Employee REMOVE me.first_name REMOVE me:Contractor

DELETE

It deletes nodes and relationships.

MATCH (me)

OPTIONAL MATCH (me)-[r]-() DELETE me, r

With these simple keywords, you should be able to start forming your first Cypher queries. After all, it's a bit like ASCII art, a structure similar to the one shown in the following diagram:

This is very easily described in Cypher as:

(a:Person {name:"Rik")–[:OWNS]–>(b:Device {brand:"LIFX"})

All we need to do to make this a proper Cypher statement is to wrap it in MATCH and RETURN statements:

Match
(a:Person {name:"Rik")–[r:OWNS]–>(b:Device {brand:"LIFX"})
return a,r,b;

This is just a simple example of how you would start using Cypher. More complex examples can of course be found elsewhere in this book. You can also find the complete Cypher Ref Card (online version at http://docs.neo4j.org/refcard/2.1/) included in the final pages of this book.

lock icon The rest of the chapter is locked
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
Visually different images