add function to delete pages by param

This commit is contained in:
Hongbo Wu
2022-07-07 11:07:00 +08:00
parent b898f55bb9
commit efd47f3f83

View File

@ -309,7 +309,7 @@ export const getPageByParam = async <K extends keyof ParamSet>(
id: body.hits.hits[0]._id,
} as Page
} catch (e) {
console.error('failed to get pages by param in elastic', e)
console.error('failed to get page by param in elastic', e)
return undefined
}
}
@ -509,3 +509,33 @@ export const countByCreatedAt = async (
return 0
}
}
export const deletePagesByParam = async <K extends keyof ParamSet>(
param: Record<K, Page[K]>
): Promise<boolean> => {
try {
const params = {
query: {
bool: {
filter: Object.keys(param).map((key) => {
return {
term: {
[key]: param[key as K],
},
}
}),
},
},
}
const { body } = await client.deleteByQuery({
index: INDEX_ALIAS,
body: params,
})
return body.deleted > 0
} catch (e) {
console.error('failed to delete pages by param in elastic', e)
return false
}
}