25.4. Cypher queries

25.4.1. Send queries with parameters
25.4.2. Send a Query
25.4.3. Return paths
25.4.4. Nested results
25.4.5. Retrieve query meta data
25.4.6. Profile a query
25.4.7. Server errors

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

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

25.4.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(341) match x -[r]-> n return type(r), n.name?, n.age?

Figure 25.4. Final Graph


Example request

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

25.4.3. Return paths

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

START x  = node(346) match path = (x--friend) return path, friend.name

Figure 25.5. Final Graph


Example request

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

25.4.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(343,342) return collect(n.name)

Figure 25.6. Final Graph


Example request

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

Example response

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

25.4.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(344) set n:foo remove n:bar return labels(n)

Figure 25.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(344) 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,
    "indexes_added" : 0,
    "properties_set" : 0,
    "constraints_removed" : 0,
    "indexes_removed" : 0,
    "labels_removed" : 1,
    "constraints_added" : 0,
    "labels_added" : 1,
    "nodes_created" : 0,
    "contains_updates" : true
  }
}

25.4.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(336) match x -[r]-> n return type(r), n.name?, n.age?

Figure 25.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(336) 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
  }
}

25.4.7. Server errors

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

START x = node(326) return x.dummy

Figure 25.9. Final Graph


Example request

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

Example response

  • 400: Bad Request
  • Content-Type: application/json
{
  "message": "The property \u0027dummy\u0027 does not exist on Node[326]",
  "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: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: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:83)",
    "org.neo4j.server.rest.repr.MappingRepresentation.serialize(MappingRepresentation.java:42)",
    "org.neo4j.server.rest.repr.OutputFormat.assemble(OutputFormat.java:185)",
    "org.neo4j.server.rest.repr.OutputFormat.formatRepresentation(OutputFormat.java:133)",
    "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:96)",
    "java.lang.reflect.Method.invoke(Method.java:601)",
    "org.neo4j.server.rest.security.SecurityFilter.doFilter(SecurityFilter.java:112)"
  ]
}