Cookies help us deliver our services. By using our services, you agree to our use of cookies. More information

Neo4j:Basics - Introduction

From NoSQLZoo
Revision as of 17:32, 12 June 2018 by 40166222 (talk | contribs) (init unfinished)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Working with the greek_gods family tree graph

This tutorial introduces NoSQL using Neo4j. We will be using the RETURN, MATCH, and WHERE commands on the greek_gods collection which contains a family tree of around 130 individuals.

For anyone wishing to use this data-set on their own machines, the Cypher file used to build it is available here: *TBA*.

Introduction to Graphs and the 'Graph Property Model'

You've likely heard of graphs before. In mathematics a graph is the term for a set of vertices and edges, here the terms nodes and relationships are used instead.

In Neo4j graphs follow the "Property Graph Model". A property is just a key-value pair, such as name: 'Alice' or age: 27. Users who have used document-oriented databases such as MongoDB will find this concept very similar.

A node is a simple entity that contains any amount of properties, and they can be labeled as belonging to a certain set. For example, in a database of celebrities Steven Fry could be labeled as a :Actor. There's no limit to the amount of labels that can apply to a node, the celebrity Hugh Laurie could be simultaneously labeled as an :Actor, :Musician, and :Author.

In the property graph model, a relationship is a connection between exactly two nodes, though there's not limit to the amount of relationships a node made have. A relationship always has both a name and a direction (start node and end node), and like a node it can also have properties. Bidirectional relationships are a little trickier, and are out-width the scope of this tutorial: Modelling Bidirectional Relationships

This remainder of this tutorial will use the Cypher query language to introduce some basic concepts.


RETURN

The RETURN clause determines what is included in the query result set. The following operation returns some numbers, strings, and lists added together.

RETURN 2 + 2, "A" + "B", [1, 2, 3] + [4];

MATCH and WHERE

The MATCH clause is used to search for data that meets a pattern you define.
The WHERE clause is used to restrict these patterns.

Find a node, n, with the label ':God' and a name property equal to 'Zeus'.

MATCH (n)
WHERE n:God AND n.name = 'Zeus'
RETURN n;
MATCH (g) WHERE g:God AND g.name = 'Zeus' RETURN g; 

In most scenarios, WHERE and MATCH can be combined. e.g. using the previous example:

MATCH (g:God {name: 'Zeus'})
RETURN g;

This query currently gets Zeus' names.

Find Hera's English and Greek names.

MATCH (g:God {name: "Zeus"}) 
RETURN g.name, g.greek;
MATCH (g:God {name: "Zeus"}) RETURN g.name, g.greek;

Find the Greek names of the 'Big Three' ('Zeus', 'Poseidon', 'Hades')

MATCH (g:God) 
WHERE g.name IN ['Hera', 'Demeter'] 
RETURN g.name, g.greek;
MATCH (g:God) WHERE g.name IN ['Zeus', 'Poseidon', 'Hades'] RETURN g.name, g.greek;


Further MATCH Examples can be found here.