diff --git a/packages/api/src/elastic/labels.ts b/packages/api/src/elastic/labels.ts index ffbbeb3c8..990c15449 100644 --- a/packages/api/src/elastic/labels.ts +++ b/packages/api/src/elastic/labels.ts @@ -94,7 +94,7 @@ export const updateLabelsInPage = async ( } } -export const deleteLabelInPages = async ( +export const deleteLabel = async ( userId: string, label: string, ctx: PageContext @@ -104,8 +104,12 @@ export const deleteLabelInPages = async ( index: INDEX_ALIAS, body: { script: { - source: - 'ctx._source.labels.removeIf(label -> label.name == params.label)', + source: `if (ctx._source.highlights != null) { + ctx._source.highlights[0].labels.removeIf(label -> label.name == params.label) + } + if (ctx._source.labels != null) { + ctx._source.labels.removeIf(label -> label.name == params.label) + }`, lang: 'painless', params: { label: label, @@ -113,12 +117,12 @@ export const deleteLabelInPages = async ( }, query: { bool: { - filter: [ - { - term: { - userId, - }, + must: { + term: { + userId, }, + }, + should: [ { nested: { path: 'labels', @@ -129,11 +133,28 @@ export const deleteLabelInPages = async ( }, }, }, + { + nested: { + path: 'highlights', + query: { + nested: { + path: 'highlights.labels', + query: { + term: { + 'highlights.labels.name': label, + }, + }, + }, + }, + }, + }, ], + minimum_should_match: 1, }, }, }, refresh: ctx.refresh, + conflicts: 'proceed', // ignore conflicts }) body.updated > 0 && @@ -255,61 +276,3 @@ export const setLabelsForHighlight = async ( return false } } - -export const deleteLabelForHighlights = async ( - userId: string, - label: string, - ctx: PageContext -): Promise => { - try { - const { body } = await client.updateByQuery({ - index: INDEX_ALIAS, - body: { - script: { - source: - 'ctx._source.highlights[0].labels.removeIf(label -> label.name == params.label)', - lang: 'painless', - params: { - label: label, - }, - }, - query: { - bool: { - filter: [ - { - term: { - userId, - }, - }, - { - nested: { - path: 'highlights', - query: { - nested: { - path: 'highlights.labels', - query: { - term: { - 'highlights.labels.name': label, - }, - }, - }, - }, - }, - }, - ], - }, - }, - }, - refresh: ctx.refresh, - conflicts: 'proceed', // ignore conflicts - }) - - body.updated > 0 && - (await ctx.pubsub.entityDeleted(EntityType.LABEL, label, ctx.uid)) - - return true - } catch (e) { - console.error('failed to delete a label for highlights in elastic', e) - return false - } -} diff --git a/packages/api/src/resolvers/labels/index.ts b/packages/api/src/resolvers/labels/index.ts index 27e9dd746..f11c35c80 100644 --- a/packages/api/src/resolvers/labels/index.ts +++ b/packages/api/src/resolvers/labels/index.ts @@ -31,15 +31,13 @@ import { createPubSubClient } from '../../datalayer/pubsub' import { AppDataSource } from '../../server' import { getPageById } from '../../elastic/pages' import { - deleteLabelForHighlights, - deleteLabelInPages, + deleteLabel, setLabelsForHighlight, updateLabelInPage, updateLabelsInPage, } from '../../elastic/labels' import { getHighlightById } from '../../elastic/highlights' import { getLabelsByIds } from '../../services/labels' -import { PageContext } from '../../elastic/types' export const labelsResolver = authorized( async (_obj, _params, { claims: { uid }, log }) => { @@ -180,13 +178,11 @@ export const deleteLabelResolver = authorized< } } - const ctx: PageContext = { + // delete label in elastic pages and highlights + await deleteLabel(uid, labelId, { pubsub: createPubSubClient(), uid, - } - // delete label in elastic pages and highlights - await deleteLabelInPages(uid, label.name, ctx) - await deleteLabelForHighlights(uid, label.name, ctx) + }) analytics.track({ userId: uid,