Below we have given examples of the most common operations in Neo4clj.
To make the client available and setup a connection run the following:
(require '[neo4clj.client :as client])
(def conn (client/connect "bolt://localhost:7687"))
Make the Neo4j client and connection available in the client and conn symbols respectively, this is a requirement for the subsequent examples to work.
You might need to adjust the connection parameters to match your setup, see below.
This section will show you how to connect with or without authentication and how to change some of the basic options for the connection.
(def conn (client/connect "bolt://localhost:7687"))
(def conn (client/connect "bolt://localhost:7687" "neo4j" "password"))
(def conn (client/connect "bolt://localhost:7687" {:log {:level :info}}))
This also works on authenticated connections.
(def conn
(client/connect
"bolt://localhost:7687"
"neo4j"
"password"
{:log {:level :info}
:encryption :none}))
In the current version we support the following options:
:log :level [:all :error :warn :info :off] - defaults to :warn
:encryption [:required :none] - defaults to :required"
(client/disconnect conn)
This section shows how to execute raw bolt queries against on a open connection
(client/execute! conn "MATCH (n:Person) RETURN n")
(client/execute! conn "MATCH (n:Person {firstName: $first_name}) RETURN n" {:first_name "Neo"})
Notice that dashes is not allowed in parameter names
This section shows how to do basic CRUD operations on nodes through Neo4clj convenience functions. To learn more about the Clojure representation of a node please see our representations page
(client/create-node! conn {:ref-id "p"
:labels [:person]
:props {:first-name "Neo"
:last-name "Anderson"}})
The entry given to find-nodes!
is a lookup representation. To learn more about the Clojure lookup representation see our representations page
(client/find-nodes! conn {:ref-id "p"
:labels [:person]
:props {:first-name "Neo"
:last-name "Anderson"}})
See the section Update and Delete Entity
This section shows how to do basic CRUD operations on relationships through Neo4clj convenience functions. To learn more about the Clojure representation of a relationship please see our representations page
The keys from
and to
are Lookup representations. To learn more about the Clojure representation of a lookup entry please see our representations page
(client/create-relationship! conn {:ref-id "p"
:type :employee
:from {:labels [:person] :props {:first-name "Neo"}}
:to {:id 12}
:props {:position "Developer"}})
To fetch relationships from the Neo4j database, please refer to the section Get Graph
See the section Update and Delete Entity
Nodes and relationships are both part of the broader category named "entities" and this section will describe how to update and delete them.
In this section you can find examples on how to update specific parts a Node or Relationship and how to delete them.
To change the labels of a node we have added two convenience functions.
Both the add-labels!
and remove-labels!
functions takes a lookup representation as second argument.
To learn more about the Clojure representation of a lookup entry please see our representations page
(client/add-labels! conn {:id 45} [:person :salesman])
(client/remove-labels! conn {:labels [:person] :props {:first-name "Neo"}} [:person :salesman])
To change the properties of a node or relationship we have added two convenience functions.
Both the update-props!
and replace-props!
functions takes a lookup representation as second argument.
To learn more about the Clojure representation of a lookup entry please see our representations page
This function takes a connection, a lookup representation and the property map to update matched entities with.
It will update an existing properties map based on the following rules.
(client/update-props! conn {:labels [:person] :first-name "Thomas"} {:first-name "Neo"})
This function takes a connection, a lookup representation and the property map to replace the property map on matched entities with.
(client/replace-props! conn {:labels [:person] :first-name "Thomas"} {:last-name-only "Anderson"})
So in the example we find all nodes with label :person
and the property key value pair :first-name "Thomas"
and replace the
whole property map, not only the key :first-name
with the property-map {:last-name-only "Anderson"}
The delete!
function takes a lookup representation as second argument and deletes all matches.
Notice: You need to ensure you have deleted all relatiohsips to a node before you can delete the node.
To learn more about the Clojure representation of a lookup entry please see our representations page
(client/delete! conn {:labels [:person] :first-name "Neo"})
This section shows how to do basic CRUD operations on a whole graph through Neo4clj convenience functions.
To make it easier to create nodes and relationships, we have made a function to do it in one single call.
The lookups
key specifies a vector of lookup representations referring existing nodes in the database.
The nodes
key specifies a vector of node representations to create.
The relatioships
key specifies a vector of relationship representations to create. It is possible to use ref-id
from the lookups
vector in the to
and from
keys of the relationship.
The returns
key specifies a vector of ref-id
from the other three keys to return as the result of the call.
(client/create-graph!
conn
{:lookups [{:ref-id "c" :labels [:city] :props {:name "New York"}}]
:nodes [{:ref-id "p" :labels [:person] :props {:first-name "Neo"}}]
:relationships [{:type :lives-in :from {:ref-id "p"} :to {:ref-id "c"} :props {:born-here false}}]
:returns ["c" "p"]})
To make it easier to fetch nodes and relationships, we have made a function to do it all in one single call.
The nodes
key specifies a vector of lookup representations of node to get from the database.
The relatioships
key specifies a vector of relationship representations which needs to exists between the nodes to match.
The returns
key specifies a vector of ref-id
from the other two keys to return as the result of the call.
(client/get-graph
conn
{:nodes [{:ref-id "c" :labels [:city]}
{:ref-id "p" :labels [:person] :props {:first-name "Neo"}}]
:relationships [{:ref-id "r" :type :lives-in :from {:ref-id "p"} :to {:ref-id "c"}}]
:returns ["c" "p" "r"]})
To help handling index creation and drops, we have added convenience functions for theese operations
Create an index on a single property
(client/create-index! conn :person [:first-name])
Create an index across multiple properties
(client/create-index! conn :person [:first-name :last-name])
Drop an index on a single property
(client/drop-index! conn :person [:first-name])
Drop an index across multiple properties
(client/drop-index! conn :person [:first-name :last-name])
This section describes how to execute several queries in a session or transaction.
By default all the operations you run in Neo4clj will be run in a separate session, but if you want to run multiple queries in a session you can do it as shown below.
(client/with-session conn session
(client/create-node! session {:ref-id "p" :labels [:person] :props {:first-name "Neo"}})
(client/create-node! session {:ref-id "p" :labels [:person] :props {:first-name "Morpheus"}}))
Notice the session variable is a placeholder, you can use as the connection in your queries.
Running queries in a transaction ensures all queries are run without errors before commiting the changes to Neo4j. In Neo4clj all transactions are auto-commiting on sucess.
(client/with-transaction conn transaction
(client/create-node! transaction {:ref-id "p" :labels [:person] :props {:first-name "Neo"}})
(client/create-node! transaction {:ref-id "p" :labels [:person] :props {:first-name "Morpheus"}}))
Notice the transaction variable is a placeholder, you can use as the connection in your queries.
By default all exceptions occuring within the body of the with-transaction
will result in a roll-back,
but it is also possible to do a manual rollback as shown below.
(client/with-transaction conn transaction
(client/create-node! transaction {:ref-id "p" :labels [:person] :props {:first-name "Neo"}})
(client/create-node! transaction {:ref-id "p" :labels [:person] :props {:first-name "Morpheus"}})
(client/rollback transaction))
Notice the transaction variable is a placeholder, you can use as the connection in your queries.
Can you improve this documentation?Edit on GitHub
cljdoc is a website building & hosting documentation for Clojure/Script libraries
× close