Compare commits

..

6 Commits

Author SHA1 Message Date
f32403961d 1.0.55 2023-08-18 07:58:10 +02:00
9734949241 fix(core): update 2023-08-18 07:58:09 +02:00
b70444824b 1.0.54 2023-08-17 20:53:28 +02:00
0eb0903667 fix(core): update 2023-08-17 20:53:28 +02:00
4d11dca22c 1.0.53 2023-08-17 19:24:35 +02:00
3079adbbd9 fix(core): update 2023-08-17 19:24:34 +02:00
3 changed files with 30 additions and 9 deletions

View File

@ -1,6 +1,6 @@
{
"name": "@apiclient.xyz/elasticsearch",
"version": "1.0.52",
"version": "1.0.55",
"private": false,
"description": "log to elasticsearch in a kibana compatible format",
"main": "dist_ts/index.js",

View File

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

View File

@ -1,5 +1,10 @@
import { Client as ElasticClient } from '@elastic/elasticsearch';
interface FastPushOptions {
deleteOldData?: boolean; // Clear the index
deleteIndex?: boolean; // Delete the entire index
}
export class FastPush {
private client: ElasticClient;
@ -10,22 +15,34 @@ export class FastPush {
});
}
async pushToIndex(indexName: string, docArray: any[]) {
async pushToIndex(indexName: string, docArray: any[], options?: FastPushOptions) {
if (docArray.length === 0) return;
// Check if index exists
const { body: indexExists } = await this.client.indices.exists({ index: indexName });
if (!indexExists) {
if (indexExists) {
if (options?.deleteIndex) {
await this.client.indices.delete({ index: indexName });
} else if (options?.deleteOldData) {
await this.client.deleteByQuery({
index: indexName,
body: {
query: {
match_all: {}
}
}
});
}
}
if (!indexExists || options?.deleteIndex) {
// Create index with mappings (for simplicity, we use dynamic mapping)
await this.client.indices.create({
index: indexName,
body: {
mappings: {
dynamic: "true",
properties: {
// If there's a need for specific mappings, they can be added here
},
dynamic: "true"
// ... other specific mappings
},
},
});
@ -45,3 +62,7 @@ export class FastPush {
await this.client.bulk({ body: bulkBody });
}
}
// Usage example:
// const fastPush = new FastPush('http://localhost:9200', { username: 'elastic', password: 'password' });
// fastPush.pushToIndex('my_index', [{ name: 'John', age: 30 }, { name: 'Jane', age: 25 }], { deleteOldData: true });