Notes adapted from 7DB - Neo 4J Day1
First we will do a quick graph review.
Some terminology:
- node is a vertex in a graph
- relationships are edges
Query languages:
- Gremlin - Older query language (supported by neo4j with plugins)
- Cypher - Used by Neo4J
To create things:
CREATE (<tag> { <data in JSON format> } )
To match things:
MATCH <criterion for a set of nodes or relationships>
WHERE <criterion for properties>
RETURN <results from MATCH and WHERE>
But lets see some examples. First, let's create some data
CREATE (w:Wine {name:"Prancing Wolf", style: "ice wine", vintage: 2015})
And let's query
MATCH (n)
RETURN n;
Question How did that query get to the serve?
Now, let's add a little more data.
CREATE (p:Publication {name: "Wine Expert Monthly"})
Question What is the tag? Question What are the properties? Question What do you think will happen if we add another publication with different properties?
Now, we have two nodes, but this is a graph and graphs have edges (relationships in the graph DB world). Lets add a relationship. We will use a match to find the nodes that we want and then create the relationship:
MATCH (p:Publication { name: "Wine Expert Monthly"}),
(w:Wine {name: "Prancing Wolf", vintage: 2015})
CREATE (p) - [r:reported_on] -> (w)
Now, we can see our relationship was added as there is now an arrow between our two nodes:
MATCH (n)
RETURN n;
And we can get relationship information:
MATCH ()- [r] - () return id(r)
Observe that the id is 0. Relationships can have properties as well. Let's set one:
MATCH ()-[r]-()
WHERE id(r) = 0
SET r.rating = 97
RETURN r
Let's add a few more relationships. For example, that "Pracing Wolf" is a Riesling.
Question Create a node for "Riesling" with tag "GrapeType" and create a relationship "grape_type" between our new node and "Prancing Wolf". When you are all done display the output with a match command
CREATE (g:GrapeType {name: "Riesling"})
Next
MATCH (w:Wine {name: "Prancing Wolf"}), (g:GrapeType {name: "Riesling"})
CREATE (w) - [r:grape_type] -> (g)
Finally display:
MATCH (n) RETURN n;
Similar to other models, we can also delete with a structure of search/delete
MATCH <criterion for a set of nodes or relationships>
DELETE <results from MATCH>
For example
$ CREATE (d: ToDelete {name: "delete me"})
$ MATCH (w:Wine {name: "Prancing Wolf"}),(d:ToDelete {name: "delete me"})
CREATE (w)-[r:relationship_to_delete]->(d)
$ MATCH ()-[r:relationship_to_delete]->()
DELETE r
$ MATCH (d:ToDelete)
DELETE d
Okay, let's delete everything!
Question write a query that deletes everything
First try:
MATCH (n) DELETE n;
That doesn't work because we get the error message:
Cannot delete node<X>, because it still has relationships.
To delete this node, you must first delete its relationships.
We could delete in two lines:
MATCH (n)-[r]-() DELETE r;
MATCH (n) DELETE n;
Or us "Option Match"
MATCH (n)
OPTIONAL MATCH (n)-[r]-()
DELETE n, r
Either way, we can check it is all gone with our favorite:
MATCH (n) RETURN n;
Next time we will get into more queries and programmatic access.