# Guide de DĂ©bogage de la Recherche Meilisearch ## 🔍 ProblĂšme Actuel La recherche Meilisearch est activĂ©e mais les rĂ©sultats ne s'affichent pas dans l'interface. ## ✅ Ce qui Fonctionne ### Backend API - ✅ Meilisearch est actif (port 7700) - ✅ Backend Express est actif (port 4000) - ✅ Index Meilisearch contient 642 documents - ✅ API `/api/search` retourne des rĂ©sultats valides **Test backend :** ```bash curl "http://localhost:4000/api/search?q=test&limit=2" # Retourne: {"hits":[...], "estimatedTotalHits":..., "processingTimeMs":...} ``` ### Configuration - ✅ `USE_MEILI: true` dans `environment.ts` - ✅ Proxy Angular configurĂ© (`/api` → `localhost:4000`) - ✅ HttpClient configurĂ© dans `index.tsx` ### Autres FonctionnalitĂ©s - ✅ **Bookmarks** : Utilise `/api/vault/bookmarks` (backend) - ✅ **Calendrier** : Utilise `/api/files/metadata` (backend) - ✅ **Vault** : Utilise `/api/vault` (backend) ## 🐛 Logs de DĂ©bogage AjoutĂ©s ### Frontend #### 1. SearchMeilisearchService ```typescript // src/core/search/search-meilisearch.service.ts console.log('[SearchMeilisearchService] Sending request:', url); console.log('[SearchMeilisearchService] Request completed'); ``` #### 2. SearchOrchestratorService ```typescript // src/core/search/search-orchestrator.service.ts console.log('[SearchOrchestrator] Calling Meilisearch with query:', query); console.log('[SearchOrchestrator] Raw Meilisearch response:', response); console.log('[SearchOrchestrator] Transformed hit:', { id, matchCount, result }); ``` #### 3. SearchPanelComponent ```typescript // src/components/search-panel/search-panel.component.ts console.log('[SearchPanel] Results received:', arr); console.error('[SearchPanel] Search error:', e); ``` ## đŸ§Ș Tests Ă  Effectuer ### 1. VĂ©rifier les Logs Console 1. **Ouvrir DevTools** (F12) 2. **Aller dans Console** 3. **Taper une recherche** (ex: "test") 4. **Observer les logs** dans l'ordre : ``` [SearchOrchestrator] Calling Meilisearch with query: test [SearchMeilisearchService] Sending request: /api/search?q=test&limit=20&highlight=true [SearchMeilisearchService] Request completed [SearchOrchestrator] Raw Meilisearch response: {hits: [...], ...} [SearchOrchestrator] Transformed hit: {id: "...", matchCount: 1, result: {...}} [SearchPanel] Results received: [{noteId: "...", matches: [...], ...}] ``` ### 2. VĂ©rifier l'Onglet Network 1. **Ouvrir DevTools** → **Network** 2. **Filtrer** : `search` 3. **Taper une recherche** 4. **VĂ©rifier** : - ✅ RequĂȘte GET `/api/search?q=...` - ✅ Status: 200 OK - ✅ Response contient `hits` avec des donnĂ©es ### 3. VĂ©rifier les RĂ©sultats TransformĂ©s Dans la console, aprĂšs une recherche : ```javascript // Inspecter le signal results // (nĂ©cessite Angular DevTools ou breakpoint) ``` ## 🔧 Points de VĂ©rification ### 1. Proxy Angular **Fichier :** `proxy.conf.json` ```json { "/api": { "target": "http://localhost:4000", "secure": false, "changeOrigin": true, "logLevel": "warn" } } ``` **VĂ©rifier :** ```bash # Depuis le navigateur (port 3000) curl http://localhost:3000/api/search?q=test # Doit retourner les mĂȘmes rĂ©sultats que : curl http://localhost:4000/api/search?q=test ``` ### 2. Transformation des RĂ©sultats **Code :** `src/core/search/search-orchestrator.service.ts` Le mapping Meilisearch → SearchResult doit produire : ```typescript { noteId: string, // ID du document matches: [ // Au moins 1 match { type: 'content', text: string, context: string, // Contenu avec ou texte brut ranges: [] } ], score: 100, allRanges: [] } ``` **ProblĂšme potentiel :** Si `matches` est vide, rien ne s'affiche. **Solution :** Le code a Ă©tĂ© modifiĂ© pour toujours crĂ©er au moins un match avec `excerpt` ou `content`. ### 3. Affichage des RĂ©sultats **Composant :** `SearchResultsComponent` **VĂ©rifier dans le template :** ```html @if (sortedGroups().length === 0) { } @else { @for (group of sortedGroups(); track group.noteId) { } } ``` **Signal Ă  inspecter :** - `results()` : Doit contenir les SearchResult[] - `sortedGroups()` : Doit contenir les ResultGroup[] ## 🚹 ProblĂšmes Possibles ### ProblĂšme 1 : RequĂȘte HTTP ne part pas **SymptĂŽmes :** - Aucun log `[SearchMeilisearchService] Sending request` - Aucune requĂȘte dans Network tab **Causes possibles :** - `USE_MEILI` n'est pas Ă  `true` - Service non injectĂ© correctement - Observable non souscrit **Solution :** ```typescript // VĂ©rifier dans environment.ts export const environment = { USE_MEILI: true, // ← Doit ĂȘtre true // ... }; ``` ### ProblĂšme 2 : RequĂȘte part mais pas de rĂ©ponse **SymptĂŽmes :** - Log `[SearchMeilisearchService] Sending request` prĂ©sent - Pas de log `Request completed` - Erreur dans Network tab **Causes possibles :** - Backend non dĂ©marrĂ© - Proxy mal configurĂ© - CORS **Solution :** ```bash # VĂ©rifier backend curl http://localhost:4000/api/search?q=test # VĂ©rifier proxy # RedĂ©marrer ng serve si nĂ©cessaire ``` ### ProblĂšme 3 : RĂ©ponse reçue mais pas de rĂ©sultats **SymptĂŽmes :** - Log `[SearchOrchestrator] Raw Meilisearch response` prĂ©sent - `hits.length > 0` - Mais `matches.length === 0` dans transformed hit **Causes possibles :** - `_formatted` absent dans la rĂ©ponse Meilisearch - `excerpt` absent - Mapping incorrect **Solution :** Le code a Ă©tĂ© modifiĂ© pour crĂ©er un match de fallback avec `excerpt` ou un extrait de `content`. ### ProblĂšme 4 : RĂ©sultats transformĂ©s mais pas affichĂ©s **SymptĂŽmes :** - Log `[SearchPanel] Results received` avec `arr.length > 0` - Mais rien ne s'affiche dans l'UI **Causes possibles :** - Signal `results` non mis Ă  jour - Composant SearchResults ne reçoit pas les donnĂ©es - Change detection non dĂ©clenchĂ©e **Solution :** ```typescript // VĂ©rifier que results.set() est appelĂ© this.results.set(arr); this.hasSearched.set(true); ``` ### ProblĂšme 5 : Highlighting ne fonctionne pas **SymptĂŽmes :** - RĂ©sultats affichĂ©s - Mais pas de surlignage (balises ``) **Causes possibles :** - `_formatted` absent - HTML Ă©chappĂ© - Balises `` non rendues **Solution :** Le code a Ă©tĂ© modifiĂ© pour : 1. DĂ©tecter les balises `` dans `match.context` 2. Retourner le HTML tel quel (sans Ă©chappement) si prĂ©sent 3. Sinon, utiliser le highlighter local ## 📋 Checklist de DĂ©bogage - [ ] Backend dĂ©marrĂ© (`node server/index.mjs`) - [ ] Frontend dĂ©marrĂ© (`npm run dev`) - [ ] Meilisearch actif (`docker ps | grep meilisearch`) - [ ] `USE_MEILI: true` dans `environment.ts` - [ ] Hard refresh du navigateur (Ctrl+Shift+R) - [ ] DevTools Console ouverte - [ ] DevTools Network tab ouverte - [ ] Taper une recherche (2+ caractĂšres) - [ ] VĂ©rifier logs console (ordre attendu) - [ ] VĂ©rifier requĂȘte HTTP dans Network - [ ] VĂ©rifier rĂ©ponse JSON (hits prĂ©sents) - [ ] VĂ©rifier transformation (matches non vide) - [ ] VĂ©rifier affichage (liste de rĂ©sultats visible) ## 🔄 Workflow de Test Complet ```bash # 1. VĂ©rifier Meilisearch docker ps | grep meilisearch curl http://127.0.0.1:7700/health # 2. VĂ©rifier Backend curl "http://localhost:4000/api/search?q=test&limit=1" # Doit retourner JSON avec hits # 3. DĂ©marrer Frontend (si pas dĂ©jĂ  fait) npm run dev # 4. Ouvrir navigateur # http://localhost:3000 # 5. Ouvrir DevTools (F12) # - Console tab # - Network tab # 6. Taper recherche # - Minimum 2 caractĂšres # - Observer logs console # - Observer requĂȘte Network # 7. VĂ©rifier affichage # - Liste de rĂ©sultats doit apparaĂźtre # - Cliquer sur un groupe pour voir les matches # - VĂ©rifier highlighting (balises ) ``` ## 🆘 Si Rien Ne Fonctionne ### Étape 1 : VĂ©rifier Backend IsolĂ© ```bash curl -v "http://localhost:4000/api/search?q=test&limit=1" ``` **Attendu :** - Status: 200 OK - Content-Type: application/json - Body: `{"hits":[...], ...}` ### Étape 2 : VĂ©rifier Proxy ```bash # Depuis un autre terminal curl "http://localhost:3000/api/search?q=test&limit=1" ``` **Attendu :** MĂȘme rĂ©ponse que backend direct ### Étape 3 : VĂ©rifier Logs Backend Dans le terminal oĂč `node server/index.mjs` tourne : ``` [Meili] Search query: test [Meili] Results: 1 hits in 15ms ``` ### Étape 4 : VĂ©rifier Logs Frontend Dans la console du navigateur : ``` [SearchOrchestrator] Calling Meilisearch with query: test [SearchMeilisearchService] Sending request: /api/search?q=test&limit=20&highlight=true ``` ### Étape 5 : Breakpoint Debugging 1. Ouvrir DevTools → Sources 2. Trouver `search-panel.component.ts` 3. Mettre un breakpoint sur `this.results.set(arr)` 4. Taper une recherche 5. Inspecter `arr` quand le breakpoint est atteint ## 📊 MĂ©triques de Performance Une fois fonctionnel, vĂ©rifier : - **Temps de recherche** : < 100ms (Meili + rĂ©seau) - **Nombre de rĂ©sultats** : DĂ©pend de la requĂȘte - **Highlighting** : Balises `` prĂ©sentes - **Pas de gel UI** : Interface reste rĂ©active ## 🎯 Prochaines Étapes Une fois la recherche fonctionnelle : 1. Retirer les `console.log` de dĂ©bogage 2. Optimiser le mapping si nĂ©cessaire 3. Ajouter tests E2E 4. Documenter le comportement final