18.3. Cypher queries

18.3.1. Send queries with parameters
18.3.2. Send a Query
18.3.3. Return paths
18.3.4. Nested results
18.3.5. Server errors

The Neo4j REST API allows querying with Cypher, see 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.

[Tip]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 18.3.1, “Send queries with parameters” for details.

18.3.1. 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.3. 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
{
  "columns" : [ "TYPE(r)" ],
  "data" : [ [ "know" ] ]
}

18.3.2. 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(168)
MATCH x -[r]-> n
RETURN type(r), n.name?, n.age?

Figure 18.4. Final Graph


Example request

  • POST http://localhost:7474/db/data/cypher
  • Accept: application/json
  • Content-Type: application/json
{
  "query" : "start x  = node(168) 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 ] ]
}

18.3.3. Return paths

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

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

Figure 18.5. Final Graph


Example request

  • POST http://localhost:7474/db/data/cypher
  • Accept: application/json
  • Content-Type: application/json
{
  "query" : "start x  = node(175) 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/175",
    "nodes" : [ "http://localhost:7474/db/data/node/175", "http://localhost:7474/db/data/node/174" ],
    "length" : 1,
    "relationships" : [ "http://localhost:7474/db/data/relationship/113" ],
    "end" : "http://localhost:7474/db/data/node/174"
  }, "you" ] ]
}

18.3.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(184,183)
RETURN collect(n.name)

Figure 18.6. Final Graph


Example request

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

Example response

  • 200: OK
  • Content-Type: application/json
{
  "columns" : [ "collect(n.name)" ],
  "data" : [ [ [ "I", "you" ] ] ]
}

18.3.5. Server errors

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

START x = node(173)
RETURN x.dummy

Figure 18.7. Final Graph


Example request

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

Example response

  • 400: Bad Request
  • Content-Type: application/json
{
  "message" : "The property 'dummy' does not exist on Node[173]",
  "exception" : "BadInputException",
  "stacktrace" : [ "org.neo4j.server.rest.repr.RepresentationExceptionHandlingIterable.exceptionOnNext(RepresentationExceptionHandlingIterable.java:40)", "org.neo4j.helpers.collection.ExceptionHandlingIterable$1.next(ExceptionHandlingIterable.java:70)", "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:50)", "org.neo4j.server.rest.repr.MappingRepresentation.serialize(MappingRepresentation.java:42)", "org.neo4j.server.rest.repr.OutputFormat.format(OutputFormat.java:170)", "org.neo4j.server.rest.repr.OutputFormat.formatRepresentation(OutputFormat.java:120)", "org.neo4j.server.rest.repr.OutputFormat.response(OutputFormat.java:107)", "org.neo4j.server.rest.repr.OutputFormat.ok(OutputFormat.java:55)", "org.neo4j.server.rest.web.CypherService.cypher(CypherService.java:68)", "java.lang.reflect.Method.invoke(Method.java:597)" ]
}