Cypher CREATE, SET, DELETE, FOREACH
Lead Cypherologist Andrés Taylor has been cranking out work like Pablo Picasso with an empty museum. He shared some of his thoughts about the new Cypher grammar available in 1.8.M01:
ABK: It seems you can’t get Cypher out of your head fast enough.
AT: Yeah, it’s really like that. My family is tired of listening to me talk over the dinner table about implementation details about Cypher, I just can’t let it go.
ABK: Some of Cypher is SQL inspired, showing your SQL roots, but the new grammar is not like SQL.
AT: At first, I didn’t want any mutation stuff in it whatsoever, it was going to be a read only language. Then, I didn’t know what I wanted, but I did know what I didn’t want. Like with SQL, with all the problems you can have with inserting data.
ABK: What kind of problems?
AT: I think that SQL is a wonderful language when you’re talking about SELECT statements, but when you’re doing data manipulation, it quickly breaks down into an imperative thing. For example a normal problem is the “insert or update”, which doesn’t have any clean solution. Another is working over collections of stuff.
ABK: Ah, so that’s why FOREACH is included now.
AT: Yup.
ABK: And so what was your solution?
AT: Being declarative. By giving our users the tools to express what they want in a declarative way so they don’t have to invent patterns to work around the language. And, it is much, much easier to understand what’s happening, for a reader of a query.
AT: Looking at a problem, a bug yesterday, I needed a graph to work on so I wrote:
CREATE a,b, a-[:x]->b, a-[:y]->b;Try It
Just two nodes, with two relationships between them, and that was awesome. Actually, the first version of this would have looked to much worse. There has been fine-tuning. There was a meetup, when Aseem came with a lot of suggestions and the community had a lot of discussion about it. This is the result.
Continuing with the example, you can give them values, with a JSON look-alike thingy:
CREATE a={name:"Andres", age:37}, b, a-[:x { foo:"bar" } ]->b, a-[:y]->bTry It
Then update a property like this:
START n=node(0) SET n.name="Michael";Try It
Simple deletes are now possible like this:
START n=node(0) DELETE n;Try It
ABK: What about more complex statements, like operations on a subgraph?
AT: Instead of sub-queries, you can chain parts of statements using WITH, which essentially combines a RETURN and a START. Like this:
START lucy=node(1) MATCH lucy-->movieTry It
WITH lucy, collect(movie.title) AS movies
SET lucy.movies = movies
To work on that collections of things, there is FOREACH:
START lucy=node(1) MATCH lucy-->movie WITH lucy, collect(movie) AS moviesTry It
FOREACH (movie in movies : SET movie.actress=lucy.name )
The one rule that we have, is that in one part of the query you can only read from the graph or write to the graph. In the short forms above, there are two logical parts, a write then a read, with an implied separation. To be explicit, and create longer queries from parts, you use WITH.
ABK: Neat. Well, thanks for all the hard work Andrés. Can’t wait to try it out.
AT: There is more to come, but this feels good. Enjoy.
Hogwarts Manual of Graphs
Having unleashed the live Graph Console, the obvious next step was to animate the Cypher examples in the Neo4j Manual. Now the same graph code that generates the documentation also creates interactive consoles to allow experimentation. As a living, constantly evolving document, the Neo4j Manual learns new ways to help you learn.The Console itself is evolving into a nice REPL, currently supporting Cypher and Geoff for initial graph creation, and Cypher for queries. A nice touch is that the visualization now highlights created nodes and relationships. When done, the graph and an initial query can be shared with a click of the share-button.
These shared graph-gists are really handy for tweeting questions, reporting issues on github and discussing graph modeling in the Neo4j Google Group.
Details
- an OSGi section has been added to the manual
- streaming results from the REST API
- the usual relentless performance improvements and bug fixes
# heroku addons:add neo4j --neo4j-version 1.8.M01