From 8f00d90bb11d7accdcf0f7d544e6152ddcef40b6 Mon Sep 17 00:00:00 2001 From: Philipp Kunz Date: Tue, 29 Aug 2023 10:00:42 +0200 Subject: [PATCH] fix(core): update --- ts/00_commitinfo_data.ts | 2 +- ts/els.classes.elasticdoc.ts | 18 ++++++++++++------ 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/ts/00_commitinfo_data.ts b/ts/00_commitinfo_data.ts index f9c5034..a3099fa 100644 --- a/ts/00_commitinfo_data.ts +++ b/ts/00_commitinfo_data.ts @@ -3,6 +3,6 @@ */ export const commitinfo = { name: '@apiclient.xyz/elasticsearch', - version: '2.0.4', + version: '2.0.5', description: 'log to elasticsearch in a kibana compatible format' } diff --git a/ts/els.classes.elasticdoc.ts b/ts/els.classes.elasticdoc.ts index ffa2fd1..2ad7ca0 100644 --- a/ts/els.classes.elasticdoc.ts +++ b/ts/els.classes.elasticdoc.ts @@ -73,15 +73,14 @@ export class ElasticDoc { this.onlyNew = options.onlyNew; if (this.onlyNew) { - // Check if index exists before attempting to search for the latest timestamp - const { body: indexExists } = await this.client.indices.exists({ index: this.index }); - - if (indexExists) { + try { const response = await this.client.search({ index: this.index, sort: '@timestamp:desc', size: 1, }); + + // If the search query succeeded, the index exists. const hit = response.body.hits.hits[0]; this.latestTimestamp = hit?._source?.['@timestamp'] || null; @@ -90,13 +89,20 @@ export class ElasticDoc { } else { console.log(`Working in "onlyNew" mode, but no documents found in index ${this.index}. Hence processing all documents now.`); } - } else { + } catch (e) { + // If the search query failed, the index likely doesn't exist or some other error occurred. + if (e.meta && e.meta.statusCode === 404) { + console.log(`Index ${this.index} does not exist. Working in "onlyNew" mode, but will process all documents as the index is empty.`); + } else { + console.log(`An error occurred while trying to retrieve the latest timestamp: ${e}`); + } + this.latestTimestamp = null; - console.log(`Index ${this.index} does not exist. Working in "onlyNew" mode, but will process all documents as the index is empty.`); } } } + async pipeDocument(optionsArg: { docId: string; timestamp?: string | number; doc: any }) { await this.ensureIndexExists(optionsArg.doc);