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(23) 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(23) 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(27) 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/27", "nodes" : [ "http://localhost:7474/db/data/node/27", "http://localhost:7474/db/data/node/26" ], "length" : 1, "relationships" : [ "http://localhost:7474/db/data/relationship/8" ], "end" : "http://localhost:7474/db/data/node/26" }, "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(36,35) 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(25) return x.dummy","params": {}},
Example response
400:
Bad Request
Content-Type:
application/json
{ "message" : "The property 'dummy' does not exist on Node[25]", "exception" : "org.neo4j.server.rest.repr.BadInputException: The property 'dummy' does not exist on Node[25]", "stacktrace" : [ "org.neo4j.server.rest.repr.RepresentationExceptionHandlingIterable.exceptionOnHasNext(RepresentationExceptionHandlingIterable.java:51)", "org.neo4j.helpers.collection.ExceptionHandlingIterable$1.hasNext(ExceptionHandlingIterable.java:61)", "org.neo4j.helpers.collection.IteratorWrapper.hasNext(IteratorWrapper.java:42)", "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.ListRepresentation.putTo(ListRepresentation.java:73)", "org.neo4j.server.rest.repr.ObjectRepresentation$PropertyGetter.putTo(ObjectRepresentation.java:133)", "org.neo4j.server.rest.repr.ObjectRepresentation.serialize(ObjectRepresentation.java:144)", "org.neo4j.server.rest.repr.MappingRepresentation.serialize(MappingRepresentation.java:42)", "org.neo4j.server.rest.repr.OutputFormat.format(OutputFormat.java:146)", "org.neo4j.server.rest.repr.OutputFormat.formatRepresentation(OutputFormat.java:114)", "org.neo4j.server.rest.repr.OutputFormat.response(OutputFormat.java:104)", "org.neo4j.server.rest.repr.OutputFormat.ok(OutputFormat.java:52)", "org.neo4j.server.rest.web.CypherService.cypher(CypherService.java:68)", "java.lang.reflect.Method.invoke(Method.java:597)" ] }
Copyright © 2012 Neo Technology