Advanced Queries: Cypher

Search
Feedback
$ curl -i -HX-User:admin -HX-Password:admin -XPOST http://localhost:8082/structr/rest/cypher -d '{
        "query": "START n=node(4) RETURN n"
}'

This should return the first user node that is created when Structr first starts.

HTTP/1.1 200 OK
Set-Cookie: JSESSIONID=mv6fedlakpg6k0pry6wupyyu;Path=/
Expires: Thu, 01 Jan 1970 00:00:00 GMT
Content-Type: application/json; charset=UTF-8
Vary: Accept-Encoding, User-Agent
Content-Length: 269
Server: Jetty(9.1.3.v20140225)

{
  "result_count": 1,
  "result": {
    "id": "f02e59a47dc9492da3e6cb7fb6b3ac25",
    "type": "User",
    "name": "admin",
    "salutation": null,
    "firstName": null,
    "middleNameOrInitial": null,
    "lastName": null
  },
  "serialization_time": "0.000201955"
}
$ curl -i -HX-User:admin -HX-Password:admin -XPOST http://localhost:8082/structr/rest/cypher -d '{
        "query": "START n=node(4) MATCH n-[r:OWNS]->m RETURN r LIMIT 1"
}'

This query returns only the relationship between the user and a file.

HTTP/1.1 200 OK
Set-Cookie: JSESSIONID=1c8quzyvqp9i0qxvysnl9sby0;Path=/
Expires: Thu, 01 Jan 1970 00:00:00 GMT
Content-Type: application/json; charset=UTF-8
Vary: Accept-Encoding, User-Agent
Content-Length: 286
Server: Jetty(9.1.3.v20140225)

{
  "result_count": 1,
  "result": {
    "id": "73e88da5ff0b45b7819816a3b7cb33f6",
    "type": "PrincipalOwnsNode",
    "relType": "OWNS",
    "sourceId": "f02e59a47dc9492da3e6cb7fb6b3ac25",
    "targetId": "380d9da41ceb46a99aa17b70e3d5bf87"
  },
  "serialization_time": "0.000346881"
}
$ curl -i -HX-User:admin -HX-Password:admin -XPOST http://localhost:8082/structr/rest/cypher -d '{
        "query": "START n=node(4) RETURN n.name"
}'

You can also access individual properties separately:

HTTP/1.1 200 OK
Set-Cookie: JSESSIONID=3vf9wcqhl3b0hifc3o2458kj;Path=/
Expires: Thu, 01 Jan 1970 00:00:00 GMT
Content-Type: application/json; charset=UTF-8
Vary: Accept-Encoding, User-Agent
Content-Length: 103
Server: Jetty(9.1.3.v20140225)

{
  "result_count": 1,
  "result": {
    "n.name": "admin"
  },
  "serialization_time": "0.000028697"
}
$ curl -i -HX-User:admin -HX-Password:admin -XPOST http://localhost:8082/structr/rest/cypher -d '{
        "query": "START u=node(4) MATCH u-[r:OWNS]->f RETURN u, r, f, f.size LIMIT 1"
}

You can even get Structr to return a mixed set of nodes, relationships and single property entries.

HTTP/1.1 200 OK
Set-Cookie: JSESSIONID=1w6olls7g3r561e7ltws1qy57f;Path=/
Expires: Thu, 01 Jan 1970 00:00:00 GMT
Content-Type: application/json; charset=UTF-8
Vary: Accept-Encoding, User-Agent
Content-Length: 1009
Server: Jetty(9.1.3.v20140225)

{
  "result_count": 4,
  "result": [
    {
      "id": "380d9da41ceb46a99aa17b70e3d5bf87",
      "type": "File",
      "name": "test10.html",
      "contentType": "text/html",
      "size": 10,
      "url": null,
      "owner": {
        "id": "f02e59a47dc9492da3e6cb7fb6b3ac25",
        "type": "User",
        "name": "admin",
        "salutation": null,
        "firstName": null,
        "middleNameOrInitial": null,
        "lastName": null
      },
      "path": "/test10.html"
    },
    {
      "f.size": 10
    },
    {
      "id": "f02e59a47dc9492da3e6cb7fb6b3ac25",
      "type": "User",
      "name": "admin",
      "salutation": null,
      "firstName": null,
      "middleNameOrInitial": null,
      "lastName": null
    },
    {
      "id": "73e88da5ff0b45b7819816a3b7cb33f6",
      "type": "PrincipalOwnsNode",
      "relType": "OWNS",
      "sourceId": "f02e59a47dc9492da3e6cb7fb6b3ac25",
      "targetId": "380d9da41ceb46a99aa17b70e3d5bf87"
    }
  ],
  "serialization_time": "0.000790774"
}

If you want to use Cypher over REST, you can use the Cypher Query Resource to obtain nodes and relationships from a single query. The Cypher resource in Structr is available at /cypher and can be used to execute arbitrary Cypher commands. You should be very aware of the fact that Cypher accesses the database on a lower level than Structr, so Structr has no control over modifications or read operations that happen using Cypher.

The use of Cypher via REST bypasses Structr's security system and the use of it in your application poses a severe security risk.

Having said that, Structr limits access to the Cypher query resource to the superuser and administrative users (users that have the isAdmin flag set).

You can access the Cypher query resource by issuing an HTTP POST request to the /cypher resource with the desired query in the request body. The following examples illustrate the use of the resource.

Graph-Browser

About this article
Last change 2016-03-04
Topics Structr 2.0