A while back I had asked if I there was an easy way to extend Cypher. Cypher is an excellent declarative way for querying a Neo4j database but the functions in it leave me wanting for me. I had this twitter conversation:
Cypher is built using Scala. As I had only basic knowledge of the language, creating new functionality would have been challenging for me. So I reached for my current favourite language, Clojure. It runs on JVM. So it should have been possible to integrate that with Scala and cypher. Once I was in clojure, I realised that I can really leverage the power of dynamic programming to attach a rocket booster to cypher. By that I mean, I could very easily add really complex functions to cypher easily. And the whole contraption would be stuck together with programming equivalent of duct tape.
So here we go.
A great use case for the extension is adding Date/DateTime support which Neo4j lacks natively. I don’t think its a deal breaker but some people have strong views aroung this, as voiced at the end of Jim Weber’s excellent talk at Skills Matter called Impossible is Nothing. A workaround is to store that data as Unix Epochs, which are just long values. But it means if you want to add a date to a node, you first have to convert it to a long externally and then run your cypher query. So there 2 steps when there should only be 1.
This is my Clojure code which added two date functions to cypher:
The code for
cypher-ext is also on github.
With my changes, you can do the following:
So the date is parsed to a long by
str-to-date function and a long is converted to date string by
Obviously now to add more functions to cypher is really easy. All I need to do is add another
defmethod in my Clojure code.
The changes I had to make to Cypher in Neo4j codebase are here. The meat of the change is in the
compute method of
Everything is Awesome!!