Updated error handling in bulk example (#927)

This commit is contained in:
Tomas Della Vedova
2019-08-01 12:20:29 +02:00
committed by GitHub
parent 2e77a7d042
commit 9142b27d9e

View File

@ -8,56 +8,82 @@ This can greatly increase the indexing speed.
---- ----
'use strict' 'use strict'
require('array.prototype.flatmap').shim()
const { Client } = require('@elastic/elasticsearch') const { Client } = require('@elastic/elasticsearch')
const client = new Client({ node: 'http://localhost:9200' }) const client = new Client({
node: 'http://localhost:9200'
})
async function run () { async function run () {
const { body: bulkResponse } = await client.bulk({ await client.indices.create({
// here we are forcing an index refresh, index: 'tweets',
// otherwise we will not get any result body: {
// in the consequent search mappings: {
refresh: true, properties: {
body: [ id: { type: 'integer' },
// operation to perform text: { type: 'text' },
{ index: { _index: 'game-of-thrones' } }, user: { type: 'keyword' },
// the document to index time: { type: 'date' }
{
character: 'Ned Stark',
quote: 'Winter is coming.'
},
{ index: { _index: 'game-of-thrones' } },
{
character: 'Daenerys Targaryen',
quote: 'I am the blood of the dragon.'
},
{ index: { _index: 'game-of-thrones' } },
{
character: 'Tyrion Lannister',
quote: 'A mind needs books like a sword needs a whetstone.'
} }
] }
}) }
}, { ignore: [400] })
const dataset = [{
id: 1,
text: 'If I fall, don\'t bring me back.',
user: 'jon',
date: new Date()
}, {
id: 2,
text: 'Witer is coming',
user: 'ned',
date: new Date()
}, {
id: 3,
text: 'A Lannister always pays his debts.',
user: 'tyrion',
date: new Date()
}, {
id: 4,
text: 'I am the blood of the dragon.',
user: 'daenerys',
date: 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',
date: new Date()
}]
const body = dataset.flatMap(doc => [{ index: { _index: 'tweets' } }, doc])
const { body: bulkResponse } = await client.bulk({ refresh: true, body })
if (bulkResponse.errors) { if (bulkResponse.errors) {
console.log(bulkResponse) const erroredDocuments = []
process.exit(1) // 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.
// Let's search! bulkResponse.items.forEach((action, i) => {
const { body } = await client.search({ const operation = Object.keys(action)[0]
index: 'game-of-thrones', if (action[operation].error) {
body: { erroredDocuments.push({
query: { // If the status is 429 it means that you can retry the document,
match: { // otherwise it's very likely a mapping error, and you should
quote: 'winter' // fix the document before to try it again.
} status: action[operation].status,
} error: action[operation].error,
operation: body[i * 2],
document: body[i * 2 + 1]
})
} }
}) })
console.log(erroredDocuments)
}
console.log(body.hits.hits) const { body: count } = await client.count({ index: 'tweets' })
console.log(count)
} }
run().catch(console.log) run().catch(console.log)