18.14. Batch operations

18.14.1. Execute multiple operations in batch
18.14.2. Refer to items created earlier in the same batch job
[Caution]Caution

Batch support is currently experimental. Expect this part of the API to change.

18.14.1. Execute multiple operations in batch

This lets you execute multiple API calls through a single HTTP call, significantly improving performance for large insert and update operations.

The batch service expects an array of job descriptions as input, each job description describing an action to be performed via the normal server API.

This service is transactional. If any of the operations performed fails (returns a non-2xx HTTP status code), the transaction will be rolled back and all changes will be undone.

Each job description should contain a path attribute, with a value relative to the data API root (so http://localhost:7474/db/data/node becomes just /node), and a method attribute containing HTTP verb to use.

Optionally you may provide a body attribute, and an id attribute to help you keep track of responses, although responses are guaranteed to be returned in the same order the job descriptions are received.

The following figure outlines the different parts of the job descriptions:

Figure 18.68. Final Graph


Example request

  • POST http://localhost:7474/db/data/batch
  • Accept: application/json
  • Content-Type: application/json
[ {
  "method" : "PUT",
  "to" : "/node/76/properties",
  "body" : {
    "age" : 1
  },
  "id" : 0
}, {
  "method" : "GET",
  "to" : "/node/76",
  "id" : 1
}, {
  "method" : "POST",
  "to" : "/node",
  "body" : {
    "age" : 1
  },
  "id" : 2
}, {
  "method" : "POST",
  "to" : "/node",
  "body" : {
    "age" : 1
  },
  "id" : 3
} ]

Example response

  • 200: OK
  • Content-Type: application/json
[ {
  "id" : 0,
  "from" : "/node/76/properties"
}, {
  "id" : 1,
  "body" : {
    "outgoing_relationships" : "http://localhost:7474/db/data/node/76/relationships/out",
    "data" : {
      "age" : 1
    },
    "traverse" : "http://localhost:7474/db/data/node/76/traverse/{returnType}",
    "all_typed_relationships" : "http://localhost:7474/db/data/node/76/relationships/all/{-list|&|types}",
    "property" : "http://localhost:7474/db/data/node/76/properties/{key}",
    "self" : "http://localhost:7474/db/data/node/76",
    "outgoing_typed_relationships" : "http://localhost:7474/db/data/node/76/relationships/out/{-list|&|types}",
    "properties" : "http://localhost:7474/db/data/node/76/properties",
    "incoming_relationships" : "http://localhost:7474/db/data/node/76/relationships/in",
    "extensions" : {
    },
    "create_relationship" : "http://localhost:7474/db/data/node/76/relationships",
    "paged_traverse" : "http://localhost:7474/db/data/node/76/paged/traverse/{returnType}{?pageSize,leaseTime}",
    "all_relationships" : "http://localhost:7474/db/data/node/76/relationships/all",
    "incoming_typed_relationships" : "http://localhost:7474/db/data/node/76/relationships/in/{-list|&|types}"
  },
  "from" : "/node/76"
}, {
  "id" : 2,
  "location" : "http://localhost:7474/db/data/node/77",
  "body" : {
    "outgoing_relationships" : "http://localhost:7474/db/data/node/77/relationships/out",
    "data" : {
      "age" : 1
    },
    "traverse" : "http://localhost:7474/db/data/node/77/traverse/{returnType}",
    "all_typed_relationships" : "http://localhost:7474/db/data/node/77/relationships/all/{-list|&|types}",
    "property" : "http://localhost:7474/db/data/node/77/properties/{key}",
    "self" : "http://localhost:7474/db/data/node/77",
    "outgoing_typed_relationships" : "http://localhost:7474/db/data/node/77/relationships/out/{-list|&|types}",
    "properties" : "http://localhost:7474/db/data/node/77/properties",
    "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}"
  },
  "from" : "/node"
}, {
  "id" : 3,
  "location" : "http://localhost:7474/db/data/node/78",
  "body" : {
    "outgoing_relationships" : "http://localhost:7474/db/data/node/78/relationships/out",
    "data" : {
      "age" : 1
    },
    "traverse" : "http://localhost:7474/db/data/node/78/traverse/{returnType}",
    "all_typed_relationships" : "http://localhost:7474/db/data/node/78/relationships/all/{-list|&|types}",
    "property" : "http://localhost:7474/db/data/node/78/properties/{key}",
    "self" : "http://localhost:7474/db/data/node/78",
    "outgoing_typed_relationships" : "http://localhost:7474/db/data/node/78/relationships/out/{-list|&|types}",
    "properties" : "http://localhost:7474/db/data/node/78/properties",
    "incoming_relationships" : "http://localhost:7474/db/data/node/78/relationships/in",
    "extensions" : {
    },
    "create_relationship" : "http://localhost:7474/db/data/node/78/relationships",
    "paged_traverse" : "http://localhost:7474/db/data/node/78/paged/traverse/{returnType}{?pageSize,leaseTime}",
    "all_relationships" : "http://localhost:7474/db/data/node/78/relationships/all",
    "incoming_typed_relationships" : "http://localhost:7474/db/data/node/78/relationships/in/{-list|&|types}"
  },
  "from" : "/node"
} ]

18.14.2. Refer to items created earlier in the same batch job

The batch operation API allows you to refer to the URI returned from a created resource in subsequent job descriptions, within the same batch call.

Use the {[JOB ID]} special syntax to inject URIs from created resources into JSON strings in subsequent job descriptions.

Figure 18.69. Final Graph


Example request

  • POST http://localhost:7474/db/data/batch
  • Accept: application/json
  • Content-Type: application/json
[ {
  "method" : "POST",
  "to" : "/node",
  "id" : 0,
  "body" : {
    "name" : "bob"
  }
}, {
  "method" : "POST",
  "to" : "/node",
  "id" : 1,
  "body" : {
    "age" : 12
  }
}, {
  "method" : "POST",
  "to" : "{0}/relationships",
  "id" : 3,
  "body" : {
    "to" : "{1}",
    "data" : {
      "since" : "2010"
    },
    "type" : "KNOWS"
  }
}, {
  "method" : "POST",
  "to" : "/index/relationship/my_rels",
  "id" : 4,
  "body" : {
    "key" : "since",
    "value" : "2010",
    "uri" : "{3}"
  }
} ]

Example response

  • 200: OK
  • Content-Type: application/json
[ {
  "id" : 0,
  "location" : "http://localhost:7474/db/data/node/79",
  "body" : {
    "outgoing_relationships" : "http://localhost:7474/db/data/node/79/relationships/out",
    "data" : {
      "name" : "bob"
    },
    "traverse" : "http://localhost:7474/db/data/node/79/traverse/{returnType}",
    "all_typed_relationships" : "http://localhost:7474/db/data/node/79/relationships/all/{-list|&|types}",
    "property" : "http://localhost:7474/db/data/node/79/properties/{key}",
    "self" : "http://localhost:7474/db/data/node/79",
    "outgoing_typed_relationships" : "http://localhost:7474/db/data/node/79/relationships/out/{-list|&|types}",
    "properties" : "http://localhost:7474/db/data/node/79/properties",
    "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}"
  },
  "from" : "/node"
}, {
  "id" : 1,
  "location" : "http://localhost:7474/db/data/node/80",
  "body" : {
    "outgoing_relationships" : "http://localhost:7474/db/data/node/80/relationships/out",
    "data" : {
      "age" : 12
    },
    "traverse" : "http://localhost:7474/db/data/node/80/traverse/{returnType}",
    "all_typed_relationships" : "http://localhost:7474/db/data/node/80/relationships/all/{-list|&|types}",
    "property" : "http://localhost:7474/db/data/node/80/properties/{key}",
    "self" : "http://localhost:7474/db/data/node/80",
    "outgoing_typed_relationships" : "http://localhost:7474/db/data/node/80/relationships/out/{-list|&|types}",
    "properties" : "http://localhost:7474/db/data/node/80/properties",
    "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}"
  },
  "from" : "/node"
}, {
  "id" : 3,
  "location" : "http://localhost:7474/db/data/relationship/26",
  "body" : {
    "start" : "http://localhost:7474/db/data/node/79",
    "data" : {
      "since" : "2010"
    },
    "self" : "http://localhost:7474/db/data/relationship/26",
    "property" : "http://localhost:7474/db/data/relationship/26/properties/{key}",
    "properties" : "http://localhost:7474/db/data/relationship/26/properties",
    "type" : "KNOWS",
    "extensions" : {
    },
    "end" : "http://localhost:7474/db/data/node/80"
  },
  "from" : "http://localhost:7474/db/data/node/79/relationships"
}, {
  "id" : 4,
  "location" : "http://localhost:7474/db/data/index/relationship/my_rels/since/2010/26",
  "body" : {
    "indexed" : "http://localhost:7474/db/data/index/relationship/my_rels/since/2010/26",
    "start" : "http://localhost:7474/db/data/node/79",
    "data" : {
      "since" : "2010"
    },
    "self" : "http://localhost:7474/db/data/relationship/26",
    "property" : "http://localhost:7474/db/data/relationship/26/properties/{key}",
    "properties" : "http://localhost:7474/db/data/relationship/26/properties",
    "type" : "KNOWS",
    "extensions" : {
    },
    "end" : "http://localhost:7474/db/data/node/80"
  },
  "from" : "/index/relationship/my_rels"
} ]