import { MeiliSearch } from 'meilisearch' class MeilisearchService { private client: MeiliSearch; constructor() { this.client = new MeiliSearch({ host: 'http://localhost:7700', }); this.createIndex('files'); // Ensure the index exists } // Implement methods for adding, removing, and updating documents and collections // using the Meilisearch API. public async addDocuments(indexName: string, documents: any[]): Promise { try { const index = this.client.index(indexName) await index.addDocuments(documents) console.log(`Added ${documents.length} documents to index ${indexName}`); } catch (error) { console.error(`Failed to add documents to index ${indexName}:`, error); throw error; } } public async removeDocuments(indexName: string, documentIds: string[]): Promise { try { const index = this.client.index(indexName) await index.deleteDocuments(documentIds) console.log(`Removed documents with IDs ${documentIds.join(',')} from index ${indexName}`); } catch (error) { console.error(`Failed to remove documents from index ${indexName}:`, error); throw error; } } public async updateDocuments(indexName: string, documents: any[]): Promise { try { const index = this.client.index(indexName) await index.updateDocuments(documents) console.log(`Updated ${documents.length} documents in index ${indexName}`); } catch (error) { console.error(`Failed to update documents in index ${indexName}:`, error); throw error; } } public async deleteIndex(indexName: string): Promise { try { await this.client.deleteIndex(indexName); console.log(`Deleted index ${indexName}`); } catch (error) { console.error(`Failed to delete index ${indexName}:`, error); throw error; } } public async createIndex(indexName: string): Promise { try { await this.client.createIndex(indexName); console.log(`Created index ${indexName}`); } catch (error) { console.error(`Failed to create index ${indexName}:`, error); throw error; } } public async getDocuments(indexName: string): Promise { try { const index = this.client.index(indexName); const { hits } = await index.search(''); // Empty search to get all documents return hits; } catch (error) { console.error(`Failed to get documents from index ${indexName}:`, error); throw error; } } public async search(indexName: string, query: string): Promise { try { const index = this.client.index(indexName); const { hits } = await index.search(query); return hits; } catch (error) { console.error(`Failed to search index ${indexName} with query ${query}:`, error); throw error; } } } export default MeilisearchService;