fix(core): update

This commit is contained in:
Philipp Kunz 2023-08-29 10:00:42 +02:00
parent 41f1758d46
commit 8f00d90bb1
2 changed files with 13 additions and 7 deletions

View File

@ -3,6 +3,6 @@
*/ */
export const commitinfo = { export const commitinfo = {
name: '@apiclient.xyz/elasticsearch', name: '@apiclient.xyz/elasticsearch',
version: '2.0.4', version: '2.0.5',
description: 'log to elasticsearch in a kibana compatible format' description: 'log to elasticsearch in a kibana compatible format'
} }

View File

@ -73,15 +73,14 @@ export class ElasticDoc {
this.onlyNew = options.onlyNew; this.onlyNew = options.onlyNew;
if (this.onlyNew) { if (this.onlyNew) {
// Check if index exists before attempting to search for the latest timestamp try {
const { body: indexExists } = await this.client.indices.exists({ index: this.index });
if (indexExists) {
const response = await this.client.search({ const response = await this.client.search({
index: this.index, index: this.index,
sort: '@timestamp:desc', sort: '@timestamp:desc',
size: 1, size: 1,
}); });
// If the search query succeeded, the index exists.
const hit = response.body.hits.hits[0]; const hit = response.body.hits.hits[0];
this.latestTimestamp = hit?._source?.['@timestamp'] || null; this.latestTimestamp = hit?._source?.['@timestamp'] || null;
@ -90,13 +89,20 @@ export class ElasticDoc {
} else { } else {
console.log(`Working in "onlyNew" mode, but no documents found in index ${this.index}. Hence processing all documents now.`); 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; 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 }) { async pipeDocument(optionsArg: { docId: string; timestamp?: string | number; doc: any }) {
await this.ensureIndexExists(optionsArg.doc); await this.ensureIndexExists(optionsArg.doc);