21.13. Node labels

Adding a label to a node

Figure 21.50. Final Graph

Example request

  • POST http://localhost:7474/db/data/node/10127/labels
  • Accept: application/json; charset=UTF-8
  • Content-Type: application/json
"Person"

Example response

  • 204: No Content

Adding multiple labels to a node

Figure 21.51. Final Graph

Example request

  • POST http://localhost:7474/db/data/node/10138/labels
  • Accept: application/json; charset=UTF-8
  • Content-Type: application/json
[ "Person", "Actor" ]

Example response

  • 204: No Content

Adding a label with an invalid name

Labels with empty names are not allowed, however, all other valid strings are accepted as label names. Adding an invalid label to a node will lead to a HTTP 400 response.

Figure 21.52. Final Graph

Example request

  • POST http://localhost:7474/db/data/node/10145/labels
  • Accept: application/json; charset=UTF-8
  • Content-Type: application/json
""

Example response

  • 400: Bad Request
  • Content-Type: application/json; charset=UTF-8
{
  "message": "Unable to add label, see nested exception.",
  "exception": "BadInputException",
  "fullname": "org.neo4j.server.rest.repr.BadInputException",
  "stacktrace": [
    "org.neo4j.server.rest.web.DatabaseActions.addLabelToNode(DatabaseActions.java:320)",
    "org.neo4j.server.rest.web.RestfulGraphDatabase.addNodeLabel(RestfulGraphDatabase.java:432)",
    "java.lang.reflect.Method.invoke(Method.java:606)",
    "org.neo4j.server.rest.transactional.TransactionalRequestDispatcher.dispatch(TransactionalRequestDispatcher.java:139)",
    "java.lang.Thread.run(Thread.java:745)"
  ],
  "cause": {
    "message": "Invalid label name \u0027\u0027.",
    "errors": [
      {
        "message": "Invalid label name \u0027\u0027.",
        "code": "Neo.ClientError.Schema.IllegalTokenName"
      }
    ],
    "cause": {
      "message": "\u0027\u0027 is not a valid token name. Only non-null, non-empty strings are allowed.",
      "errors": [
        {
          "message": "\u0027\u0027 is not a valid token name. Only non-null, non-empty strings are allowed.",
          "code": "Neo.ClientError.Schema.IllegalTokenName"
        }
      ],
      "exception": "IllegalTokenNameException",
      "stacktrace": [
        "org.neo4j.kernel.impl.api.DataIntegrityValidatingStatementOperations.checkValidTokenName(DataIntegrityValidatingStatementOperations.java:190)",
        "org.neo4j.kernel.impl.api.DataIntegrityValidatingStatementOperations.labelGetOrCreateForName(DataIntegrityValidatingStatementOperations.java:83)",
        "org.neo4j.kernel.impl.api.OperationsFacade.labelGetOrCreateForName(OperationsFacade.java:512)",
        "org.neo4j.kernel.impl.core.NodeProxy.addLabel(NodeProxy.java:501)",
        "org.neo4j.server.rest.web.DatabaseActions.addLabelToNode(DatabaseActions.java:315)",
        "org.neo4j.server.rest.web.RestfulGraphDatabase.addNodeLabel(RestfulGraphDatabase.java:432)",
        "java.lang.reflect.Method.invoke(Method.java:606)",
        "org.neo4j.server.rest.transactional.TransactionalRequestDispatcher.dispatch(TransactionalRequestDispatcher.java:139)",
        "java.lang.Thread.run(Thread.java:745)"
      ],
      "fullname": "org.neo4j.kernel.api.exceptions.schema.IllegalTokenNameException"
    },
    "exception": "ConstraintViolationException",
    "stacktrace": [
      "org.neo4j.kernel.impl.core.NodeProxy.addLabel(NodeProxy.java:512)",
      "org.neo4j.server.rest.web.DatabaseActions.addLabelToNode(DatabaseActions.java:315)",
      "org.neo4j.server.rest.web.RestfulGraphDatabase.addNodeLabel(RestfulGraphDatabase.java:432)",
      "java.lang.reflect.Method.invoke(Method.java:606)",
      "org.neo4j.server.rest.transactional.TransactionalRequestDispatcher.dispatch(TransactionalRequestDispatcher.java:139)",
      "java.lang.Thread.run(Thread.java:745)"
    ],
    "fullname": "org.neo4j.graphdb.ConstraintViolationException"
  },
  "errors": [
    {
      "message": "Unable to add label, see nested exception.",
      "code": "Neo.ClientError.Schema.IllegalTokenName"
    }
  ]
}

Replacing labels on a node

This removes any labels currently on a node, and replaces them with the labels passed in as the request body.

Figure 21.53. Final Graph

Example request

  • PUT http://localhost:7474/db/data/node/10128/labels
  • Accept: application/json; charset=UTF-8
  • Content-Type: application/json
[ "Actor", "Director" ]

Example response

  • 204: No Content

Removing a label from a node

Figure 21.54. Final Graph

Example request

  • DELETE http://localhost:7474/db/data/node/10129/labels/Person
  • Accept: application/json; charset=UTF-8

Example response

  • 204: No Content

Listing labels for a node

Figure 21.55. Final Graph

Example request

  • GET http://localhost:7474/db/data/node/10134/labels
  • Accept: application/json; charset=UTF-8

Example response

  • 200: OK
  • Content-Type: application/json; charset=UTF-8
[ "Director", "Actor" ]

Get all nodes with a label

Figure 21.56. Final Graph

Example request

  • GET http://localhost:7474/db/data/label/Actor/nodes
  • Accept: application/json; charset=UTF-8

Example response

  • 200: OK
  • Content-Type: application/json; charset=UTF-8
[ {
  "labels" : "http://localhost:7474/db/data/node/10140/labels",
  "outgoing_relationships" : "http://localhost:7474/db/data/node/10140/relationships/out",
  "data" : {
    "name" : "Clint Eastwood"
  },
  "traverse" : "http://localhost:7474/db/data/node/10140/traverse/{returnType}",
  "all_typed_relationships" : "http://localhost:7474/db/data/node/10140/relationships/all/{-list|&|types}",
  "self" : "http://localhost:7474/db/data/node/10140",
  "property" : "http://localhost:7474/db/data/node/10140/properties/{key}",
  "outgoing_typed_relationships" : "http://localhost:7474/db/data/node/10140/relationships/out/{-list|&|types}",
  "properties" : "http://localhost:7474/db/data/node/10140/properties",
  "incoming_relationships" : "http://localhost:7474/db/data/node/10140/relationships/in",
  "extensions" : { },
  "create_relationship" : "http://localhost:7474/db/data/node/10140/relationships",
  "paged_traverse" : "http://localhost:7474/db/data/node/10140/paged/traverse/{returnType}{?pageSize,leaseTime}",
  "all_relationships" : "http://localhost:7474/db/data/node/10140/relationships/all",
  "incoming_typed_relationships" : "http://localhost:7474/db/data/node/10140/relationships/in/{-list|&|types}",
  "metadata" : {
    "id" : 10140,
    "labels" : [ "Director", "Actor" ]
  }
}, {
  "labels" : "http://localhost:7474/db/data/node/10141/labels",
  "outgoing_relationships" : "http://localhost:7474/db/data/node/10141/relationships/out",
  "data" : {
    "name" : "Donald Sutherland"
  },
  "traverse" : "http://localhost:7474/db/data/node/10141/traverse/{returnType}",
  "all_typed_relationships" : "http://localhost:7474/db/data/node/10141/relationships/all/{-list|&|types}",
  "self" : "http://localhost:7474/db/data/node/10141",
  "property" : "http://localhost:7474/db/data/node/10141/properties/{key}",
  "outgoing_typed_relationships" : "http://localhost:7474/db/data/node/10141/relationships/out/{-list|&|types}",
  "properties" : "http://localhost:7474/db/data/node/10141/properties",
  "incoming_relationships" : "http://localhost:7474/db/data/node/10141/relationships/in",
  "extensions" : { },
  "create_relationship" : "http://localhost:7474/db/data/node/10141/relationships",
  "paged_traverse" : "http://localhost:7474/db/data/node/10141/paged/traverse/{returnType}{?pageSize,leaseTime}",
  "all_relationships" : "http://localhost:7474/db/data/node/10141/relationships/all",
  "incoming_typed_relationships" : "http://localhost:7474/db/data/node/10141/relationships/in/{-list|&|types}",
  "metadata" : {
    "id" : 10141,
    "labels" : [ "Actor" ]
  }
} ]

Get nodes by label and property

You can retrieve all nodes with a given label and property by passing one property as a query parameter. Notice that the property value is JSON-encoded and then URL-encoded.

If there is an index available on the label/property combination you send, that index will be used. If no index is available, all nodes with the given label will be filtered through to find matching nodes.

Currently, it is not possible to search using multiple properties.

Figure 21.57. Final Graph

Example request

  • GET http://localhost:7474/db/data/label/Person/nodes?name=%22Clint+Eastwood%22
  • Accept: application/json; charset=UTF-8

Example response

  • 200: OK
  • Content-Type: application/json; charset=UTF-8
[ {
  "labels" : "http://localhost:7474/db/data/node/10143/labels",
  "outgoing_relationships" : "http://localhost:7474/db/data/node/10143/relationships/out",
  "data" : {
    "name" : "Clint Eastwood"
  },
  "traverse" : "http://localhost:7474/db/data/node/10143/traverse/{returnType}",
  "all_typed_relationships" : "http://localhost:7474/db/data/node/10143/relationships/all/{-list|&|types}",
  "self" : "http://localhost:7474/db/data/node/10143",
  "property" : "http://localhost:7474/db/data/node/10143/properties/{key}",
  "outgoing_typed_relationships" : "http://localhost:7474/db/data/node/10143/relationships/out/{-list|&|types}",
  "properties" : "http://localhost:7474/db/data/node/10143/properties",
  "incoming_relationships" : "http://localhost:7474/db/data/node/10143/relationships/in",
  "extensions" : { },
  "create_relationship" : "http://localhost:7474/db/data/node/10143/relationships",
  "paged_traverse" : "http://localhost:7474/db/data/node/10143/paged/traverse/{returnType}{?pageSize,leaseTime}",
  "all_relationships" : "http://localhost:7474/db/data/node/10143/relationships/all",
  "incoming_typed_relationships" : "http://localhost:7474/db/data/node/10143/relationships/in/{-list|&|types}",
  "metadata" : {
    "id" : 10143,
    "labels" : [ "Person" ]
  }
} ]

List all labels

Figure 21.58. Final Graph

Example request

  • GET http://localhost:7474/db/data/labels
  • Accept: application/json; charset=UTF-8

Example response

  • 200: OK
  • Content-Type: application/json; charset=UTF-8
[ "Director", "First", "Actor", "Second", "AlbumRelease", "Person", "Bar", "Foo", "AlbumGroup", "DecibelEntity" ]