Compare commits
12 Commits
Author | SHA1 | Date | |
---|---|---|---|
4aa0592bd5 | |||
0313e5045a | |||
7442d93f58 | |||
35e70aae62 | |||
cc30f43a28 | |||
100a8fc12e | |||
f32403961d | |||
9734949241 | |||
b70444824b | |||
0eb0903667 | |||
4d11dca22c | |||
3079adbbd9 |
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "@apiclient.xyz/elasticsearch",
|
"name": "@apiclient.xyz/elasticsearch",
|
||||||
"version": "1.0.52",
|
"version": "2.0.1",
|
||||||
"private": false,
|
"private": false,
|
||||||
"description": "log to elasticsearch in a kibana compatible format",
|
"description": "log to elasticsearch in a kibana compatible format",
|
||||||
"main": "dist_ts/index.js",
|
"main": "dist_ts/index.js",
|
||||||
|
@ -50,9 +50,21 @@ tap.test('should create an ElasticDoc instance', async () => {
|
|||||||
|
|
||||||
tap.test('should add and update documents in a piping session', async () => {
|
tap.test('should add and update documents in a piping session', async () => {
|
||||||
await testElasticDoc.startPipingSession();
|
await testElasticDoc.startPipingSession();
|
||||||
await testElasticDoc.pipeDocument('1', { name: 'doc1' });
|
await testElasticDoc.pipeDocument({
|
||||||
await testElasticDoc.pipeDocument('2', { name: 'doc2' });
|
docId: '1',
|
||||||
await testElasticDoc.pipeDocument('1', { name: 'updated doc1' });
|
timestamp: new Date().toISOString(),
|
||||||
|
doc: { name: 'doc1' }
|
||||||
|
});
|
||||||
|
await testElasticDoc.pipeDocument({
|
||||||
|
docId: '2',
|
||||||
|
timestamp: new Date().toISOString(),
|
||||||
|
doc: { name: 'doc2' }
|
||||||
|
});
|
||||||
|
await testElasticDoc.pipeDocument({
|
||||||
|
docId: '1',
|
||||||
|
timestamp: new Date().toISOString(),
|
||||||
|
doc: { name: 'updated doc1' }
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
tap.test('should delete documents not part of the piping session', async () => {
|
tap.test('should delete documents not part of the piping session', async () => {
|
||||||
|
@ -3,6 +3,6 @@
|
|||||||
*/
|
*/
|
||||||
export const commitinfo = {
|
export const commitinfo = {
|
||||||
name: '@apiclient.xyz/elasticsearch',
|
name: '@apiclient.xyz/elasticsearch',
|
||||||
version: '1.0.52',
|
version: '2.0.1',
|
||||||
description: 'log to elasticsearch in a kibana compatible format'
|
description: 'log to elasticsearch in a kibana compatible format'
|
||||||
}
|
}
|
||||||
|
@ -20,6 +20,7 @@ export class ElasticDoc {
|
|||||||
public client: ElasticClient;
|
public client: ElasticClient;
|
||||||
public index: string;
|
public index: string;
|
||||||
private sessionDocs: Set<string> = new Set();
|
private sessionDocs: Set<string> = new Set();
|
||||||
|
private indexInitialized: boolean = false;
|
||||||
|
|
||||||
private BATCH_SIZE = 1000;
|
private BATCH_SIZE = 1000;
|
||||||
|
|
||||||
@ -31,17 +32,59 @@ export class ElasticDoc {
|
|||||||
this.index = options.index;
|
this.index = options.index;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async ensureIndexExists(doc: any) {
|
||||||
|
if (!this.indexInitialized) {
|
||||||
|
const { body: indexExists } = await this.client.indices.exists({ index: this.index });
|
||||||
|
if (!indexExists) {
|
||||||
|
const mappings = this.createMappingsFromDoc(doc);
|
||||||
|
await this.client.indices.create({
|
||||||
|
index: this.index,
|
||||||
|
body: {
|
||||||
|
mappings,
|
||||||
|
settings: {
|
||||||
|
// You can define the settings according to your requirements here
|
||||||
|
},
|
||||||
|
},
|
||||||
|
});
|
||||||
|
}
|
||||||
|
this.indexInitialized = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private createMappingsFromDoc(doc: any): any {
|
||||||
|
const properties: any = {};
|
||||||
|
for (const key in doc) {
|
||||||
|
if (key === '@timestamp') {
|
||||||
|
properties[key] = { type: 'date' };
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
properties[key] = { type: typeof doc[key] === 'number' ? 'float' : 'text' };
|
||||||
|
}
|
||||||
|
return { properties };
|
||||||
|
}
|
||||||
|
|
||||||
async startPipingSession() {
|
async startPipingSession() {
|
||||||
this.sessionDocs.clear();
|
this.sessionDocs.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
async pipeDocument(docId: string, doc: any) {
|
async pipeDocument(optionsArg: {
|
||||||
|
docId: string;
|
||||||
|
timestamp: string | number;
|
||||||
|
doc: any;
|
||||||
|
}) {
|
||||||
|
await this.ensureIndexExists(optionsArg.doc);
|
||||||
|
|
||||||
|
const documentBody = {
|
||||||
|
...optionsArg.doc,
|
||||||
|
...(optionsArg.timestamp && { '@timestamp': optionsArg.timestamp }),
|
||||||
|
};
|
||||||
|
|
||||||
await this.client.index({
|
await this.client.index({
|
||||||
index: this.index,
|
index: this.index,
|
||||||
id: docId,
|
id: optionsArg.docId,
|
||||||
body: doc,
|
body: documentBody,
|
||||||
});
|
});
|
||||||
this.sessionDocs.add(docId);
|
this.sessionDocs.add(optionsArg.docId);
|
||||||
}
|
}
|
||||||
|
|
||||||
async endPipingSession() {
|
async endPipingSession() {
|
||||||
|
@ -1,5 +1,10 @@
|
|||||||
import { Client as ElasticClient } from '@elastic/elasticsearch';
|
import { Client as ElasticClient } from '@elastic/elasticsearch';
|
||||||
|
|
||||||
|
interface FastPushOptions {
|
||||||
|
deleteOldData?: boolean; // Clear the index
|
||||||
|
deleteIndex?: boolean; // Delete the entire index
|
||||||
|
}
|
||||||
|
|
||||||
export class FastPush {
|
export class FastPush {
|
||||||
private client: ElasticClient;
|
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;
|
if (docArray.length === 0) return;
|
||||||
|
|
||||||
// Check if index exists
|
|
||||||
const { body: indexExists } = await this.client.indices.exists({ index: indexName });
|
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)
|
// Create index with mappings (for simplicity, we use dynamic mapping)
|
||||||
await this.client.indices.create({
|
await this.client.indices.create({
|
||||||
index: indexName,
|
index: indexName,
|
||||||
body: {
|
body: {
|
||||||
mappings: {
|
mappings: {
|
||||||
dynamic: "true",
|
dynamic: "true"
|
||||||
properties: {
|
// ... other specific mappings
|
||||||
// If there's a need for specific mappings, they can be added here
|
|
||||||
},
|
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
@ -45,3 +62,7 @@ export class FastPush {
|
|||||||
await this.client.bulk({ body: bulkBody });
|
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 });
|
||||||
|
Reference in New Issue
Block a user