Skip navigation

Category Archives: NoSQL

Neo4j graph is comprised of Nodes and Relationships (well and more things like properties).

  • Nodes represent entities (ex: actorsdirectors and movies).
  • Relationships are used to connect them. So we will have an ACTED_IN relationship between Keanu Reeves and The Matrix and a DIRECTED relationship between Clint Eastwood and the movie Unforgiven.

Nodes

Cypher uses ASCII-art to represent patterns. We surround nodes with parentheses which look like circles, e.g. (node). If we later want to refer to the node, we’ll give it an identifier like (a) for actor or (m) for movie. In real world queries, we’ll probably use longer, more expressive variable names like (actor) or (movie).

Properties

Both Nodes and Relationships can have Properties. Properties are key:value pairs used to add information about the nodes and relationships.

Nodes with Properties

(m {title:"The Matrix"}) Movie with a title property
(a {name:"Keanu Reeves",born:1964}) Actor with name and born property

Relationships examples *:

-[r]-> a relationship referred to as “r”
(a)-[r]->(m) actors having a relationship referred to as “r” to movies
-[:ACTED_IN]-> the relationship type is ACTED_IN
(a)-[:ACTED_IN]->(m) actors that ACTED_IN some movie
(d)-[:DIRECTED]->(m) directors that DIRECTED some movie

untitled

General view of the movie database example, in fact the DOMAIN MODEL, in a way of think it’s natural to think in that way, it’s how the information flows and how we built sentences, so is even more natural than SLQ:

untitled

Relationship with Properties

MATCH (node1)-[:REL_TYPE]->(node2)

(a)-[:ACTED_IN {roles:["Neo"]}]->(m) Relationship ACTED_IN with roles property (an array of character names)

 Labels

To allow us to distinguish between different types of nodes, we us Labels. Any Node can have one or more labels. The obvious choice are labels like Movie or Person. We might also label Clint Eastwood and Keanu Reeves as Actor, and Clint Eastwood also as a Director.

(a:Person) a Person
(a:Person {name:"Keanu Reeves"}) a Person with properties
(a:Person)-[:ACTED_IN]->(m:Movie) a Person that ACTED_IN some movie

Examples:

//node(d) with relation of type "DIRECTED" with node (m)
MATCH (d)-[:DIRECTED]->(m) return d,m LIMIT 10

//nodes with any relation
 MATCH (d)-->(m) return d,m

//nodes with property "Keanu..." labeled as "Person" with a relationship of type ACTED_IN with (m) 
MATCH (a:Person {name:"Keanu Reeves"})-[:`ACTED_IN`]->(b) RETURN a,b LIMIT 25

 

What the hell is Neo4J? It is a Open Source NoSQL graph database that stores data in a graph.

“A Graph —records data in→ Nodes —which have→ Properties” 

The simplest graph is a single node* a node can have 1 to N properties, nodes have explicit relationships.

* Record that has named values referred to as Properties

NOTE: It has a cool server with an interface “ready to play” but it has really bad performance, showing 100 nodes at the same time on my platform** it is unusable.

** Intel i5 – 4 Gb ram + Strange HD + Chrome.

Relationships organize the Graph

untitled“Nodes —are organized by→ Relationships —which also have→ Properties”

Relationships organize Nodes into arbitrary structures, List, Tree,  Map, or a compound Entity – any of which can be combined into yet more complex ones.

Labels group the Nodes

“Nodes —are grouped by→ Labels —into→ Sets”

Labels are a means of grouping the nodes in the graph. They can be used to restrict queries to subsets of the graph, as well as enabling optional model constraints and indexing rules.

Query a Graph with a Traversal

untitled“A Traversal —navigates→ a Graph; it —identifies→ Paths —which order→ Nodes”

A Traversal is how you query a Graph, navigating from starting Nodes to related Nodes according to an algorithm, finding answers to questions like “what music do my friends like that I don’t yet own,” or “if this power supply goes down, what web services are affected?”

Indexes look-up Nodes or Relationships

“An Index —maps from→ Properties —to either→ Nodes or Relationships”

untitled

You may want to find a specific Node or Relationship according to a Property it has. Rather than traversing the entire graph, use an Index to perform a look-up, for questions like “find the Account for username master-of-graphs.”

 

 

“A Graph Database —manages a→ Graph and —also manages related→ Indexes

untitled

neo4j

 

 

 

 

 

 

 

 

 

You write queries using Cypher, a declarative graph query language for the graph databaseNeo4j that allows for expressive and efficient querying and updating of the graph store. Cypher is a relatively simple but still very powerful language. Very complicated database queries can easily be expressed through Cypher. This allows you to focus on your domain instead of getting lost in database access.

Format

Cypher contains a variety of clauses, some of the most common of which include MATCH and WHERE. These function slightly differently than in SQL. MATCH is used for filtering results based on relationships, and WHERE is used to filter results based on properties.

Cypher additionally contains clauses for writing, updating, and deleting data. CREATE and DELETE are used to create and delete nodes and relationships. SET and REMOVE are used to set values to properties and add labels on nodes. It should be noted that nodes can only be deleted when they have no other relationships still existing. 

from: http://docs.neo4j.org/chunked/2.0.1/what-is-a-graphdb.html