fediblock-instance/lib/taskdeletedup.js

53 lines
2.2 KiB
JavaScript
Raw Normal View History

2024-10-14 18:13:12 +00:00
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
}
})
2024-10-14 18:16:45 +00:00
for (const instance of result.hits.hits) {
if (last && instance._source && last.instance === instance._source.instance) {
await client.delete({ index: index, id: instance._id })
2024-10-14 18:13:12 +00:00
count.deleted++
2024-10-14 18:16:45 +00:00
console.log('deleted ' + instance._id + ': ' + instance._source.instance)
2024-10-14 18:13:12 +00:00
}
else {
2024-10-14 18:16:45 +00:00
last = instance._source
2024-10-14 18:13:12 +00:00
}
2024-10-14 18:16:45 +00:00
if (instance._id === result.hits.hits[result.hits.hits.length - 1]._id) {
lastsort = instance.sort
2024-10-14 18:13:12 +00:00
}
}
count.total += result.hits.hits.length
if (result.hits.hits.length !== size) {
break
}
} while (result.hits && result.hits.hits && result.hits.hits.length > 0)
2024-10-14 18:16:45 +00:00
return console.log('Index: ' + constant.index + ' - Total: ' + count.total + ' - Deleted: ' + count.deleted)
2024-10-14 18:13:12 +00:00
},
job = schedule.scheduleJob('0 ' + constant.taskdeletedup + ' * * *', async () => {
await deleteDup()
})
}