@@ -8,57 +8,83 @@ This can greatly increase the indexing speed.
88----
99'use strict'
1010
11+ require('array.prototype.flatmap').shim()
1112const { Client } = require('@elastic/elasticsearch')
12- const client = new Client({ node: 'http://localhost:9200' })
13+ const client = new Client({
14+ node: 'http://localhost:9200'
15+ })
1316
1417async function run () {
15- const { body: bulkResponse } = await client.bulk ({
16- // here we are forcing an index refresh ,
17- // otherwise we will not get any result
18- // in the consequent search
19- refresh: true,
20- body: [
21- // operation to perform
22- { index : { _index : 'game-of-thrones' } },
23- // the document to index
24- {
25- character: 'Ned Stark',
26- quote: 'Winter is coming.'
27- },
18+ await client.indices.create ({
19+ index: 'tweets' ,
20+ body: {
21+ mappings: {
22+ properties: {
23+ id: { type: 'integer' },
24+ text: { type: 'text' },
25+ user : { type : 'keyword' },
26+ time: { type: 'date' }
27+ }
28+ }
29+ }
30+ }, { ignore: [400] })
2831
29- { index: { _index: 'game-of-thrones' } },
30- {
31- character: 'Daenerys Targaryen',
32- quote: 'I am the blood of the dragon.'
33- },
32+ const dataset = [{
33+ id: 1,
34+ text: 'If I fall, don\'t bring me back.',
35+ user: 'jon',
36+ date: new Date()
37+ }, {
38+ id: 2,
39+ text: 'Witer is coming',
40+ user: 'ned',
41+ date: new Date()
42+ }, {
43+ id: 3,
44+ text: 'A Lannister always pays his debts.',
45+ user: 'tyrion',
46+ date: new Date()
47+ }, {
48+ id: 4,
49+ text: 'I am the blood of the dragon.',
50+ user: 'daenerys',
51+ date: new Date()
52+ }, {
53+ id: 5, // change this value to a string to see the bulk response with errors
54+ text: 'A girl is Arya Stark of Winterfell. And I\'m going home.',
55+ user: 'arya',
56+ date: new Date()
57+ }]
3458
35- { index: { _index: 'game-of-thrones' } },
36- {
37- character: 'Tyrion Lannister',
38- quote: 'A mind needs books like a sword needs a whetstone.'
39- }
40- ]
41- })
59+ const body = dataset.flatMap(doc => [{ index: { _index: 'tweets' } }, doc])
4260
43- if (bulkResponse.errors) {
44- console.log(bulkResponse)
45- process.exit(1)
46- }
61+ const { body: bulkResponse } = await client.bulk({ refresh: true, body })
4762
48- // Let's search!
49- const { body } = await client.search({
50- index: 'game-of-thrones',
51- body: {
52- query: {
53- match: {
54- quote: 'winter'
55- }
63+ if (bulkResponse.errors) {
64+ const erroredDocuments = []
65+ // The items array has the same order of the dataset we just indexed.
66+ // The presence of the `error` key indicates that the operation
67+ // that we did for the document has failed.
68+ bulkResponse.items.forEach((action, i) => {
69+ const operation = Object.keys(action)[0]
70+ if (action[operation].error) {
71+ erroredDocuments.push({
72+ // If the status is 429 it means that you can retry the document,
73+ // otherwise it's very likely a mapping error, and you should
74+ // fix the document before to try it again.
75+ status: action[operation].status,
76+ error: action[operation].error,
77+ operation: body[i * 2],
78+ document: body[i * 2 + 1]
79+ })
5680 }
57- }
58- })
81+ })
82+ console.log(erroredDocuments)
83+ }
5984
60- console.log(body.hits.hits)
85+ const { body: count } = await client.count({ index: 'tweets' })
86+ console.log(count)
6187}
6288
6389run().catch(console.log)
64- ----
90+ ----
0 commit comments