The Neo4j REST API allows querying with the Chapter 16, 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
.
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(210) MATCH x -[r]-> n RETURN type(r), n.name?, n.age?
Example request
POST
http://localhost:7474/db/data/cypher
Accept:
application/json
Content-Type:
application/json
{"query": "start x = node(210) 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?" ] }
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
Example request
POST
http://localhost:7474/db/data/cypher
Accept:
application/json
Content-Type:
application/json
{"query": "start x = node(214) 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/214", "nodes" : [ "http://localhost:7474/db/data/node/214", "http://localhost:7474/db/data/node/213" ], "length" : 1, "relationships" : [ "http://localhost:7474/db/data/relationship/92" ], "end" : "http://localhost:7474/db/data/node/213" }, "you" ] ], "columns" : [ "path", "friend.name" ] }
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)
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)" ] }
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)
Example request
POST
http://localhost:7474/db/data/cypher
Accept:
application/json
Content-Type:
application/json
{"query": "start n = node(223,222) return collect(n.name)","params": {}},
Example response
200:
OK
Content-Type:
application/json
{ "data" : [ [ [ "I", "you" ] ] ], "columns" : [ "collect(n.name)" ] }
Errors on the server will be reported as a JSON-formatted stacktrace and message.
START x = node(%I%) RETURN x.dummy
Example request
POST
http://localhost:7474/db/data/cypher
Accept:
application/json
Content-Type:
application/json
{"query": "start x = node(212) return x.dummy","params": {}},
Example response
400:
Bad Request
Content-Type:
application/json
{ "message" : "The property 'dummy' does not exist on Node[212]", "exception" : "org.neo4j.cypher.EntityNotFoundException: The property 'dummy' does not exist on Node[212]", "stacktrace" : [ "org.neo4j.cypher.internal.commands.Property.compute(Expression.scala:211)", "org.neo4j.cypher.internal.commands.Expression$$anonfun$apply$1.apply(Expression.scala:30)", "scala.collection.MapLike$class.getOrElse(MapLike.scala:122)", "scala.collection.mutable.HashMap.getOrElse(HashMap.scala:43)", "org.neo4j.cypher.internal.commands.Expression.apply(Expression.scala:30)", "org.neo4j.cypher.internal.pipes.ExtractPipe$$anonfun$createResults$1$$anonfun$apply$1.apply(ExtractPipe.scala:39)", "org.neo4j.cypher.internal.pipes.ExtractPipe$$anonfun$createResults$1$$anonfun$apply$1.apply(ExtractPipe.scala:39)", "scala.collection.LinearSeqOptimized$class.foreach(LinearSeqOptimized.scala:59)", "scala.collection.immutable.List.foreach(List.scala:45)", "org.neo4j.cypher.internal.pipes.ExtractPipe$$anonfun$createResults$1.apply(ExtractPipe.scala:39)", "org.neo4j.cypher.internal.pipes.ExtractPipe$$anonfun$createResults$1.apply(ExtractPipe.scala:38)", "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:38)", "org.neo4j.cypher.internal.pipes.ColumnFilterPipe.createResults(ColumnFilterPipe.scala:37)", "org.neo4j.cypher.internal.executionplan.ExecutionPlanImpl$$anonfun$3$$anonfun$apply$1.apply(ExecutionPlanImpl.scala:62)", "org.neo4j.cypher.internal.executionplan.ExecutionPlanImpl$$anonfun$3$$anonfun$apply$1.apply(ExecutionPlanImpl.scala:62)", "org.neo4j.cypher.PipeExecutionResult.immutableResult(PipeExecutionResult.scala:36)", "org.neo4j.cypher.PipeExecutionResult.iterator(PipeExecutionResult.scala:138)", "org.neo4j.cypher.PipeExecutionResult.hasNext(PipeExecutionResult.scala:140)", "scala.collection.Iterator$$anon$19.hasNext(Iterator.scala:334)", "scala.collection.JavaConversions$IteratorWrapper.hasNext(JavaConversions.scala:562)", "org.neo4j.server.rest.repr.CypherResultRepresentation.createResultRepresentation(CypherResultRepresentation.java:60)", "org.neo4j.server.rest.repr.CypherResultRepresentation.<init>(CypherResultRepresentation.java:40)", "org.neo4j.server.rest.web.CypherService.cypher(CypherService.java:68)", "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)" ] }
Copyright © 2012 Neo Technology