Import #
Create, update or delete large amount of documents as fast as possible.
This route is faster than the document:m* routes family (e.g. document:mCreate), but no real-time notifications will be generated, even if some of the documents in the import match subscription filters.
import (index, collection, bulkData, [options])| Arguments | Type | Description |
|---|---|---|
index | string | Index name |
collection | string | Collection name |
bulkData | object[] | List of documents to be added to the collection |
options | object | Query options |
bulkData #
This API takes a JSON array containing a list of objects working in pairs. In each pair, the first object specifies the action to perform (the most common is create) and the second specifies the document itself, like in the example below:
[
// Action object
{ create: { _id: 'id' } },
// Document object
{ myField: 'myValue', myOtherField: 'myOtherValue' },
// Another action object
{ create: { _id: 'another-id' } },
// Another document object
{ myField: 'anotherValue', myOtherField: 'yetAnotherValue' }
];You cannot specify either the _index or the _type options with a bulk import.
You have to specify the index/collection in the request.
Possible actions are create, index, update, delete.
Learn more at Elasticsearch Bulk API
options #
Additional query options
| Property | Type (default) | Description |
|---|---|---|
queuable | boolean ( true) | If true, queues the request during downtime, until connected to Kuzzle again |
timeout | number ( -1) | Time (in ms) during which a request will still be waited to be resolved. Set it -1 if you want to wait indefinitely |
triggerEvents | boolean ( false) | If set to true, will trigger events even if using Embeded SDK. You should always ensure that your events/pipes does not create an infinite loop. Available since Kuzzle 2.31.0 |
Resolves #
Resolves to an object containing 2 properties:
| Property | Type | Description |
|---|---|---|
successes | object[] | Array of object containing successful document import |
errors | object[] | Array of object containing failed document import |
Each item of the successes array is an object containing the action name as key and the corresponding object contain the following properties:
| Property | Type | Description |
|---|---|---|
_id | String | Document unique identifier |
status | String | HTTP status code for that query |
Each item of the successes array is an object containing the action name as key and the corresponding object contain the following properties:
| Property | Type | Description |
|---|---|---|
_id | String | Document unique identifier |
status | String | HTTP status code for that query |
error | Object | Error object |
Each error object contain the following properties:
| Property | Type | Description |
|---|---|---|
type | String | Elasticsearch client error type |
reason | String | human readable error message |
Usage #
const bulkData = [
{ index: { } },
{ a: 'document', with: 'any', number: 'of fields' },
{ create: { _id: 'uniq-id-2' } },
{ another: 'document' },
{ create: { _id: 'uniq-id-3' } },
{ and: { another: 'one' } }
];
try {
const response = await kuzzle.bulk.import('nyc-open-data', 'yellow-taxi', bulkData);
console.log(response);
/*
{ errors: [],
successes:
[ {
index: {
_id: "hQ10_GwBB2Y5786Pu_NO",
status: 201
}
},
{
create: {
_id: "uniq-id-2",
status: 201
}
},
{
create: {
_id: "uniq-id-3",
status: 201
}
} ] }
*/
console.log(`Successfully imported ${response.successes.length} documents`);
} catch (error) {
console.error(error.message);
}