Javascript 7.x

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.


Copied to clipboard!
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:

Copied to clipboard!
[
  // 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

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

Copied to clipboard!
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);
}