* delete asciidoc files * add migrated files * Apply suggestions from review Co-authored-by: Josh Mock <josh@joshmock.com> * Apply suggestions from review Co-authored-by: Josh Mock <josh@joshmock.com> * add the new ci checks (#2634) --------- Co-authored-by: Marci W <333176+marciw@users.noreply.github.com> Co-authored-by: Josh Mock <josh@joshmock.com>
100 lines
2.6 KiB
Markdown
100 lines
2.6 KiB
Markdown
---
|
|
mapped_pages:
|
|
- https://www.elastic.co/guide/en/elasticsearch/client/javascript-api/current/bulk_examples.html
|
|
---
|
|
|
|
# Bulk [bulk_examples]
|
|
|
|
With the [`bulk` API](/reference/api-reference.md#_bulk), you can perform multiple index/delete operations in a single API call. The `bulk` API significantly increases indexing speed.
|
|
|
|
::::{note}
|
|
You can also use the [bulk helper](/reference/client-helpers.md#bulk-helper).
|
|
::::
|
|
|
|
|
|
```js
|
|
'use strict'
|
|
|
|
require('array.prototype.flatmap').shim()
|
|
const { Client } = require('@elastic/elasticsearch')
|
|
const client = new Client({
|
|
cloud: { id: '<cloud-id>' },
|
|
auth: { apiKey: 'base64EncodedKey' }
|
|
})
|
|
|
|
async function run () {
|
|
await client.indices.create({
|
|
index: 'tweets',
|
|
operations: {
|
|
mappings: {
|
|
properties: {
|
|
id: { type: 'integer' },
|
|
text: { type: 'text' },
|
|
user: { type: 'keyword' },
|
|
time: { type: 'date' }
|
|
}
|
|
}
|
|
}
|
|
}, { ignore: [400] })
|
|
|
|
const dataset = [{
|
|
id: 1,
|
|
text: 'If I fall, don\'t bring me back.',
|
|
user: 'jon',
|
|
time: new Date()
|
|
}, {
|
|
id: 2,
|
|
text: 'Winter is coming',
|
|
user: 'ned',
|
|
time: new Date()
|
|
}, {
|
|
id: 3,
|
|
text: 'A Lannister always pays his debts.',
|
|
user: 'tyrion',
|
|
time: new Date()
|
|
}, {
|
|
id: 4,
|
|
text: 'I am the blood of the dragon.',
|
|
user: 'daenerys',
|
|
time: new Date()
|
|
}, {
|
|
id: 5, // change this value to a string to see the bulk response with errors
|
|
text: 'A girl is Arya Stark of Winterfell. And I\'m going home.',
|
|
user: 'arya',
|
|
time: new Date()
|
|
}]
|
|
|
|
const operations = dataset.flatMap(doc => [{ index: { _index: 'tweets' } }, doc])
|
|
|
|
const bulkResponse = await client.bulk({ refresh: true, operations })
|
|
|
|
if (bulkResponse.errors) {
|
|
const erroredDocuments = []
|
|
// The items array has the same order of the dataset we just indexed.
|
|
// The presence of the `error` key indicates that the operation
|
|
// that we did for the document has failed.
|
|
bulkResponse.items.forEach((action, i) => {
|
|
const operation = Object.keys(action)[0]
|
|
if (action[operation].error) {
|
|
erroredDocuments.push({
|
|
// If the status is 429 it means that you can retry the document,
|
|
// otherwise it's very likely a mapping error, and you should
|
|
// fix the document before to try it again.
|
|
status: action[operation].status,
|
|
error: action[operation].error,
|
|
operation: operations[i * 2],
|
|
document: operations[i * 2 + 1]
|
|
})
|
|
}
|
|
})
|
|
console.log(erroredDocuments)
|
|
}
|
|
|
|
const count = await client.count({ index: 'tweets' })
|
|
console.log(count)
|
|
}
|
|
|
|
run().catch(console.log)
|
|
```
|
|
|