Spring in Practice

Willie Wheeler's Spring blog

Neo4j 1.6.1 Cypher Query Examples When Using Spring Data Neo4j 2.0

| Comments

This post shows how to perform various sample Cypher queries when using Neo4j 1.6.1 and Spring Data Neo4j (SDN) 2.0.

SDN 2.0 assumes Neo4j 1.6, and it imposes specific structures on databases that it creates. For example, SDN uses the type property on nodes to store the associated Java class, and it names its indexes using the Java class’ simple name.

In the examples below, I’m using the Neo4j shell, though you can of course run Cypher queries outside of the shell (e.g., from within the app itself). See my earlier blog post if you’re interested in learning how to use the shell.

I’ll probably add more examples to this post from time to time.

Return a single node with a given ID

neo4j-sh (Skybase,1)$ start n=node(1) return n
+------------------------------------------------------------------------------------------------------------------+
| n                                                                                                                |
+------------------------------------------------------------------------------------------------------------------+
| Node[1]{__type__->"org.skydingo.skybase.model.Application",name->"Skybase",shortDescription->"Cloud-based CMDB"} |
+------------------------------------------------------------------------------------------------------------------+

Return multiple nodes, looked up by ID

neo4j-sh (Skybase,1)$ start n=node(1,4,5) return n
+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| n                                                                                                                                                                                  |
+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Node[1]{__type__->"org.skydingo.skybase.model.Application",name->"Skybase",shortDescription->"Cloud-based CMDB"}                                                                   |
| Node[4]{__type__->"org.skydingo.skybase.model.Module",name->"Domain",shortDescription->"Domain model",moduleId->"org.skydingo.skybase.domain",groupId->"org.skydingo.skybase"}     |
| Node[5]{moduleId->"org.skydingo.skybase.service",name->"Service",groupId->"org.skydingo.skybase",shortDescription->"Service module",__type__->"org.skydingo.skybase.model.Module"} |
+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

Find all nodes of a given type

neo4j-sh[readonly] (Maven,3)$ start n=node:__types__(className="org.skydingo.skybase.model.Module") return n
+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| n                                                                                                                                                                                  |
+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Node[3]{__type__->"org.skydingo.skybase.model.Module",name->"Maven",shortDescription->"Maven plugins",moduleId->"skybase-maven-plugin",groupId->"org.skydingo.skybase"}            |
| Node[4]{__type__->"org.skydingo.skybase.model.Module",name->"Domain",shortDescription->"Domain model",moduleId->"org.skydingo.skybase.domain",groupId->"org.skydingo.skybase"}     |
| Node[5]{moduleId->"org.skydingo.skybase.service",name->"Service",groupId->"org.skydingo.skybase",shortDescription->"Service module",__type__->"org.skydingo.skybase.model.Module"} |
| Node[7]{moduleId->"org.skydingo.skybase.client",name->"Client",groupId->"org.skydingo.skybase",shortDescription->"Client module",__type__->"org.skydingo.skybase.model.Module"}    |
+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

Find all instances of a class having a certain property

Say we have a org.skydingo.skybase.model.Module class with a groupId property and a moduleId property, both annotated with SDN’s @Indexed annotation. Then SDN will create an index for this class called Module. Here’s how to find modules in a given group:

neo4j-sh (Skybase,1)$ start n=node:Module("groupId:org.skydingo.skybase") return n
+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| n                                                                                                                                                                                  |
+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Node[3]{__type__->"org.skydingo.skybase.model.Module",name->"Maven",shortDescription->"Maven plugins",moduleId->"skybase-maven-plugin",groupId->"org.skydingo.skybase"}            |
| Node[4]{__type__->"org.skydingo.skybase.model.Module",name->"Domain",shortDescription->"Domain model",moduleId->"org.skydingo.skybase.domain",groupId->"org.skydingo.skybase"}     |
| Node[5]{moduleId->"org.skydingo.skybase.service",name->"Service",groupId->"org.skydingo.skybase",shortDescription->"Service module",__type__->"org.skydingo.skybase.model.Module"} |
| Node[7]{moduleId->"org.skydingo.skybase.client",name->"Client",groupId->"org.skydingo.skybase",shortDescription->"Client module",__type__->"org.skydingo.skybase.model.Module"}    |
+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

Here’s an alternative syntax for doing exactly the same thing:

neo4j-sh (Skybase,1)$ start n=node:Module(groupId = 'org.skydingo.skybase') return n
+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| n                                                                                                                                                                                  |
+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Node[3]{__type__->"org.skydingo.skybase.model.Module",name->"Maven",shortDescription->"Maven plugins",moduleId->"skybase-maven-plugin",groupId->"org.skydingo.skybase"}            |
| Node[4]{__type__->"org.skydingo.skybase.model.Module",name->"Domain",shortDescription->"Domain model",moduleId->"org.skydingo.skybase.domain",groupId->"org.skydingo.skybase"}     |
| Node[5]{moduleId->"org.skydingo.skybase.service",name->"Service",groupId->"org.skydingo.skybase",shortDescription->"Service module",__type__->"org.skydingo.skybase.model.Module"} |
| Node[7]{moduleId->"org.skydingo.skybase.client",name->"Client",groupId->"org.skydingo.skybase",shortDescription->"Client module",__type__->"org.skydingo.skybase.model.Module"}    |
+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

Find all nodes having a certain set of property values

neo4j-sh[readonly] (Maven,3)$ start n=node:Module("groupId:org.skydingo.skybase, moduleId:skybase-maven-plugin") return n  
+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| n                                                                                                                                                                       |
+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Node[3]{__type__->"org.skydingo.skybase.model.Module",name->"Maven",shortDescription->"Maven plugins",moduleId->"skybase-maven-plugin",groupId->"org.skydingo.skybase"} |
+-------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

Find all nodes in a specified relationship to a given node

neo4j-sh (Skybase,1)$ start app=node(1) match app-[:APPLICATION_MODULE]->module return module
+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| module                                                                                                                                                                             |
+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Node[7]{moduleId->"org.skydingo.skybase.client",name->"Client",groupId->"org.skydingo.skybase",shortDescription->"Client module",__type__->"org.skydingo.skybase.model.Module"}    |
| Node[5]{moduleId->"org.skydingo.skybase.service",name->"Service",groupId->"org.skydingo.skybase",shortDescription->"Service module",__type__->"org.skydingo.skybase.model.Module"} |
| Node[4]{__type__->"org.skydingo.skybase.model.Module",name->"Domain",shortDescription->"Domain model",moduleId->"org.skydingo.skybase.domain",groupId->"org.skydingo.skybase"}     |
| Node[3]{__type__->"org.skydingo.skybase.model.Module",name->"Maven",shortDescription->"Maven plugins",moduleId->"skybase-maven-plugin",groupId->"org.skydingo.skybase"}            |
+------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+

Reader-contributed

I’ll just add queries here if people send them. It’s useful to have examples of queries around.

Return a user’s events (of specific types), ordered by date

Mark Spritzler sent me the following query, which he noted that Michael Hunger mostly wrote:

START user=node({0})
MATCH user-[r]-event
WHERE type(r) = "ATTENDING"
OR type(r) = "INVITED"
OR type(r) = "HOSTING"
RETURN ID(event) as eventId, event.eventDate as eventDate, event.title as eventName, type(r) as eventUserType
ORDER BY event.eventDate desc

Comments