17.3. Cypher queries

17.3.1. Send queries with parameters
17.3.2. Send a Query
17.3.3. Return paths
17.3.4. Nested results
17.3.5. Retrieve query meta data
17.3.6. Profile a query
17.3.7. Server errors

The Neo4j REST API allows querying with Cypher, see Chapter 14, 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 17.3.1, “Send queries with parameters” for details.

17.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 17.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" ] ]
}

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

Figure 17.4. Final Graph


Example request

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

17.3.3. Return paths

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

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

Figure 17.5. Final Graph


Example request

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

17.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(204,203)
RETURN collect(n.name)

Figure 17.6. Final Graph


Example request

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

Example response

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

17.3.5. Retrieve query meta data

By passing in an additional GET header when you execute cypher queries, meta data about the query will be returned, such as how many labels were added or removed by the query.

START n = node(205)
SET n:foo
REMOVE n:bar
RETURN labels(n)

Figure 17.7. Final Graph


Example request

  • POST http://localhost:7474/db/data/cypher?includeStats=true
  • Accept: application/json
  • Content-Type: application/json
{
  "query" : "start n = node(205) set n:foo remove n:bar return labels(n)",
  "params" : {
  }
}

Example response

  • 200: OK
  • Content-Type: application/json
{
  "columns" : [ "labels(n)" ],
  "data" : [ [ [ "foo" ] ] ],
  "stats" : {
    "relationships_created" : 0,
    "nodes_deleted" : 0,
    "relationship_deleted" : 0,
    "properties_set" : 0,
    "labels_removed" : 1,
    "labels_added" : 1,
    "nodes_created" : 0,
    "contains_updates" : true
  }
}

17.3.6. Profile a query

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

Figure 17.8. Final Graph


Example request

  • POST http://localhost:7474/db/data/cypher?profile=true
  • Accept: application/json
  • Content-Type: application/json
{
  "query" : "start x  = node(197) 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" : [ ],
        "rows" : 2
      } ],
      "rows" : 2
    } ],
    "rows" : 2
  }
}

17.3.7. Server errors

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

START x = node(187)
RETURN x.dummy

Figure 17.9. Final Graph


Example request

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

Example response

  • 400: Bad Request
  • Content-Type: application/json
{
  "message": "The property \u0027dummy\u0027 does not exist on Node[187]",
  "exception": "EntityNotFoundException",
  "fullname": "org.neo4j.cypher.EntityNotFoundException",
  "stacktrace": [
    "org.neo4j.cypher.internal.helpers.MapSupport$PropertyContainerMap$$anonfun$apply$1.apply(MapSupport.scala:67)",
    "org.neo4j.cypher.internal.helpers.MapSupport$PropertyContainerMap$$anonfun$apply$1.apply(MapSupport.scala:67)",
    "scala.Option.getOrElse(Option.scala:120)",
    "org.neo4j.cypher.internal.helpers.MapSupport$PropertyContainerMap.apply(MapSupport.scala:67)",
    "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:84)",
    "org.neo4j.cypher.internal.ClosingIterator.next(ClosingIterator.scala:43)",
    "org.neo4j.cypher.PipeExecutionResult.next(PipeExecutionResult.scala:144)",
    "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:79)",
    "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:87)",
    "java.lang.reflect.Method.invoke(Method.java:597)"
  ]
}