20.12. Node labels

20.12.1. Adding a label to a node
20.12.2. Adding multiple labels to a node
20.12.3. Adding a label with an invalid name
20.12.4. Replacing labels on a node
20.12.5. Removing a label from a node
20.12.6. Listing labels for a node
20.12.7. Get all nodes with a label
20.12.8. Get nodes by label and property
20.12.9. List all labels

20.12.1. Adding a label to a node

Figure 20.49. Final Graph


Example request

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

Example response

  • 204: No Content

20.12.2. Adding multiple labels to a node

Figure 20.50. Final Graph


Example request

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

Example response

  • 204: No Content

20.12.3. 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 20.51. Final Graph


Example request

  • POST http://localhost:7474/db/data/node/118/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:328)",
    "org.neo4j.server.rest.web.RestfulGraphDatabase.addNodeLabel(RestfulGraphDatabase.java:447)",
    "java.lang.reflect.Method.invoke(Method.java:606)",
    "org.neo4j.server.rest.transactional.TransactionalRequestDispatcher.dispatch(TransactionalRequestDispatcher.java:139)",
    "org.neo4j.server.rest.security.SecurityFilter.doFilter(SecurityFilter.java:112)",
    "java.lang.Thread.run(Thread.java:724)"
  ],
  "cause": {
    "message": "Invalid label name \u0027\u0027.",
    "cause": {
      "message": "\u0027\u0027 is not a valid token name. Only non-null, non-empty strings are allowed.",
      "exception": "IllegalTokenNameException",
      "stacktrace": [
        "org.neo4j.kernel.impl.api.DataIntegrityValidatingStatementOperations.checkValidTokenName(DataIntegrityValidatingStatementOperations.java:171)",
        "org.neo4j.kernel.impl.api.DataIntegrityValidatingStatementOperations.labelGetOrCreateForName(DataIntegrityValidatingStatementOperations.java:82)",
        "org.neo4j.kernel.impl.api.OperationsFacade.labelGetOrCreateForName(OperationsFacade.java:464)",
        "org.neo4j.kernel.impl.core.NodeProxy.addLabel(NodeProxy.java:517)",
        "org.neo4j.server.rest.web.DatabaseActions.addLabelToNode(DatabaseActions.java:323)",
        "org.neo4j.server.rest.web.RestfulGraphDatabase.addNodeLabel(RestfulGraphDatabase.java:447)",
        "java.lang.reflect.Method.invoke(Method.java:606)",
        "org.neo4j.server.rest.transactional.TransactionalRequestDispatcher.dispatch(TransactionalRequestDispatcher.java:139)",
        "org.neo4j.server.rest.security.SecurityFilter.doFilter(SecurityFilter.java:112)",
        "java.lang.Thread.run(Thread.java:724)"
      ],
      "fullname": "org.neo4j.kernel.api.exceptions.schema.IllegalTokenNameException"
    },
    "exception": "ConstraintViolationException",
    "stacktrace": [
      "org.neo4j.kernel.impl.core.NodeProxy.addLabel(NodeProxy.java:526)",
      "org.neo4j.server.rest.web.DatabaseActions.addLabelToNode(DatabaseActions.java:323)",
      "org.neo4j.server.rest.web.RestfulGraphDatabase.addNodeLabel(RestfulGraphDatabase.java:447)",
      "java.lang.reflect.Method.invoke(Method.java:606)",
      "org.neo4j.server.rest.transactional.TransactionalRequestDispatcher.dispatch(TransactionalRequestDispatcher.java:139)",
      "org.neo4j.server.rest.security.SecurityFilter.doFilter(SecurityFilter.java:112)",
      "java.lang.Thread.run(Thread.java:724)"
    ],
    "fullname": "org.neo4j.graphdb.ConstraintViolationException"
  }
}

20.12.4. 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 20.52. Final Graph


Example request

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

Example response

  • 204: No Content

20.12.5. Removing a label from a node

Figure 20.53. Final Graph


Example request

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

Example response

  • 204: No Content

20.12.6. Listing labels for a node

Figure 20.54. Final Graph


Example request

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

Example response

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

20.12.7. Get all nodes with a label

Figure 20.55. 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/113/labels",
  "outgoing_relationships" : "http://localhost:7474/db/data/node/113/relationships/out",
  "data" : {
    "name" : "Clint Eastwood"
  },
  "traverse" : "http://localhost:7474/db/data/node/113/traverse/{returnType}",
  "all_typed_relationships" : "http://localhost:7474/db/data/node/113/relationships/all/{-list|&|types}",
  "self" : "http://localhost:7474/db/data/node/113",
  "property" : "http://localhost:7474/db/data/node/113/properties/{key}",
  "outgoing_typed_relationships" : "http://localhost:7474/db/data/node/113/relationships/out/{-list|&|types}",
  "properties" : "http://localhost:7474/db/data/node/113/properties",
  "incoming_relationships" : "http://localhost:7474/db/data/node/113/relationships/in",
  "extensions" : {
  },
  "create_relationship" : "http://localhost:7474/db/data/node/113/relationships",
  "paged_traverse" : "http://localhost:7474/db/data/node/113/paged/traverse/{returnType}{?pageSize,leaseTime}",
  "all_relationships" : "http://localhost:7474/db/data/node/113/relationships/all",
  "incoming_typed_relationships" : "http://localhost:7474/db/data/node/113/relationships/in/{-list|&|types}"
}, {
  "labels" : "http://localhost:7474/db/data/node/114/labels",
  "outgoing_relationships" : "http://localhost:7474/db/data/node/114/relationships/out",
  "data" : {
    "name" : "Donald Sutherland"
  },
  "traverse" : "http://localhost:7474/db/data/node/114/traverse/{returnType}",
  "all_typed_relationships" : "http://localhost:7474/db/data/node/114/relationships/all/{-list|&|types}",
  "self" : "http://localhost:7474/db/data/node/114",
  "property" : "http://localhost:7474/db/data/node/114/properties/{key}",
  "outgoing_typed_relationships" : "http://localhost:7474/db/data/node/114/relationships/out/{-list|&|types}",
  "properties" : "http://localhost:7474/db/data/node/114/properties",
  "incoming_relationships" : "http://localhost:7474/db/data/node/114/relationships/in",
  "extensions" : {
  },
  "create_relationship" : "http://localhost:7474/db/data/node/114/relationships",
  "paged_traverse" : "http://localhost:7474/db/data/node/114/paged/traverse/{returnType}{?pageSize,leaseTime}",
  "all_relationships" : "http://localhost:7474/db/data/node/114/relationships/all",
  "incoming_typed_relationships" : "http://localhost:7474/db/data/node/114/relationships/in/{-list|&|types}"
} ]

20.12.8. 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 20.56. 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/116/labels",
  "outgoing_relationships" : "http://localhost:7474/db/data/node/116/relationships/out",
  "data" : {
    "name" : "Clint Eastwood"
  },
  "traverse" : "http://localhost:7474/db/data/node/116/traverse/{returnType}",
  "all_typed_relationships" : "http://localhost:7474/db/data/node/116/relationships/all/{-list|&|types}",
  "self" : "http://localhost:7474/db/data/node/116",
  "property" : "http://localhost:7474/db/data/node/116/properties/{key}",
  "outgoing_typed_relationships" : "http://localhost:7474/db/data/node/116/relationships/out/{-list|&|types}",
  "properties" : "http://localhost:7474/db/data/node/116/properties",
  "incoming_relationships" : "http://localhost:7474/db/data/node/116/relationships/in",
  "extensions" : {
  },
  "create_relationship" : "http://localhost:7474/db/data/node/116/relationships",
  "paged_traverse" : "http://localhost:7474/db/data/node/116/paged/traverse/{returnType}{?pageSize,leaseTime}",
  "all_relationships" : "http://localhost:7474/db/data/node/116/relationships/all",
  "incoming_typed_relationships" : "http://localhost:7474/db/data/node/116/relationships/in/{-list|&|types}"
} ]

20.12.9. List all labels

Figure 20.57. 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
[ "novice", "expert", "mylabel", "user", "SomeLabel", "coder", "person", "Person", "Director", "Actor", "chef", "prog" ]