|
| 1 | +--- |
| 2 | +title: "Neocons, a Clojure client for Neo4J REST API: Using the Cypher Query Language" |
| 3 | +layout: article |
| 4 | +--- |
| 5 | + |
| 6 | +## About this guide |
| 7 | + |
| 8 | + * What is Cypher |
| 9 | + * Using Cypher to traverse graphs, retrieve nodes, relationships and path |
| 10 | + * Using mutating Cypher in Neo4J 1.8 to create and mutate graphs |
| 11 | + |
| 12 | +This work is licensed under a <a rel="license" href="http://creativecommons.org/licenses/by/3.0/">Creative Commons Attribution 3.0 Unported License</a> (including images & stylesheets). The source is available [on Github](https://github.com/clojurewerkz/neocons.docs). |
| 13 | + |
| 14 | + |
| 15 | +## What version of Neocons does this guide cover? |
| 16 | + |
| 17 | +This guide covers Neocons 2.0. |
| 18 | + |
| 19 | + |
| 20 | +## Cypher Overview |
| 21 | + |
| 22 | +One of the most powerful features of Neo4J is [Cypher, a query language (like SQL) for querying, traversing and mutating (Neo4J Server 1.8+) |
| 23 | +graphs](http://docs.neo4j.org/chunked/milestone/cypher-query-lang.html). Cypher makes queries like "return me all friends of my friends" or "return me all pages this page links to that were updated |
| 24 | +less than 24 hours ago" possible in a couple of lines of code. As such, operations and ad hoc queries in the Clojure REPL or Neo4J shell |
| 25 | +with Cypher are very common. |
| 26 | + |
| 27 | +Cypher also enables several operations Neo4J Server REST API does not provide to be executed efficiently. One common example is |
| 28 | +the "multi-get" operation that returns a collection of nodes by ids. |
| 29 | + |
| 30 | +Cypher queries are performed using `clojurewerkz.neocons.rest.cypher/tquery` and `clojurewerkz.neocons.rest.cypher/query` functions. |
| 31 | +`clojurewerkz.neocons.rest.cypher/tquery` is more common because it returns data in a more convenient tabular form, while |
| 32 | +`/query` returns columns and result rows separately. |
| 33 | + |
| 34 | +Covering Cypher itself is out of scope for this tutorial so lets just take a look at a couple of examples. Here is how |
| 35 | +to find all Amy's friends via Cypher: |
| 36 | + |
| 37 | +``` clojure |
| 38 | +(ns neocons.docs.examples |
| 39 | + (:require [clojurewerkz.neocons.rest :as nr] |
| 40 | + [clojurewerkz.neocons.rest.nodes :as nn] |
| 41 | + [clojurewerkz.neocons.rest.relationships :as nrl] |
| 42 | + [clojurewerkz.neocons.rest.cypher :as cy])) |
| 43 | + |
| 44 | +(defn -main |
| 45 | + [& args] |
| 46 | + (let [conn (nr/connect "http://localhost:7474/db/data/") |
| 47 | + amy (nn/create conn {:username "amy"}) |
| 48 | + bob (nn/create conn {:username "bob"}) |
| 49 | + _ (nrl/create conn amy bob :friend {:source "college"}) |
| 50 | + res (cy/tquery conn "START person=node({sid}) MATCH person-[:friend]->friend RETURN friend" {:sid (:id amy)})] |
| 51 | + (println res))) |
| 52 | +``` |
| 53 | + |
| 54 | +And here is how to get back usernames and ages of multiple people using Cypher: |
| 55 | + |
| 56 | +``` clojure |
| 57 | +(ns neocons.docs.examples |
| 58 | + (:require [clojurewerkz.neocons.rest :as nr] |
| 59 | + [clojurewerkz.neocons.rest.nodes :as nn] |
| 60 | + [clojurewerkz.neocons.rest.relationships :as nrl] |
| 61 | + [clojurewerkz.neocons.rest.cypher :as cy])) |
| 62 | + |
| 63 | +(defn -main |
| 64 | + [& args] |
| 65 | + (let [conn (nr/connect "http://localhost:7474/db/data/") |
| 66 | + amy (nn/create conn {:username "amy" :age 27}) |
| 67 | + bob (nn/create conn {:username "bob" :age 28}) |
| 68 | + _ (nrl/create conn amy bob :friend {:source "college"}) |
| 69 | + res (cy/tquery conn "START x = node({ids}) RETURN x.username, x.age" {:ids (map :id [amy bob])})] |
| 70 | + (println res))) |
| 71 | +``` |
| 72 | + |
| 73 | +The latter query is roughly equivalent to |
| 74 | + |
| 75 | +``` sql |
| 76 | +SELECT username, age FROM nodes WHERE id IN (…); |
| 77 | +``` |
| 78 | + |
| 79 | +in SQL. |
| 80 | + |
| 81 | +Cypher is fundamental to Neo4J is the most powerful (and easy to use) tool for many common cases. |
| 82 | + |
| 83 | + |
| 84 | + |
| 85 | +## Retrieving nodes via Cypher |
| 86 | + |
| 87 | +TBD |
| 88 | + |
| 89 | + |
| 90 | +## Retrieving relationships via Cypher |
| 91 | + |
| 92 | +TBD |
| 93 | + |
| 94 | + |
| 95 | +## Retrieving paths via Cypher |
| 96 | + |
| 97 | +TBD |
| 98 | + |
| 99 | + |
| 100 | +## Creating nodes via Cypher |
| 101 | + |
| 102 | +TBD |
| 103 | + |
| 104 | + |
| 105 | +## Creating relationships via Cypher |
| 106 | + |
| 107 | +TBD |
| 108 | + |
| 109 | + |
| 110 | +## Updating nodes via Cypher |
| 111 | + |
| 112 | +TBD |
| 113 | + |
| 114 | + |
| 115 | + |
| 116 | +## What to Read Next |
| 117 | + |
| 118 | +Congratulations, this is the last guide. For the definitive reference on Cypher, see [Neo4J documentation on Cypher](http://docs.neo4j.org/chunked/stable/cypher-query-lang.html). |
| 119 | + |
| 120 | +Take a look at [other guides](/articles/guides.html), they cover all kinds of topics. |
| 121 | + |
| 122 | + |
| 123 | + |
| 124 | +## Tell Us What You Think! |
| 125 | + |
| 126 | +Please take a moment to tell us what you think about this guide on Twitter or the [Neocons mailing list](https://groups.google.com/forum/#!forum/clojure-neo4j) |
| 127 | + |
| 128 | +Let us know what was unclear or what has not been covered. Maybe you do not like the guide style or grammar or discover spelling mistakes. Reader feedback is key to making the documentation better. |
0 commit comments