Here is a simple graph model straight from the neo4j website
We'll start with something then make it evolve.
//Creates 2 Person type node Anna and Dany in a UNION type relationship with a Union node
CREATE p =(a:Person { name:'Anna' })-[:UNION]->(:Union { name:'Wedding' })<-[:UNION]-(b:Person { name: 'Dany' })
RETURN p
// Creates the WIFE and HUSBAND relationship between Anna and Danny
MATCH (a:Person),(b:Person)
WHERE a.name = 'Anna' AND b.name = 'Dany'
CREATE (a)-[:WIFE]->(b)
CREATE (b)-[:HUSBAND]->(a)
RETURN a, b
// Find the :Union node named Wedding or using any with its ID (81 in my case) and setting the new gedcom property at U11
MATCH (a:Union)
WHERE id(a)=81 OR a.name = 'Wedding'
SET a.gedcom = 'U11'
RETURN a
// Finding the :Union node with gedcom at U11 and create a new Person node named Tim related to it as a CHILD
MATCH (a:Union)
WHERE a.gedcom = 'U11'
CREATE (:Person {name:"Tim"})-[:CHILD]->(a)
RETURN a
// Find that created note with a CHILD relation, finds any UNION in the relation and add a SON relationship from the node Tim to them.
MATCH (a:Person { name:'Tim' })-[:CHILD]->(:Union { gedcom:'U11' })
MATCH (b:Person)-[:UNION]->(:Union { gedcom:'U11' })
CREATE (a)-[:SON]->(b)
RETURN a
Neo4j platform has a built-in graph database. SQL databases are like arrays, the graph database is based from node and relationships. Once installed it works similarly to any database.
Neo4j is based on 3 component:
- Nodes identified by labels
- Properties identified by property-names
- Relations identified by relation-types
Neo4j's website is full of documentation to learn more about it.
You can try it in the neo4j sandbox directly from their site. (Can be a bit slow)
Neo4j is free and can be downloaded directly from their website.
Checkout neo4j's installation doc.
You might need to download a JVM (Open JDK or Oracle JDK).
- Linux, the Open JDK v8
sudo apt-get install openjdk-8-jre
- On windows, you can donwload the Oracle JDK v8
Cypher is the querie language for neo4j, it is an SQL-inspired language for describing patterns in graphs visually using an ascii-art syntax.
You can try the movie example by typing :play movie graph
or you can follow this simple gist.
Creating a node labeled Person
with a property named name
of value Joe
.
CREATE (:Person {name:"Jerry"})
Nodes are surrounded by ( )
as a circle.
Creating two different nodes like before but this time linking one to the other with a relation, typed as KNOW
and with a property named date
.
CREATE (:Person {name:'Rick'})-[:KNOW]->(:Morty {name: "Morty", dimension: "C-137"})
CREATE (:Person {name:'Rick'})-[:KNOW {date:'2015-06-23'}]->(:Person {name: "Beth"})
Relation are arrows -->
. The relationship is only one-sided
Queries can use a lot of special keywords and can be tweaked. Here is a simple one.
MATCH (:Person)-[:KNOW]->(n:Morty)
WHERE n.dimention = "C-137"
RETURN n
The n
here define the node in the query, it is optional, here creating advance functions to display nodes.
APOC "Awesome Procedures On Cypher" is a library of procedure to help with visualisation and data manipulation on Neo4j.
To install Apoc:
- Download the latest release. And put it in the Neo4j plugin folder which place may differ:
C:\Users\username\AppData\Roaming\Neo4j Desktop\Application\neo4jDatabases\database-xxxx\installation-<version>\plugins
C:\Program Files\Neo4j CE <version>\plugins
- Install the latest release from 'Neo4j Desktop' by going on a Database then [ 🔧 Manage ] then
plugin > install
. - Get the package with npm
npm install apoc
There are multiple options to interac with Neo4j.
With python 3, you can use the pyhton driver with py2neo a client library and toolkit to communicate with neo4j.
To install py2neo
pip install py2neo
Neo4j works with a REST API, which can be used to send cypher queries and receive data.
Py2neo in the example works with bottle which is a micro web-framework to interac with the Neo4j database via its Rest API.
pip install bottle
The librairy is also stored under Bottle.py for the example.
Virtualenv is a tool to create isolated Python environments. Virtualenv creates a folder which contains all the necessary executables to use the packages that a Python project would need.
pip install virtualenv
Checkout the example movie py2neo from neo4j. On windows to activate the cypher app rather than the (Linux) instruction from the example:
cypher-app\Scripts\activate
The default neo4j browser allows to display nicely the results of a Cypher query with the built-in D3.js librairy. However there are multiple options that can be used to display the graph database available on neo4j.
- More information: D3 wiki
- Librairy for neo4j and D3.js: neo4jd3
- Some examples:
- An example project from neo4j.
- Visualizing a Network with Cypher and D3.js from Max De Marzi. [ 3d-intro, 3d-network ]
- visualizations of graph data with D3 and Neo4j from Grundsatzlich.
You can use the minified 3D.js library with:
<script src="https://d3js.org/d3.v4.min.js"></script>
GEDCOM (an acronym standing for Genealogical Data Communication) is an open de facto specification for exchanging genealogical data between different genealogy software.
There are some gedcom parser in the resources folder.