Files
hasher/lib/elasticsearch.ts
2025-12-04 00:58:40 +01:00

79 líneas
1.6 KiB
TypeScript

import { Client } from '@elastic/elasticsearch';
const ELASTICSEARCH_NODE = process.env.ELASTICSEARCH_NODE || 'http://localhost:9200';
const INDEX_NAME = 'hasher';
export const esClient = new Client({
node: ELASTICSEARCH_NODE,
requestTimeout: 30000,
maxRetries: 3,
});
export const INDEX_MAPPING = {
settings: {
number_of_shards: 10,
number_of_replicas: 1,
analysis: {
analyzer: {
lowercase_analyzer: {
type: 'custom',
tokenizer: 'keyword',
filter: ['lowercase']
}
}
}
},
mappings: {
properties: {
plaintext: {
type: 'text',
analyzer: 'lowercase_analyzer',
fields: {
keyword: {
type: 'keyword'
}
}
},
md5: {
type: 'keyword'
},
sha1: {
type: 'keyword'
},
sha256: {
type: 'keyword'
},
sha512: {
type: 'keyword'
},
bcrypt: {
type: 'keyword'
},
created_at: {
type: 'date'
}
}
}
};
export async function initializeIndex(): Promise<void> {
try {
const indexExists = await esClient.indices.exists({ index: INDEX_NAME });
if (!indexExists) {
await esClient.indices.create({
index: INDEX_NAME,
...INDEX_MAPPING
} as any);
console.log(`Index '${INDEX_NAME}' created successfully with 10 shards`);
} else {
console.log(`Index '${INDEX_NAME}' already exists`);
}
} catch (error) {
console.error('Error initializing Elasticsearch index:', error);
throw error;
}
}
export { INDEX_NAME };