1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108
| const elasticsearch = require("elasticsearch");
const cities = require('./cities.json') const axios = require('axios')
const client = new elasticsearch.Client({ host: "localhost:9200", log: "trace", apiVersion: "6.8", }); const sleep = time => new Promise(reslove => setTimeout(reslove, time))
const init = async () => { try { await client.ping({ requestTimeout: 30000 }); } catch (e) { console.log('e', e) } } init()
const createFunc = async () => { try { await client.create({ index: 'es_user', type: '_doc', id: '1', body: {} }); } catch (error) { console.log('create error', error) } }
const deleteFunc = async () => { try { await client.delete({ index: 'es_user', type: '_doc', id: '1', }); } catch (error) { console.log('delete error', error) } }
const push = (cities, start, end) => { var bulk = []; cities.slice(start, end).forEach(city => { bulk.push({ index: { _index: 'es_user', _type: '_doc', } }) bulk.push(city) })
client.bulk({ body: bulk }, function (err, response) { if (err) { console.log("Failed Bulk operation".red, err) } else { console.log("Successfully imported %s".green, cities.length); } });
}
const go = async () => { let citiesLen = Math.ceil(cities.length / 1000) for (let i = 0; i <= citiesLen; i++) { await axios({ method: 'put', url: 'http://localhost:9200/_all/_settings', headers: { 'Content-Type': 'application/json' }, data: { "index.blocks.read_only_allow_delete": false } }) await sleep(300) push(cities, i * 1000, (i+1) * 1000) } }
|