diff --git a/packages/api/src/elastic/labels.ts b/packages/api/src/elastic/labels.ts index 4741fd9b0..e69f8312e 100644 --- a/packages/api/src/elastic/labels.ts +++ b/packages/api/src/elastic/labels.ts @@ -198,3 +198,60 @@ export const updateLabelInPage = async ( return false } } + +export const setLabelsForHighlight = async ( + highlightId: string, + labels: Label[], + ctx: PageContext +): Promise => { + try { + const { body } = await client.updateByQuery({ + index: INDEX_ALIAS, + body: { + script: { + source: `ctx._source.highlights[0].labels = params.labels`, + lang: 'painless', + params: { + labels: labels, + }, + }, + query: { + nested: { + path: 'highlights', + query: { + term: { + 'highlights.id': highlightId, + }, + }, + }, + }, + }, + refresh: ctx.refresh, + conflicts: 'proceed', // ignore conflicts + }) + + if (!body.updated) { + return false + } + + for (const label of labels) { + await ctx.pubsub.entityCreated