26.11. Node labels

26.11.1. Adding a label to a node
26.11.2. Adding multiple labels to a node
26.11.3. Adding a label with an invalid name
26.11.4. Replacing labels on a node
26.11.5. Removing a label from a node
26.11.6. Listing labels for a node
26.11.7. Get all nodes with a label
26.11.8. Get nodes by label and property
26.11.9. List all labels

26.11.1. Adding a label to a node

Figure 26.47. Final Graph


Example request

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

Example response

  • 204: No Content

26.11.2. Adding multiple labels to a node

Figure 26.48. Final Graph


Example request

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

Example response

  • 204: No Content

26.11.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 26.49. Final Graph


Example request

  • POST http://localhost:7474/db/data/node/83/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:319)",
    "org.neo4j.server.rest.web.RestfulGraphDatabase.addNodeLabel(RestfulGraphDatabase.java:442)",
    "java.lang.reflect.Method.invoke(Method.java:606)",
    "org.neo4j.server.rest.transactional.TransactionalRequestDispatcher.dispatch(TransactionalRequestDispatcher.java:132)",
    "org.neo4j.server.rest.security.SecurityFilter.doFilter(SecurityFilter.java:112)"
  ],
  "cause": {
    "message": "Unable to add label.",
    "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:71)",
        "org.neo4j.kernel.impl.api.DataIntegrityValidatingStatementOperations.labelGetOrCreateForName(DataIntegrityValidatingStatementOperations.java:80)",
        "org.neo4j.kernel.impl.core.NodeProxy.addLabel(NodeProxy.java:514)",
        "org.neo4j.server.rest.web.DatabaseActions.addLabelToNode(DatabaseActions.java:314)",
        "org.neo4j.server.rest.web.RestfulGraphDatabase.addNodeLabel(RestfulGraphDatabase.java:442)",
        "java.lang.reflect.Method.invoke(Method.java:606)",
        "org.neo4j.server.rest.transactional.TransactionalRequestDispatcher.dispatch(TransactionalRequestDispatcher.java:132)",
        "org.neo4j.server.rest.security.SecurityFilter.doFilter(SecurityFilter.java:112)"
      ],
      "fullname": "org.neo4j.kernel.api.exceptions.schema.IllegalTokenNameException"
    },
    "exception": "ConstraintViolationException",
    "stacktrace": [
      "org.neo4j.kernel.impl.core.NodeProxy.addLabel(NodeProxy.java:519)",
      "org.neo4j.server.rest.web.DatabaseActions.addLabelToNode(DatabaseActions.java:314)",
      "org.neo4j.server.rest.web.RestfulGraphDatabase.addNodeLabel(RestfulGraphDatabase.java:442)",
      "java.lang.reflect.Method.invoke(Method.java:606)",
      "org.neo4j.server.rest.transactional.TransactionalRequestDispatcher.dispatch(TransactionalRequestDispatcher.java:132)",
      "org.neo4j.server.rest.security.SecurityFilter.doFilter(SecurityFilter.java:112)"
    ],
    "fullname": "org.neo4j.graphdb.ConstraintViolationException"
  }
}

26.11.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 26.50. Final Graph


Example request

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

Example response

  • 204: No Content

26.11.5. Removing a label from a node

Figure 26.51. Final Graph


Example request

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

Example response

  • 204: No Content

26.11.6. Listing labels for a node

Figure 26.52. Final Graph


Example request

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

Example response

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

26.11.7. Get all nodes with a label

Figure 26.53. Final Graph


Example request

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

Example response

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

26.11.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 26.54. Final Graph


Example request

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

Example response

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

26.11.9. List all labels

Figure 26.55. 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
[ "You", "second", "MyOtherLabel", "MyLabel", "MyThirdLabel", "Me", "first", "Person" ]