23.11. Node labels

23.11.1. Adding a label to a node
23.11.2. Adding multiple labels to a node
23.11.3. Adding a label with an invalid name
23.11.4. Replacing labels on a node
23.11.5. Removing a label from a node
23.11.6. Listing labels for a node
23.11.7. Get all nodes with a label
23.11.8. Get nodes by label and property

23.11.1. Adding a label to a node

Figure 23.47. Final Graph


Example request

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

Example response

  • 204: No Content

23.11.2. Adding multiple labels to a node

Figure 23.48. Final Graph


Example request

  • POST http://localhost:7474/db/data/node/70/labels
  • Accept: application/json
  • Content-Type: application/json
[ "MyLabel", "MyOtherLabel" ]

Example response

  • 204: No Content

23.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 23.49. Final Graph


Example request

  • POST http://localhost:7474/db/data/node/77/labels
  • Accept: application/json
  • Content-Type: application/json
""

Example response

  • 400: Bad Request
  • Content-Type: application/json
{
  "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:343)",
    "org.neo4j.server.rest.web.RestfulGraphDatabase.addNodeLabel(RestfulGraphDatabase.java:427)",
    "java.lang.reflect.Method.invoke(Method.java:597)"
  ],
  "cause": {
    "message": "Unable to add label.",
    "cause": {
      "message": "\u0027\u0027 is not a valid label name. Only non-empty strings are allowed.",
      "exception": "ConstraintViolationKernelException",
      "stacktrace": [
        "org.neo4j.kernel.impl.api.ConstraintEvaluatingStatementContext.getOrCreateLabelId(ConstraintEvaluatingStatementContext.java:44)",
        "org.neo4j.kernel.impl.api.CompositeStatementContext.getOrCreateLabelId(CompositeStatementContext.java:384)",
        "org.neo4j.kernel.impl.api.CompositeStatementContext.getOrCreateLabelId(CompositeStatementContext.java:384)",
        "org.neo4j.kernel.impl.core.NodeProxy.addLabel(NodeProxy.java:325)",
        "org.neo4j.server.rest.web.DatabaseActions.addLabelToNode(DatabaseActions.java:337)",
        "org.neo4j.server.rest.web.RestfulGraphDatabase.addNodeLabel(RestfulGraphDatabase.java:427)",
        "java.lang.reflect.Method.invoke(Method.java:597)"
      ],
      "fullname": "org.neo4j.kernel.api.ConstraintViolationKernelException"
    },
    "exception": "ConstraintViolationException",
    "stacktrace": [
      "org.neo4j.kernel.impl.core.NodeProxy.addLabel(NodeProxy.java:329)",
      "org.neo4j.server.rest.web.DatabaseActions.addLabelToNode(DatabaseActions.java:337)",
      "org.neo4j.server.rest.web.RestfulGraphDatabase.addNodeLabel(RestfulGraphDatabase.java:427)",
      "java.lang.reflect.Method.invoke(Method.java:597)"
    ],
    "fullname": "org.neo4j.graphdb.ConstraintViolationException"
  }
}

23.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 23.50. Final Graph


Example request

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

Example response

  • 204: No Content

23.11.5. Removing a label from a node

Figure 23.51. Final Graph


Example request

  • DELETE http://localhost:7474/db/data/node/67/labels/MyLabel
  • Accept: application/json

Example response

  • 204: No Content

23.11.6. Listing labels for a node

Figure 23.52. Final Graph


Example request

  • GET http://localhost:7474/db/data/node/69/labels
  • Accept: application/json

Example response

  • 200: OK
  • Content-Type: application/json
[ "Me", "You" ]

23.11.7. Get all nodes with a label

Figure 23.53. Final Graph


Example request

  • GET http://localhost:7474/db/data/label/first/nodes
  • Accept: application/json

Example response

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

23.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. Currently, it is not possible to specify multiple properties to search by.

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.

Figure 23.54. Final Graph


Example request

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

Example response

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