From c08afe6451f8d39aacb09e01685c98b008a6a91a Mon Sep 17 00:00:00 2001 From: Hongbo Wu Date: Tue, 28 Jun 2022 12:03:37 +0800 Subject: [PATCH] Update page updatedAt when updating labels --- packages/api/src/elastic/labels.ts | 26 ++++++++++++++++++-------- 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/packages/api/src/elastic/labels.ts b/packages/api/src/elastic/labels.ts index 25f479704..ce02655e2 100644 --- a/packages/api/src/elastic/labels.ts +++ b/packages/api/src/elastic/labels.ts @@ -15,13 +15,16 @@ export const addLabelInPage = async ( body: { script: { source: `if (ctx._source.labels == null) { - ctx._source.labels = [params.label] + ctx._source.labels = [params.label]; + ctx._source.updatedAt = params.updatedAt } else if (!ctx._source.labels.any(label -> label.name == params.label.name)) { - ctx._source.labels.add(params.label) + ctx._source.labels.add(params.label) ; + ctx._source.updatedAt = params.updatedAt } else { ctx.op = 'none' }`, lang: 'painless', params: { label: label, + updatedAt: new Date(), }, }, }, @@ -99,20 +102,22 @@ export const deleteLabel = async ( ctx: PageContext ): Promise => { try { - console.log('deleting label', label) const { body } = await client.updateByQuery({ index: INDEX_ALIAS, body: { script: { source: `if (ctx._source.highlights != null && ctx._source.highlights[0].labels != null) { - ctx._source.highlights[0].labels.removeIf(label -> label.name == params.label) + ctx._source.highlights[0].labels.removeIf(label -> label.name == params.label); + ctx._source.updatedAt = params.updatedAt } if (ctx._source.labels != null) { - ctx._source.labels.removeIf(label -> label.name == params.label) + ctx._source.labels.removeIf(label -> label.name == params.label); + ctx._source.updatedAt = params.updatedAt }`, lang: 'painless', params: { label: label, + updatedAt: new Date(), }, }, query: { @@ -178,15 +183,18 @@ export const updateLabel = async ( script: { source: `if (ctx._source.labels != null) { ctx._source.labels.removeIf(l -> l.id == params.label.id); - ctx._source.labels.add(params.label) + ctx._source.labels.add(params.label); + ctx._source.updatedAt = params.updatedAt } if (ctx._source.highlights != null) { ctx._source.highlights[0].labels.removeIf(l -> l.id == params.label.id); - ctx._source.highlights[0].labels.add(params.label) + ctx._source.highlights[0].labels.add(params.label); + ctx._source.updatedAt = params.updatedAt }`, lang: 'painless', params: { label: label, + updatedAt: new Date(), }, }, query: { @@ -252,10 +260,12 @@ export const setLabelsForHighlight = async ( index: INDEX_ALIAS, body: { script: { - source: `ctx._source.highlights[0].labels = params.labels`, + source: `ctx._source.highlights[0].labels = params.labels; + ctx._source.updatedAt = params.updatedAt`, lang: 'painless', params: { labels: labels, + updatedAt: new Date(), }, }, query: {