The Neo4j REST API allows querying with Cypher, see Part III, “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
.
Tip | |
---|---|
In order to speed up queries in repeated scenarios, try not to use literals but replace them with parameters wherever possible in order to let the server cache query plans, see Section 22.3.1, “Send queries with parameters” for details. |
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; charset=UTF-8
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; charset=UTF-8
{ "columns" : [ "TYPE(r)" ], "data" : [ [ "know" ] ] }
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(101) MATCH x -[r]-> n RETURN type(r), n.name?, n.age?
Example request
POST
http://localhost:7474/db/data/cypher
Accept:
application/json; charset=UTF-8
Content-Type:
application/json
{ "query" : "start x = node(101) match x -[r]-> n return type(r), n.name?, n.age?", "params" : { } }
Example response
200:
OK
Content-Type:
application/json; charset=UTF-8
{ "columns" : [ "type(r)", "n.name?", "n.age?" ], "data" : [ [ "know", "him", 25 ], [ "know", "you", null ] ] }
Paths can be returned together with other return types by just specifying returns.
START x = node(105) MATCH path = (x--friend) RETURN path, friend.name
Example request
POST
http://localhost:7474/db/data/cypher
Accept:
application/json; charset=UTF-8
Content-Type:
application/json
{ "query" : "start x = node(105) match path = (x--friend) return path, friend.name", "params" : { } }
Example response
200:
OK
Content-Type:
application/json; charset=UTF-8
{ "columns" : [ "path", "friend.name" ], "data" : [ [ { "start" : "http://localhost:7474/db/data/node/105", "nodes" : [ "http://localhost:7474/db/data/node/105", "http://localhost:7474/db/data/node/104" ], "length" : 1, "relationships" : [ "http://localhost:7474/db/data/relationship/103" ], "end" : "http://localhost:7474/db/data/node/104" }, "you" ] ] }
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(103,102) RETURN collect(n.name)
Example request
POST
http://localhost:7474/db/data/cypher
Accept:
application/json; charset=UTF-8
Content-Type:
application/json
{ "query" : "start n = node(103,102) return collect(n.name)", "params" : { } }
Example response
200:
OK
Content-Type:
application/json; charset=UTF-8
{ "columns" : [ "collect(n.name)" ], "data" : [ [ [ "I", "you" ] ] ] }
Errors on the server will be reported as a JSON-formatted stacktrace and message.
START x = node(86) RETURN x.dummy
Example request
POST
http://localhost:7474/db/data/cypher
Accept:
application/json; charset=UTF-8
Content-Type:
application/json
{ "query" : "start x = node(86) return x.dummy", "params" : { } }
Example response
400:
Bad Request
Content-Type:
application/json; charset=UTF-8
{ "message": "The property \u0027dummy\u0027 does not exist on Node[86]", "exception": "EntityNotFoundException", "fullname": "org.neo4j.cypher.EntityNotFoundException", "stacktrace": [ "org.neo4j.cypher.internal.helpers.MapSupport$PropertyContainerMap$$anonfun$apply$1.apply(MapSupport.scala:64)", "org.neo4j.cypher.internal.helpers.MapSupport$PropertyContainerMap$$anonfun$apply$1.apply(MapSupport.scala:64)", "scala.Option.getOrElse(Option.scala:120)", "org.neo4j.cypher.internal.helpers.MapSupport$PropertyContainerMap.apply(MapSupport.scala:64)", "org.neo4j.cypher.internal.helpers.MapSupport$PropertyContainerMap.apply(MapSupport.scala:52)", "org.neo4j.cypher.internal.commands.expressions.Property.apply(Property.scala:31)", "org.neo4j.cypher.internal.pipes.ExtractPipe$$anonfun$internalCreateResults$1$$anonfun$apply$1.apply(ExtractPipe.scala:47)", "org.neo4j.cypher.internal.pipes.ExtractPipe$$anonfun$internalCreateResults$1$$anonfun$apply$1.apply(ExtractPipe.scala:45)", "scala.collection.immutable.Map$Map1.foreach(Map.scala:109)", "org.neo4j.cypher.internal.pipes.ExtractPipe$$anonfun$internalCreateResults$1.apply(ExtractPipe.scala:45)", "org.neo4j.cypher.internal.pipes.ExtractPipe$$anonfun$internalCreateResults$1.apply(ExtractPipe.scala:44)", "scala.collection.Iterator$$anon$11.next(Iterator.scala:328)", "scala.collection.Iterator$$anon$11.next(Iterator.scala:328)", "org.neo4j.cypher.internal.ClosingIterator$$anonfun$next$1.apply(ClosingIterator.scala:44)", "org.neo4j.cypher.internal.ClosingIterator.failIfThrows(ClosingIterator.scala:86)", "org.neo4j.cypher.internal.ClosingIterator.next(ClosingIterator.scala:43)", "org.neo4j.cypher.PipeExecutionResult.next(PipeExecutionResult.scala:159)", "org.neo4j.cypher.PipeExecutionResult.next(PipeExecutionResult.scala:33)", "scala.collection.Iterator$$anon$11.next(Iterator.scala:328)", "scala.collection.convert.Wrappers$IteratorWrapper.next(Wrappers.scala:30)", "org.neo4j.cypher.PipeExecutionResult$$anon$1.next(PipeExecutionResult.scala:75)", "org.neo4j.helpers.collection.ExceptionHandlingIterable$1.next(ExceptionHandlingIterable.java:67)", "org.neo4j.helpers.collection.IteratorWrapper.next(IteratorWrapper.java:47)", "org.neo4j.server.rest.repr.ListRepresentation.serialize(ListRepresentation.java:64)", "org.neo4j.server.rest.repr.Serializer.serialize(Serializer.java:75)", "org.neo4j.server.rest.repr.MappingSerializer.putList(MappingSerializer.java:61)", "org.neo4j.server.rest.repr.CypherResultRepresentation.serialize(CypherResultRepresentation.java:57)", "org.neo4j.server.rest.repr.MappingRepresentation.serialize(MappingRepresentation.java:41)", "org.neo4j.server.rest.repr.OutputFormat.assemble(OutputFormat.java:180)", "org.neo4j.server.rest.repr.OutputFormat.formatRepresentation(OutputFormat.java:132)", "org.neo4j.server.rest.repr.OutputFormat.response(OutputFormat.java:119)", "org.neo4j.server.rest.repr.OutputFormat.ok(OutputFormat.java:57)", "org.neo4j.server.rest.web.CypherService.cypher(CypherService.java:95)", "java.lang.reflect.Method.invoke(Method.java:597)" ] }
Copyright © 2014 Neo Technology