module.exports = client => { const constant = require('./constant'), schedule = require('node-schedule'), size = 500, deleteDup = async () => { const count = { deleted: 0, total: 0 } let lastsort = undefined, result = undefined, last = undefined do { result = await client.search({ index: constant.index, size: size, body: { query: { match_all: {} }, sort: [{ "instance": { "order": "asc" }, "last": { "order": "desc", "numeric_type": "date_nanos", "format": "strict_date_optional_time_nanos" } }], search_after: lastsort } }) for (const instance of result.hits.hits) { if (last && instance._source && last.instance === instance._source.instance) { await client.delete({ index: index, id: instance._id }) count.deleted++ console.log('deleted ' + instance._id + ': ' + instance._source.instance) } else { last = instance._source } if (instance._id === result.hits.hits[result.hits.hits.length - 1]._id) { lastsort = instance.sort } } count.total += result.hits.hits.length if (result.hits.hits.length !== size) { break } } while (result.hits && result.hits.hits && result.hits.hits.length > 0) return console.log('Index: ' + constant.index + ' - Total: ' + count.total + ' - Deleted: ' + count.deleted) }, job = schedule.scheduleJob('0 ' + constant.taskdeletedup + ' * * *', async () => { await deleteDup() }) }