Download as pdf or txt
Download as pdf or txt
You are on page 1of 8

BROUGHT TO YOU BY:

203
Get More Refcardz! Visit Refcardz.com

» Getting Started with Neo4j

Querying Graphs with Neo4j


CONTENTS » Local Installation
» Neo4j In the Cloud
» Neo4j’s Query Language
» Performance Tips
By Michael Hunger
» And more...

What is a Graph Database? performance. Note that although they are directed,
relationships can always be navigated in both directions.
We live in a connected world. There are no isolated There is only one consistency rule in a graph database:
pieces of information, but rich, connected domains all “No broken links”. Since a relationship always has to have
around us. Only a database that embraces relationships a start and end node, you can only delete a node by also
as a core aspect of its data model is able to store, removing its associated relationships.
process, and query connections efficiently. While other
databases compute joins expensively at query time, a
graph database stores connections as first class citizens, What is Neo4j?
readily available for any “join-like” navigation operation.
Accessing those already persisted connections is an Neo4j is an open-source, NoSQL graph database
efficient, constant-time operation and allows you to implemented mainly in Java and Scala. Its development
traverse millions of relationships per second. started in 2003 and it has been sponsored by Neo
Independent of the size of the total dataset, graph Technology, Inc. since 2011. The source code and issue
databases excel at managing highly connected data and tracking is available on github.com/neo4j, with an active
complex queries. Armed only with a pattern and a set community supporting users on Stack Overflow and the
of starting points, graph databases explore the larger Neo4j Google Group.
neighborhoods around these initial entities — collecting Neo4j is used by hundreds of thousands of users in
and aggregating information from millions of nodes and almost all industries. Use cases include matchmaking,
relationships — but leaving the billions outside the search network management, impact analysis, software
perimeter untouched. analytics, scientific research, routing, organizational
The Property Graph Model and project management, content management,
recommendations, social networks, and more.
If you’ve ever worked with an object model or entity
relationship diagram, the labeled property graph model Neo4j Editions
will be familiar. Neo4j’s free Community Edition is a high-performance,
fully ACID-transactional database. It includes (but is not
Labeled Property Graph Data Model limited to) all the functionality described in this Refcard.

Neo4j’s Enterprise Edition adds scalable clustering, fail-


over, live backups, and comprehensive monitoring for
production use.

More information about the Community and Enterprise


editions is available at neo4j.com/product.
neo4j7
Edition

The property graph contains connected entities (the


nodes) which can hold any number of properties (key-
Graphs with

value-pairs). Nodes can be tagged with several labels Free Online Training
representing different roles in the domain. Besides

Learn Neo4j
putting a subset of node properties and relationships into
Java Enterprise

a certain context, labels also allow you to attach


metadata — like index or constraint information — to
nodes.
The World’s Leading
Relationships provide directed, named semantic Graph Database
connections between two nodes. A relationship always
has a direction, a type, a start node, and an end node.
Querying

Like nodes, relationships can have arbitrary properties.


Usually, relationships contain quantitative properties,
Start
such as weights, distances, ratings, time intervals, or Now
strengths. As relationships are stored efficiently in the
graph database, two nodes can have as many different or
similar relationships connecting them without sacrificing

© DZone, Inc. | dzone.com


2 Querying graphs with neo4j

Neo4j Server The Neo4j Browser: Getting Started

You can download Neo4j from neo4j.com/download. Neo4j Once you have Neo4j running (accessible at localhost:7474
Server can be installed and run on all operating systems. It or on a remote server), open the Neo4j Browser and check
provides an easy-to-use web interface at localhost:7474 out the Cypher Workbench. It provides guides to get you
started, links to the manual and a sample movie database.
The simplest way of getting started is to use Neo4j’s
database browser to execute your graph queries (written Neo4j Browser
in Cypher, the graph query language described in this
Refcard) in a workbench-like fashion. Results are rendered
as either intuitive graph visualizations or as easy-to-read
exportable tables.

Neo4j is a NoSQL
Graph Database
open source
welcoming UI your graph-query workbench
easy data modeling
readable queries Let’s add some data!
active community
In the sidebar on the left, open the Information tab (the
high performance
“i”), then choose the “Movie Graph App”. You’ll see a slide-
optional schema show explaining the dataset of Movie and Person nodes
connected via the ACTED_IN and DIRECTED relationships.

A remote Neo4j Server can be accessed via its Cypher On the second slide, a large chunk of Cypher code contains
HTTP API, either directly or through one of the many the statements to create the dataset. Click on it to transfer
available language drivers. For especially high performance the whole statement to the command line above.
use cases, you can add Neo4j Server extensions in any
Run the statement by hitting the “Execute” button on the
JVM language to access Neo4j’s internal database engine
top right. This inserts data into the database and renders a
directly without network overhead.
lonely node as a result.

Double click that lonely node and note how it expands


Getting Started with Neo4j into a star of other nodes, connected to the initial node by
relationships.
Local Installation Select any node and check the black property pop-up.
Prerequisites: Java 7 or greater Select its “eye” tab to customize node and relationship
sizes, captions and colors. Select the title property as the
• Visit neo4j.com/download and download Neo4j for caption for the movie nodes and name for the people.
your platform Continue to explore the graph visualization.

• On Windows, run the installer, choose a directory and For a simple query that returns a Person node with its
start the server associated Movies, run:
MATCH (p:Person {name:"Tom Hanks"})-->(m:Movie)
• On the other platforms, unzip the file and change to RETURN p,m
the directory in a terminal, e.g. ~/Download/neo4j-
community-2.1.5 In the next step we’re going to connect to the database.

• From the extracted directory, run bin/neo4j to start Connecting to Neo4j


the server, which should then be up and listening on Now that you have your server up and running, you can
http://localhost:7474 access it from any driver or just plain http. Enter :GET /
db/data/ into the browser console to see a representation
of the management API. You can see a “transaction” entry
Neo4j In the Cloud representing the transactional HTTP endpoint that you can
POST Cypher statements to. Assuming you have loaded the
In order to get started with Neo4j as a cloud hosted service, sample Movie dataset, try to execute:
visit neo4j.com/developer/guide-cloud-deployment and
choose a Neo4j Cloud Hosting provider that meets your :POST /db/data/transaction/commit
needs. Most have free offerings to get started and provide {"statements":[{"statement" :
provisioned Neo4j instances within minutes. "MATCH (m:Movie) RETURN m.title LIMIT {limit}",
"parameters":{"limit":10}}]}
With that simple request, you have effectively utilized the
http endpoint used by many Neo4j drivers!

© DZone, Inc. | dzone.com


3 Querying graphs with neo4j

Head over to neo4j.com/developer/language-guides to find Cypher Reference


a suitable development guide and driver for your language-
stack. Note: {value} denotes either literals, for ad hoc Cypher
queries; or parameters, for applications. Neo4j properties
You’ve taken a small yet vital step on the path to your own can be strings, numbers, or booleans, or arrays thereof.
Neo4j-powered application. With a basic understanding of
Cypher, you can start to build your applications. Read Query Structure
[[OPTIONAL] MATCH [WHERE]]
The next section introduces Neo4j's query language and
provides a reference for the most frequently used clauses [WITH [ORDER BY] [SKIP] [LIMIT]]
and operations. RETURN [ORDER BY] [SKIP] [LIMIT]

Cypher — Neo4j’s Query Language MATCH


Command Description
Introduction
MATCH Node patterns can contain
When representing graphs on a whiteboard, we draw nodes (n:Person)-[:KNOWS]->(m:Person) labels and are connected by
as circles and relationships as arrows. Cypher was born out WHERE n.name="Alice" relationships.
of graph patterns transformed from these two-dimensional Any pattern can be used in
whiteboard drawings into one-dimensional ASCII art. MATCH (n)-->(m)
MATCH.
Cypher denotes nodes with round parentheses: (a:Node),
MATCH
and relationships with labeled arrows: (n:Person {name:"Alice"})-->(m)
Patterns with node properties.
-[:A_RELATIONSHIP]->. Additional {key:value}
structures represent properties for both. MATCH p = (n)-->(m) Assign a path to p.
Optional pattern, NULLs will be
Patterns of nodes and relationships are the building blocks OPTIONAL MATCH (n)-[r]->(m)
used for missing parts.
for all Cypher queries.
As in most query languages, a Cypher statement is a series WHERE
of clauses. The simplest statement consists of a MATCH or Command Description
CREATE clause followed by a RETURN clause. Other clauses Use a predicate to filter.
are borrowed from other query languages (like SQL): WHERE, Note that WHERE is always part
ORDER BY, and LIMIT SKIP. of a MATCH, OPTIONAL MATCH,
WHERE node.property = {value} WITH or START clause.
More advanced statements use node-relationship patterns Putting it after a different
as predicates or expressions. Try the following two Cypher clause in a query will alter what
statements in your Neo4j Browser: it does.
CREATE (who:Person {name:"Me"})-[likes:LIKE]->
RETURN
(what:Graph:Database {name:"Neo4j"})
Command Description
RETURN who, likes, what;
Return the value of all named
RETURN *
identifiers.
RETURN n AS columnName Use alias for result column.
RETURN DISTINCT n Return unique rows.
ORDER BY n.property Sort the result.
Sort the result in descending
ORDER BY n.property DESC
order.
MATCH (:Person {name:"Me"})-[:LIKE]->(what) SKIP {skip_number} Skip a number of results.
RETURN what.name AS What, LIMIT {limit_number} Limit the number of results.
count(*) as times AS Times;
Skip results at the top and limit
SKIP {skip} LIMIT {limit}
what times the number of results.

Neo4j 1 The number of matching rows.


RETURN count(*)
See Aggregation for more.
If these clauses look familiar — especially if you’re a SQL
WITH
developer — that’s great! Cypher is intended to be familiar
enough to help you move rapidly along the learning curve. Command Description
At the same time, it’s tailored to query connected data in an WITH chains query parts. It
expressive but still easy-to-understand fashion. allows you to specify which
projection of your data is
MATCH (user)-[:KNOWS]-(friend)
For live graph models using Cypher, check the available after WITH.
WHERE user.name = {name}
Graph-Gists contributed by Neo4j users.
WITH user, count(*) AS friends You can also use ORDER BY,
The following Cypher language reference details everything SKIP, LIMIT and aggregation
you need to know about the language and should equip WHERE friends > 10
with WITH.
RETURN user
you to write Cypher queries that help you express your You might have to alias
intriguing business questions. expressions to give them a
name.

© DZone, Inc. | dzone.com


4 Querying graphs with neo4j

UNION SET
Command Description Command Description

MATCH (a)-[:KNOWS]->(b) Returns the distinct union of all SET


RETURN b.name query results. n.property1 = {value1}, Update or create a property
UNION Result column types and names n.property2 = {value2}
MATCH (a)-[:LOVES]->(b) have to match.
RETURN b.name Set all properties.
SET n = {map} This will remove any existing
MATCH (a)-[:KNOWS]->(b) properties.
RETURN b.name
Returns the union of all query Add and update properties, while
UNION ALL SET n += {map}
results, including duplicated rows. keeping existing ones.
MATCH (a)-[:LOVES]->(b)
RETURN b.name SET n:Person Adds a label Person to a node.

Write-Only Query Structure DELETE


(CREATE | MERGE)* Command Description
[SET|DELETE|REMOVE|FOREACH]*
[RETURN [ORDER BY] [SKIP] [LIMIT]] DELETE n, r Delete nodes and relationships.

Read-Write Query Structure REMOVE


[[OPTIONAL] MATCH WHERE]
Command Description
[WITH [ORDER BY] [SKIP] [LIMIT]]
(CREATE | MERGE)* REMOVE n:Person Remove a label from n.
[SET|DELETE|REMOVE|FOREACH]*
[RETURN [ORDER BY] [SKIP] [LIMIT]] REMOVE n.property Remove a property.

CREATE INDEX
Command Description
Command Description
CREATE Create a node with the given CREATE INDEX ON Create an index on the label
(n:Person {name: {value}}) properties. :Person(name) Person and property name.
Create a node with the given An index can be automatically
CREATE (n:Person {map})
properties. used for equality comparison.
MATCH (n:Person)
CREATE Create nodes with the given Note that for example
WHERE n.name = {value}
(n:Person {collOfMaps}) properties. lower(n.name) = {value}
will not use an index.
Create a relationship with the
CREATE (n)-[r:KNOWS]->(m) given type and direction; bind an MATCH (n:Person) An index can be automatically
identifier to it. WHERE n.name IN {values} used for the IN collection checks.

CREATE Create a relationship with Index usage can be enforced with


MATCH (n:Person)
(n)-[:LOVES since:{value}}] the given type, direction, and USING. E.g. when Cypher uses a
USING INDEX n:Person(name)
->(m) properties. suboptimal index or when more
WHERE n.name = {value}
than one index should be used.
MERGE Drop the index on the label Person
DROP INDEX ON :Person(name)
Command Description and property name.

MERGE CONSTRAINT
(n:Person {name: {value}}) Match pattern and create it if it
ON CREATE does not exist. Command Description
SET n.created=timestamp()
Create a unique constraint on the
ON MATCH Use ON CREATE and ON MATCH for
label Person and property name.
SET n.counter= conditional updates.
coalesce(n.counter,0)+1,
CREATE CONSTRAINT If any other node with the label
n.accessed=timestamp()
ON (p:Person) Person is updated or created
MATCH ASSERT p.name IS UNIQUE with a value for name that already
(a:Person {name:{value1}}), MERGE finds or creates a exists, the write operation will
(b:Person {name: {value2}}) relationship between the nodes. fail. This constraint will create an
MERGE (a)-[r:LOVES]->(b) accompanying index.

MATCH DROP CONSTRAINT Drop the unique constraint and


(a:Person {name: {value1}}) ON (p:Person) index on the label Person and
MERGE matches or creates whole ASSERT p.name IS UNIQUE property name.
MERGE
subgraphs attached to the node.
(a)-[r:KNOWS]->(b:Person
{name: {value2}})

© DZone, Inc. | dzone.com


5 Querying graphs with neo4j

Operators Labels
Type Operator Command Description

Mathematical +, -, *, /, %, ^ CREATE Create a node with label and


(n:Person {name:{value}}) property.
Comparison =, <>, <, >, <=, >=
MERGE Matches or creates unique node(s)
Boolean AND, OR, XOR, NOT (n:Person {name:{value}}) with label and unique property.

String + SET n:Spouse:Parent:Employee Add label(s) to a node.

Collection +, IN, [x], [x .. y] MATCH (n:Person) Matches nodes labeled as Person.

Regular Expression =~ MATCH (n:Person) Matches nodes labeled Person


WHERE n.name = {value} with the given name.
NULL WHERE (n:Person) Checks existence of label on node.

NULL is used to represent missing/undefined values.


• labels(n) Labels of the node.

NULL is not equal to NULL. Not knowing two values


• REMOVE n:Person Remove label from node.
does not imply that they are the same value. So the
expression NULL = NULL yields NULL and not TRUE. To Collections
check if an expression is NULL, use IS NULL. Command Description

Literal collections are declared in


• Arithmetic expressions, comparisons and function calls [‘a’,’b’,’c’] AS coll
square brackets.
(except coalesce) will return NULL if any argument is
NULL. length({coll}) AS len, Collections can be passed in as
{coll}[0] AS value parameters.
• Missing elements like a property that doesn’t exist or range({from},{to},{step}) Range creates a collection of
accessing elements that don’t exist in a collection yields AS coll numbers (step is optional).
NULL.
Relationship identifiers of a
MATCH (a)-[r:KNOWS*..5]->()
variable length path is a collection
• In OPTIONAL MATCH clauses, NULLs will be used for RETURN r AS rels
of relationships.
missing parts of the pattern.
RETURN Properties can be arrays/
Patterns node.coll[0] AS value, collections of strings, numbers or
length(node.coll) AS len booleans.
Command Description
Collection elements can be
(n)-->(m) A relationship from n to m exists. accessed with idx subscripts in
square brackets.
Matches nodes with the label
(n:Person) coll[{idx}] AS value, Invalid indexes return NULL.
Person.
coll[{start}..{end}] AS Slices can be retrieved with
Matches nodes that have both slice intervals from start to end,
(n:Person:Swedish) each of which can be omitted or
Person and Swedish labels.
negative.
Matches nodes with the declared Out of range elements are ignored.
(n:Person {name: {value}})
properties.
With UNWIND, you can transform
Node n, labeled Person, has a UNWIND {names} AS name
(n:Person)-->(m) any collection back into individual
relationship to node m. MATCH
rows.
(n:Person {name:name})
A relationship in any direction The example matches all names
(n)--(m) RETURN avg(n.age)
between n and m. from a list of names.

(m)<-[:KNOWS]-(n)
A relationship from n to m of type Maps
KNOWS exists.
Command Description
A relationship from n to m of type
(n)-[:KNOWS|:LOVES]->(m) Literal maps are declared in curly
KNOWS or LOVES exists. {name:"Alice", age:38,
braces much like property maps.
address:{city:"London",
Bind an identifier to the Nested maps and collections are
(n)-[r]->(m) residential:true}}
relationship. supported.
Variable length paths can span 1 MERGE Maps can be passed in as
(n)-[*1..5]->(m)
to 5 hops. (p:Person{name:{map}.name}) parameters and used as map or by
ON CREATE SET p={map} accessing keys.
Variable length path of any depth.
(n)-[*]->(m)
See performance tips. range({start},{end},{step}) Range creates a collection of
AS coll numbers (step is optional).
Match or set properties in MATCH,
(n)-[:KNOWS]->(m:Label
CREATE, CREATE UNIQUE or MERGE MATCH (n:Person)-[r]->(m) Nodes and relationships are
{property: {value}})
clauses. RETURN n,r,m returned as maps of their data.
shortestPath( Find a single shortest path for Map entries can be accessed by
map.name, map.age, map.
(n1)-[*..6]-(n2)) previously matched nodes. their keys.
children[0]
Invalid keys result in an error.
allShortestPaths(
Find all shortest paths.
(n1)-[*..6]-(n2))

© DZone, Inc. | dzone.com


6 Querying graphs with neo4j

Relationship Functions Path Functions


Command Description Command Description

String representation of the length(path) The length of the path.


type(rel)
relationship type.
The nodes in the path as a
startNode(rel) Start node of the relationship. nodes(path)
collection.
endNode(rel) End node of the relationship. relationships(path), The relationships in the path as a
rels(path) collection.
id(rel) The internal id of the relationship.
MATCH path=(n)-->(m)
Assign a path and process its
Predicates RETURN extract(
nodes.
x IN nodes(path) | x.prop)
Command Description
MATCH path=(n1)-[*]->(n2)
n.property <> {value} Comparison operators. FOREACH Execute an update operation for
(n IN rels(path) | each relationship of a path.
has(n.property) Functions. SET n.marked = TRUE)
n.number >= 1 Boolean operators combine
Collection Functions
AND n.number <= 10 predicates.
Command Description
n:Person Check for node labels.
length({coll}) Length of the collection.
identifier IS NULL Check if something is NULL.
Head returns the first, last the last
NOT has(n.property) Either property does not exist or head({coll}), last({coll}), element, and tail the remainder of
OR n.property = {value} predicate is TRUE. tail({coll}) the collection. All return NULL for
Non-existing property returns an empty collection.
n.property = {value} NULL, which is not equal to [x IN coll Combination of filter and extract in
anything. WHERE has(x.prop) | x.prop] a concise notation.
n.property =~ "Tob.*" Regular expression. A collection of the value of the
Make sure the pattern has at least extract(x IN coll | x.prop) expression for each element in the
(n)-[:KNOWS]->(m) original collection.
one match.
Exclude nodes with certain pattern A filtered collection of the
NOT (n)-[:KNOWS]->(m) filter(x IN coll
matches from the result. elements where the predicate is
WHERE x.prop <> {value})
TRUE.
n.property IN Check if an element exists in a
[{value1}, {value2}] collection. Evaluate expression for each
reduce(s = 0, x IN coll |
element in the collection,
s + x.prop)
accumulate the results.
Collection Predicates
FOREACH (value IN coll |
Command Description Execute an update operation for
CREATE
each element in a collection.
Returns TRUE if the predicate (:Person{name:value}))
ALL(x IN coll
is TRUE for all elements of the
WHERE has(x.property))
collection. Mathematical Functions
Returns TRUE if the predicate is Command Description
ANY(x IN coll
TRUE for at least one element of
WHERE has(x.property)) abs({expr}) The absolute value.
the collection.
Returns TRUE if the predicate A random value. Returns a new
NONE(x IN coll value for each call. Also useful
is FALSE for all elements of the rand()
WHERE has(x.property)) for selecting subset or random
collection.
ordering.
Returns TRUE if the predicate is
SINGLE(x IN coll Round to the nearest integer. Ceil
TRUE for exactly one element in round({expr}),
WHERE has(x.property)) and floor find the closest integer
the collection. ceil({expr}), floor({expr})
rounded up or down.
Functions sqrt({expr}) The square root.
Command Description
sign({expr}) 0 if zero, -1 if negative, 1 if positive.
coalesce(n.property,...,
The first non-NULL expression. Trigonometric functions, also cos,
{defaultValue})
sin({expr}) tan, cot, asin, acos, atan, atan2,
Milliseconds since midnight, haversin.
timestamp()
January 1, 1970 UTC.
degrees({expr}), Converts radians into degrees, use
The internal id of the relationship radians({expr}), pi() radians for the reverse.
id(node_or_relationship)
or node.
Logarithm base 10, natural
log10({expr}), log({expr}),
Converts the given input in an logarithm, e to the power of the
exp({expr}), e()
toInt({expr}) integer if possible; otherwise it parameter.
returns NULL.
Converts the given input in a
toFloat({expr}) floating point number if possible;
otherwise it returns NULL.

© DZone, Inc. | dzone.com


7 Querying graphs with neo4j

String Functions Upgrading


Command Description
In Neo4j 2.0, several Cypher features from version 1.9
String representation of the have been deprecated or removed:
toString({expression})
expression.
• START is optional.
Replace all occurrences of search
replace({string}, {search},
{replacement})
with replacement. • MERGE takes CREATE UNIQUE’s role for the unique
All arguments are be expressions. creation of patterns. Note that they are not the
substring({string}, Get part of a string. same.
{begin}, {len}) The len argument is optional.
• Optional relationships are handled by OPTIONAL
left({string}, {len}), The first part of a string. MATCH, not question marks.
right({string}, {len}) The last part of the string.
trim({string}) • Non-existing properties return NULL: n.prop? and
Trim all whitespace, or on left or n.prop! have been removed.
ltrim({string})
right side.
rtrim({string})
• The separator for collection functions changed
upper({string}),
UPPERCASE and lowercase. from ":" to "|".
lower({string})
Split a string into a collection of • Paths are no longer collections, use nodes(path)
split({string}, {delim})
strings. or rels(path).

Aggregation • Parentheses around nodes in patterns are no


longer optional.
Command Description

count(*) The number of matching rows. • CREATE a={property:’value’} has been


removed
count(identifier) The number of non-NULL values.
• Use REMOVE to remove properties.
All aggregation functions also
count(DISTINCT identifier)
take the DISTINCT modifier, which • Parameters for index-keys and nodes in patterns
removes duplicates from the are no longer allowed.
values.

collect(n.property) Value collection, ignores NULL. • To use the older syntax, prepend your Cypher
statement with CYPHER 1.9.
Sum numerical values. Similar
sum(n.property)
functions are avg, min, max.
Performance Tips
Discrete percentile. Continuous
percentileDisc(n.property, percentile is percentileCont.
{percentile}) The percentile argument is from • Use parameters instead of literals when possible. This
0.0 to 1.0. allows Cypher to reuse your queries instead of having
Standard deviation for a sample
to parse and build new execution plans.
stdev(n.property) of a population. For an entire
population use stdevp.
• Always set an upper limit for your variable length
patterns. It’s easy to have a query touch all nodes in a
CASE graph by mistake.

Command Description • Return only the data you need. Avoid returning
CASE n.eyes Return THEN value from the
whole nodes and relationships — instead, return only
WHEN 'blue' THEN 1 matching WHEN value. the properties you need.
WHEN 'brown' THEN 2 The ELSE value is optional, and
ELSE 3 substituted for NULL if missing.
END
Use Case: Recommendations
CASE
Return THEN value from the first Recommendations in Neo4j are both powerful and easy
WHEN n.eyes = 'blue' THEN 1
WHEN n.age < 40 THEN 2
WHEN predicate evaluating to TRUE. to implement. You can recommend anything, including
Predicates are evaluated in order. friends, music, products, places, books, jobs, travel-
ELSE 3
END connections ... even Refcardz.

START As you’re reading one, let’s take a Refcard collection as an


example for a tiny recommendation system. You can find
Command Description
more recommendation examples online.
START n =
Query the index with an exact In our example domain we have :Refcard nodes, which
node:indexName(key={value})
query. Use node_auto_index for
n=node:nodeIndexName(key={value})
the old automatic index. have a title and a lot of content, and connected
n=node:nodeIndexName(key={value}) :Author, :Topic, and :Skill nodes.
Query the index by passing To create three entries in our database, we would execute
the query string directly, can this statement:
START n =
be used with lucene or spatial
node:indexName({query})
syntax. E.g.: "name:Jo*" or
"withinDistance:[60,15,100]"

© DZone, Inc. | dzone.com


8 Querying graphs with neo4j

CREATE (:Author {name:"Michael Hunger"}) Now we can run a simple query that asks the following
-[:WROTE]->(neo4j:Refcard {title:"Querying Graphs Neo4j"}) question: “I really liked the Core Spring Data Refcard.
-[:FOR_SKILL]->(:Skill {level:1}), Matching my reading history and skills, what other Refcardz
(neo4j)<-[:TAGGED]-(nosql:Topic {name:"NoSQL"}),
should I read?”
(neo4j)<-[:TAGGED]-(:Topic {name:"GraphDB"})
CREATE (:Author {name:"Oliver Gierke"})
-[:WROTE]->(springData:Refcard {title:"Core Spring Data"})
MATCH (ref1:Refcard {title:"Core Spring Data"})
-[:FOR_SKILL]->(:Skill {level:2}),
<-[:TAGGED]-(t)-[:TAGGED]->(ref2:Refcard)<-[:WROTE]-(author)
(springData)<-[:TAGGED]-(:Topic {name:"Framework"}),
MATCH (ref1)-[:FOR_SKILL]->(skill1), (ref2)-[:FOR_SKILL]->(skill2)
(springData)<-[:TAGGED]-(nosql)
WHERE abs(skill1.level-skill2.level) < 2
CREATE (:Author {name:"Alex Baranau"})
RETURN ref2.title AS Title, author.name AS Author,
-[:WROTE]->(hbase:Refcard {title:"Apache HBase"})
count(*) as Score, collect(DISTINCT t.name) as Topics
-[:FOR_SKILL]->(:Skill {level:5}),
ORDER BY Score DESC
(hbase)<-[:TAGGED]-(:Topic {name:"Infrastructure"}),
(hbase)<-[:TAGGED]-(nosql)

Title Author Score Topics

Querying Graphs
Michael Hunger 1 [NoSQL]
with Neo4j
Oliver
Gierke
Framework
WR

2 That’s it! Based on your previous reading habits, the


OT

D
GE
E

FO G
TA
R_S
KIL
L database suggests you read the "Querying Graphs with
Core
Spring
Data
Neo4j" Refcard if you haven’t done so already, because its
skill level is similar to that of the Core Spring Data Refcard
and it shares a topic: NoSQL.
TAGGED

GraphDB

Infrastructure
Explore this example further in one of our live graph gist
ED

presentations.
G

NoSQL
G
TA

TA
GGE TAG
D GE
D GE D
TA
G Querying
Graphs WROTE Michael
Apache with Hunger
Alex WROTE HBase Neo4j
Baranau
FO
ILL

R_
SK

SK
IL
R_

L
FO

1
5

ABOUT THE AUTHOR RECOMMENDED BOOK


Michael Hunger has been passionate about software Graphs model the real world beautifully. But in a graph
development for a long time. He is particularly interested in the database, the absence of a natural schema can make both
people who develop software and making them successful by design and implementation difficult. Graph Databases helps
coding, writing, and speaking. For the last few years he has been you solve graph modeling and querying problems by walking
working at Neo Technology on the open source Neo4j graph through every aspect of graph database development, from
database (http://neo4j.com). There, Michael is involved in many basics of graph theory to predictive analysis with graphs to
things but focuses on developer evangelism in the great Neo4j graph database internals.
community and leading the Spring Data Neo4j project (http://
projects.spring.io/spring-data-neo4j).
FREE DOWNLOAD

Browse Our Collection of 250+ Free Resources, including:


Research Guides: Unbiased insight from leading tech experts
Refcardz: Library of 200+ reference cards covering the latest tech topics
Communities: Share links, author articles, and engage with other tech experts

JOIN NOW
DZone, Inc.
150 Preston Executive Dr.
Suite 201
Cary, NC 27513
DZone communities deliver over 6 million pages each month to more than 3.3 million software 888.678.0399
developers, architects and decision makers. DZone offers something for everyone, including 919.678.0300
news, tutorials, cheat sheets, research guides, feature articles, source code and more.
Refcardz Feedback Welcome
"DZone is a developer's dream," says PC Magazine. refcardz@dzone.com
Sponsorship Opportunities
|
Copyright © 2014 DZone, Inc. All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in
any form or by means electronic, mechanical, photocopying, or otherwise, without prior written permission © ofDZone,
the publisher.Inc. dzone.com
sales@dzone.com
Version 1.0 $7.95

You might also like