69 lines
1.8 KiB
TypeScript
69 lines
1.8 KiB
TypeScript
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;
|
|
|
|
constructor(node: string, auth?: { username: string; password: string }) {
|
|
this.client = new ElasticClient({
|
|
node: node,
|
|
...(auth && { auth: auth }),
|
|
});
|
|
}
|
|
|
|
async pushToIndex(indexName: string, docArray: any[], options?: FastPushOptions) {
|
|
if (docArray.length === 0) return;
|
|
|
|
const indexExists = await this.client.indices.exists({ index: indexName });
|
|
|
|
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"
|
|
// ... other specific mappings
|
|
},
|
|
},
|
|
});
|
|
}
|
|
|
|
// Bulk insert documents
|
|
const bulkBody = [];
|
|
for (const doc of docArray) {
|
|
bulkBody.push({
|
|
index: {
|
|
_index: indexName,
|
|
},
|
|
});
|
|
bulkBody.push(doc);
|
|
}
|
|
|
|
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 });
|