Skip to content

Small Cypher and Neo4j Tutorial

mariamoritz edited this page Nov 21, 2014 · 2 revisions

Intro

The application uses the graph data base neo4j to store textual data. Next to the API of the application, a great tool for administration purposes of these data is the neo4j API that comes with the database. You can either use it under:

localhost:7474/browser

or you can use the data browser of the old, but more performant interface under:

localhost:7474/webadmin

Both interfaces work with cypher (http://neo4j.com/docs/2.1/cypher-refcard/ and http://neo4j.com/docs/stable/).

Small Cypher turorial

Create a node

CREATE (n)

Create a node with a label

CREATE (n:Document)

Create a node with a label and attributes

CREATE (n:Document {author: 'Thucydides', title : 'Pentecontaetia' })

Query a node with a label

MATCH (n:`Document`) RETURN n LIMIT 25

Query a relationship

MATCH (a)-[s]->(b) RETURN s LIMIT 25
MATCH (a)-[s]->(b) RETURN a, b LIMIT 25

Labels are required for indexing, query a labelled node or relationship:

MATCH (a:`Document`)-[s:`sentences`]->(b:`Sentence`) RETURN a, b LIMIT 25

Query a sub graph

MATCH (d:`Document`)-[sr:`sentences`]->(s:`Sentence`)-[wr:`words`]->(w:`Word`) RETURN d, s, w LIMIT 25

Delete a node type (all document nodes with this label)

MATCH (n:Document {title : `Pentecontaetia`})
DELETE n

Delete a sub graph (deletes all documents named 'Pentecontaetia' and its direct relationships)

MATCH (n:`Document` {author : 'authorname'})-[r]-() RETURN n LIMIT 25
DELETE n,r

Set an attribute

MATCH (n {title : 'Pentecontaetia'})
SET n.title = 'Neu'
RETURN n

Example filter query

MATCH (n:`Document`)
WHERE HAS (n.title) AND n.title = 'Pentecontaetia'
RETURN n

Create an index, e.g.

CREATE INDEX ON :Document(CTS)
CREATE INDEX ON :Document(ID)
CREATE INDEX ON :Document(author)

Small Python REST client tutorial

The API of the application uses a neo4j REST client written in python. A general tutorial folloes. Please use: http://neo4j-rest-client.readthedocs.org/en/latest/info.html for further details.

Graph database import and instance

from neo4jrestclient.client import GraphDatabase
gdb = GraphDatabase("http://localhost:7474/db/data/")

Create a node

d = gdb.nodes.create(author="Thucydides", name="Pentecontaetia")
s = gdb.nodes.create(CTS="urn:cts...s", sentence="This is a sentence string .")

Set a label (two variants)

d = gdb.labels.create("Document")
d.labels.add("Document")

Set a relatinship (two variants)

d.sentences(s) d.relationships.create("sentences", s)

Developer Guide

Backend

Frontend

Content Guide

What did you break?

  • Postgres Database
  • Neo4j
  • Django
  • "The server"
  • Git
  • UI
Clone this wiki locally