18.12. Cypher queries

18.12.1. Send a Query
18.12.2. Return paths
18.12.3. Send queries with parameters
18.12.4. Nested results
18.12.5. Server errors

The Neo4j REST API allows querying with the Chapter 15, Cypher Query Language. The results are returned as a list of string headers (columns), and a data part, consisting of a list of all rows, every row consisting of a list of REST representations of the field value - Node, Relationship, Path or any simple value like String.

18.12.1. Send a Query

A simple query returning all nodes connected to node 1, returning the node and the name property, if it exists, otherwise null:

START x  = node(205)
MATCH x -[r]-> n
RETURN type(r), n.name?, n.age?

Figure 18.59. Final Graph


Example request

  • POST http://localhost:7474/db/data/cypher
  • Accept: application/json
  • Content-Type: application/json
{"query": "start x  = node(205) match x -[r]-> n return type(r), n.name?, n.age?","params": {}},

Example response

  • 200: OK
  • Content-Type: application/json
{
  "data" : [ [ "know", "him", 25 ], [ "know", "you", null ] ],
  "columns" : [ "TYPE(r)", "n.name", "n.age" ]
}

18.12.2. Return paths

Paths can be returned together with other return types by just specifying returns.

START x  = node(%I%)
MATCH path = (x--friend)
RETURN path, friend.name

Figure 18.60. Final Graph


Example request

  • POST http://localhost:7474/db/data/cypher
  • Accept: application/json
  • Content-Type: application/json
{"query": "start x  = node(209) match path = (x--friend) return path, friend.name","params": {}},

Example response

  • 200: OK
  • Content-Type: application/json
{
  "data" : [ [ {
    "start" : "http://localhost:7474/db/data/node/209",
    "nodes" : [ "http://localhost:7474/db/data/node/209", "http://localhost:7474/db/data/node/208" ],
    "length" : 1,
    "relationships" : [ "http://localhost:7474/db/data/relationship/92" ],
    "end" : "http://localhost:7474/db/data/node/208"
  }, "you" ] ],
  "columns" : [ "path", "friend.name" ]
}

18.12.3. Send queries with parameters

Cypher supports queries with parameters which are submitted as a JSON map.

START x  = node:node_auto_index(name={STARTName})
MATCH path = (x-[r]-friend)
WHERE friend.name = {name}
RETURN TYPE(r)

Figure 18.61. Final Graph


Example request

  • POST http://localhost:7474/db/data/cypher
  • Accept: application/json
  • Content-Type: application/json
{"query": "start x  = node:node_auto_index(name={startName}) match path = (x-[r]-friend) where friend.name = {name} return TYPE(r)","params": {"startName":"I","name":"you"}},

Example response

  • 200: OK
  • Content-Type: application/json
{
  "data" : [ [ "know" ] ],
  "columns" : [ "TYPE(r)" ]
}

18.12.4. Nested results

When sending queries that return nested results like list and maps, these will get serialized into nested JSON representations according to their types.

START n = node(%I%,%you%)
RETURN collect(n.name), collect(n)

Figure 18.62. Final Graph


Example request

  • POST http://localhost:7474/db/data/cypher
  • Accept: application/json
  • Content-Type: application/json
{"query": "start n = node(215,214) return collect(n.name), collect(n)","params": {}},

Example response

  • 200: OK
  • Content-Type: application/json
{
  "data" : [ [ [ "I", "you" ], [ {
    "outgoing_relationships" : "http://localhost:7474/db/data/node/215/relationships/out",
    "data" : {
      "name" : "I"
    },
    "traverse" : "http://localhost:7474/db/data/node/215/traverse/{returnType}",
    "all_typed_relationships" : "http://localhost:7474/db/data/node/215/relationships/all/{-list|&|types}",
    "property" : "http://localhost:7474/db/data/node/215/properties/{key}",
    "self" : "http://localhost:7474/db/data/node/215",
    "outgoing_typed_relationships" : "http://localhost:7474/db/data/node/215/relationships/out/{-list|&|types}",
    "properties" : "http://localhost:7474/db/data/node/215/properties",
    "incoming_relationships" : "http://localhost:7474/db/data/node/215/relationships/in",
    "extensions" : {
    },
    "create_relationship" : "http://localhost:7474/db/data/node/215/relationships",
    "paged_traverse" : "http://localhost:7474/db/data/node/215/paged/traverse/{returnType}{?pageSize,leaseTime}",
    "all_relationships" : "http://localhost:7474/db/data/node/215/relationships/all",
    "incoming_typed_relationships" : "http://localhost:7474/db/data/node/215/relationships/in/{-list|&|types}"
  }, {
    "outgoing_relationships" : "http://localhost:7474/db/data/node/214/relationships/out",
    "data" : {
      "name" : "you"
    },
    "traverse" : "http://localhost:7474/db/data/node/214/traverse/{returnType}",
    "all_typed_relationships" : "http://localhost:7474/db/data/node/214/relationships/all/{-list|&|types}",
    "property" : "http://localhost:7474/db/data/node/214/properties/{key}",
    "self" : "http://localhost:7474/db/data/node/214",
    "outgoing_typed_relationships" : "http://localhost:7474/db/data/node/214/relationships/out/{-list|&|types}",
    "properties" : "http://localhost:7474/db/data/node/214/properties",
    "incoming_relationships" : "http://localhost:7474/db/data/node/214/relationships/in",
    "extensions" : {
    },
    "create_relationship" : "http://localhost:7474/db/data/node/214/relationships",
    "paged_traverse" : "http://localhost:7474/db/data/node/214/paged/traverse/{returnType}{?pageSize,leaseTime}",
    "all_relationships" : "http://localhost:7474/db/data/node/214/relationships/all",
    "incoming_typed_relationships" : "http://localhost:7474/db/data/node/214/relationships/in/{-list|&|types}"
  } ] ] ],
  "columns" : [ "collect(n.name)", "collect(n)" ]
}

18.12.5. Server errors

Errors on the server will be reported as a JSON-formatted stacktrace and message.

START x = node(%I%)
RETURN x.dummy

Figure 18.63. Final Graph


Example request

  • POST http://localhost:7474/db/data/cypher
  • Accept: application/json
  • Content-Type: application/json
{"query": "start x = node(207) return x.dummy","params": {}},

Example response

  • 400: Bad Request
  • Content-Type: application/json
{
  "message" : "The property 'dummy' does not exist on Node[207]",
  "exception" : "org.neo4j.cypher.EntityNotFoundException: The property 'dummy' does not exist on Node[207]",
  "stacktrace" : [ "org.neo4j.cypher.commands.Property.apply(Expression.scala:77)", "org.neo4j.cypher.commands.Property.apply(Expression.scala:70)", "org.neo4j.cypher.commands.ExpressionReturnItem.apply(ReturnItem.scala:37)", "org.neo4j.cypher.commands.ExpressionReturnItem.apply(ReturnItem.scala:35)", "org.neo4j.cypher.internal.pipes.ExtractPipe$$anonfun$createResults$1$$anonfun$2.apply(ExtractPipe.scala:42)", "org.neo4j.cypher.internal.pipes.ExtractPipe$$anonfun$createResults$1$$anonfun$2.apply(ExtractPipe.scala:42)", "scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:194)", "scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:194)", "scala.collection.LinearSeqOptimized$class.foreach(LinearSeqOptimized.scala:59)", "scala.collection.immutable.List.foreach(List.scala:45)", "scala.collection.TraversableLike$class.map(TraversableLike.scala:194)", "scala.collection.immutable.List.map(List.scala:45)", "org.neo4j.cypher.internal.pipes.ExtractPipe$$anonfun$createResults$1.apply(ExtractPipe.scala:42)", "org.neo4j.cypher.internal.pipes.ExtractPipe$$anonfun$createResults$1.apply(ExtractPipe.scala:41)", "scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:194)", "scala.collection.TraversableLike$$anonfun$map$1.apply(TraversableLike.scala:194)", "scala.collection.LinearSeqOptimized$class.foreach(LinearSeqOptimized.scala:59)", "scala.collection.immutable.List.foreach(List.scala:45)", "scala.collection.TraversableLike$class.map(TraversableLike.scala:194)", "scala.collection.immutable.List.map(List.scala:45)", "org.neo4j.cypher.internal.pipes.ExtractPipe.createResults(ExtractPipe.scala:41)", "org.neo4j.cypher.internal.pipes.ColumnFilterPipe.createResults(ColumnFilterPipe.scala:33)", "org.neo4j.cypher.internal.ExecutionPlanImpl$$anonfun$6.apply(ExecutionPlanImpl.scala:120)", "org.neo4j.cypher.internal.ExecutionPlanImpl$$anonfun$6.apply(ExecutionPlanImpl.scala:118)", "org.neo4j.cypher.internal.ExecutionPlanImpl.execute(ExecutionPlanImpl.scala:34)", "org.neo4j.cypher.ExecutionEngine.execute(ExecutionEngine.scala:72)", "org.neo4j.cypher.ExecutionEngine.execute(ExecutionEngine.scala:68)", "org.neo4j.cypher.javacompat.ExecutionEngine.execute(ExecutionEngine.java:73)", "org.neo4j.server.rest.web.CypherService.cypher(CypherService.java:71)", "sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)", "sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)", "sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)", "java.lang.reflect.Method.invoke(Method.java:597)", "com.sun.jersey.spi.container.JavaMethodInvokerFactory$1.invoke(JavaMethodInvokerFactory.java:60)", "com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$ResponseOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:205)", "com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:75)", "com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:288)", "com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108)", "com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)", "com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84)", "com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1469)", "com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1400)", "com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1349)", "com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1339)", "com.sun.jersey.spi.container.servlet.WebComponent.service(WebComponent.java:416)", "com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:537)", "com.sun.jersey.spi.container.servlet.ServletContainer.service(ServletContainer.java:699)", "javax.servlet.http.HttpServlet.service(HttpServlet.java:820)", "org.mortbay.jetty.servlet.ServletHolder.handle(ServletHolder.java:511)", "org.mortbay.jetty.servlet.ServletHandler.handle(ServletHandler.java:390)", "org.mortbay.jetty.servlet.SessionHandler.handle(SessionHandler.java:182)", "org.mortbay.jetty.handler.ContextHandler.handle(ContextHandler.java:765)", "org.mortbay.jetty.handler.HandlerCollection.handle(HandlerCollection.java:114)", "org.mortbay.jetty.handler.HandlerWrapper.handle(HandlerWrapper.java:152)", "org.mortbay.jetty.Server.handle(Server.java:326)", "org.mortbay.jetty.HttpConnection.handleRequest(HttpConnection.java:542)", "org.mortbay.jetty.HttpConnection$RequestHandler.content(HttpConnection.java:943)", "org.mortbay.jetty.HttpParser.parseNext(HttpParser.java:756)", "org.mortbay.jetty.HttpParser.parseAvailable(HttpParser.java:212)", "org.mortbay.jetty.HttpConnection.handle(HttpConnection.java:404)", "org.mortbay.io.nio.SelectChannelEndPoint.run(SelectChannelEndPoint.java:410)", "org.mortbay.thread.QueuedThreadPool$PoolThread.run(QueuedThreadPool.java:582)" ]
}