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
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
{ "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(340) 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(340) match x -[r]-> n return type(r), n.name?, n.age?", "params" : { } }
Example response
200:
OK
Content-Type:
application/json
{ "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(344) 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(344) match path = (x--friend) return path, friend.name", "params" : { } }
Example response
200:
OK
Content-Type:
application/json
{ "columns" : [ "path", "friend.name" ], "data" : [ [ { "start" : "http://localhost:7474/db/data/node/344", "nodes" : [ "http://localhost:7474/db/data/node/344", "http://localhost:7474/db/data/node/343" ], "length" : 1, "relationships" : [ "http://localhost:7474/db/data/relationship/198" ], "end" : "http://localhost:7474/db/data/node/343" }, "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(342,341) RETURN collect(n.name)
Example request
POST
http://localhost:7474/db/data/cypher
Accept:
application/json
Content-Type:
application/json
{ "query" : "start n = node(342,341) return collect(n.name)", "params" : { } }
Example response
200:
OK
Content-Type:
application/json
{ "columns" : [ "collect(n.name)" ], "data" : [ [ [ "I", "you" ] ] ] }
By passing in an extra parameter, you can ask the cypher executor to return a profile of the query as it is executed. This can help in locating bottlenecks.
START x = node(335) MATCH x -[r]-> n RETURN type(r), n.name?, n.age?
Example request
POST
http://localhost:7474/db/data/cypher?profile=true
Accept:
application/json
Content-Type:
application/json
{ "query" : "start x = node(335) match x -[r]-> n return type(r), n.name?, n.age?", "params" : { } }
Example response
200:
OK
Content-Type:
application/json
{ "columns" : [ "type(r)", "n.name?", "n.age?" ], "data" : [ [ "know", "him", 25 ], [ "know", "you", null ] ], "plan" : { "args" : { "returnItemNames" : [ "type(r)", "n.name?", "n.age?" ], "_rows" : 2, "_db_hits" : 0, "symKeys" : [ "x", "n", "n.name?", "n.age?", "type(r)", "r" ] }, "dbHits" : 0, "name" : "ColumnFilter", "children" : [ { "args" : { "_rows" : 2, "_db_hits" : 4, "exprKeys" : [ "type(r)", "n.name?", "n.age?" ], "symKeys" : [ "n", "x", "r" ] }, "dbHits" : 4, "name" : "Extract", "children" : [ { "args" : { "trail" : "(x)-[r WHERE true AND true]->(n)", "_rows" : 2, "_db_hits" : 3 }, "dbHits" : 3, "name" : "TraversalMatcher", "children" : [ { "args" : { "_rows" : 1, "_db_hits" : 0 }, "dbHits" : 0, "name" : "ParameterPipe", "children" : [ ], "rows" : 1 } ], "rows" : 2 } ], "rows" : 2 } ], "rows" : 2 } }
Errors on the server will be reported as a JSON-formatted stacktrace and message.
START x = node(325) RETURN x.dummy
Example request
POST
http://localhost:7474/db/data/cypher
Accept:
application/json
Content-Type:
application/json
{ "query" : "start x = node(325) return x.dummy", "params" : { } }
Example response
400:
Bad Request
Content-Type:
application/json
{ "message": "The property \u0027dummy\u0027 does not exist on Node[325]", "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:135)", "org.neo4j.cypher.PipeExecutionResult.next(PipeExecutionResult.scala:32)", "scala.collection.Iterator$$anon$11.next(Iterator.scala:328)", "scala.collection.convert.Wrappers$IteratorWrapper.next(Wrappers.scala:30)", "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:58)", "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:42)", "org.neo4j.server.rest.repr.OutputFormat.assemble(OutputFormat.java:179)", "org.neo4j.server.rest.repr.OutputFormat.formatRepresentation(OutputFormat.java:131)", "org.neo4j.server.rest.repr.OutputFormat.response(OutputFormat.java:117)", "org.neo4j.server.rest.repr.OutputFormat.ok(OutputFormat.java:55)", "org.neo4j.server.rest.web.CypherService.cypher(CypherService.java:95)", "java.lang.reflect.Method.invoke(Method.java:597)" ] }
Copyright © 2013 Neo Technology