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 = {
name: '@apiclient.xyz/elasticsearch',
version: '2.0.4',
version: '2.0.5',
description: 'log to elasticsearch in a kibana compatible format'
}

View File

@ -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,14 +89,21 @@ export class ElasticDoc {
} else {
console.log(`Working in "onlyNew" mode, but no documents found in index ${this.index}. Hence processing all documents now.`);
}
} else {
this.latestTimestamp = null;
} 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;
}
}
}
async pipeDocument(optionsArg: { docId: string; timestamp?: string | number; doc: any }) {
await this.ensureIndexExists(optionsArg.doc);