graphrag-search 0.2.11

GraphRAG — Hybrid search engine: vector embeddings + knowledge graph over Markdown notes. 100% local, single Rust binary.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
use anyhow::Result;
use log::debug;
use rusqlite::Connection;
use serde::Serialize;
use std::collections::{HashMap, HashSet};

use crate::embed::ollama::OllamaClient;
use crate::graph::expand::{self, Neighbor};
use crate::search::filter::Filter;
use crate::vector;

/// Clips a text string to at most `max_len` characters.
fn clip_text(s: &str, max_len: usize) -> String {
    if s.len() <= max_len {
        s.to_string()
    } else {
        let end = s
            .char_indices()
            .nth(max_len)
            .map(|(i, _)| i)
            .unwrap_or(s.len());
        format!("{}...", &s[..end])
    }
}

/// Un resultado individual de la búsqueda híbrida
#[derive(Debug, Clone, Serialize)]
pub struct SearchResult {
    pub id: i64,
    pub label: String,
    pub r#type: String,
    pub score: f64,
    pub content: String,
    /// Ruta del archivo (si es una nota)
    pub file: String,
    pub neighbors: Vec<Neighbor>,
    /// Cabecera del chunk que matched (si aplica)
    pub chunk_header: String,
    /// Texto completo del chunk que matched (si aplica)
    pub chunk_text: String,
}

impl SearchResult {
    #[allow(dead_code)]
    fn new(
        id: i64,
        label: String,
        type_: String,
        score: f64,
        content: String,
        file: String,
    ) -> Self {
        Self {
            id,
            label,
            r#type: type_,
            score,
            content,
            file,
            neighbors: Vec::new(),
            chunk_header: String::new(),
            chunk_text: String::new(),
        }
    }
}

/// Metadatos de un chunk cargado para búsqueda
#[derive(Debug, Clone)]
struct ChunkMeta {
    #[allow(dead_code)]
    chunk_id: i64,
    note_id: i64,
    header: String,
    text: String,
}

/// Motor de búsqueda híbrida: vectores (por chunks) + grafos de conocimiento
pub struct HybridSearch {
    conn: Connection,
    ollama: OllamaClient,
    // Vectores de chunks
    chunk_vecs: Vec<Vec<f32>>,
    chunk_meta: Vec<ChunkMeta>,
    loaded: bool,
    // Cache de embeddings de consultas
    embed_cache: std::collections::HashMap<String, Vec<f32>>,
}

impl HybridSearch {
    /// Crea un nuevo motor HybridSearch
    pub fn new(db_path: &str, ollama: OllamaClient) -> Result<Self> {
        let conn = Connection::open(db_path)?;
        debug!("HybridSearch abierto: db={}", db_path);

        Ok(Self {
            conn,
            ollama,
            chunk_vecs: Vec::new(),
            chunk_meta: Vec::new(),
            loaded: false,
            embed_cache: std::collections::HashMap::new(),
        })
    }

    /// Constructor internal para tests (usa conexión existente)
    #[cfg(test)]
    fn new_internal(conn: Connection, ollama: OllamaClient) -> Result<Self> {
        Ok(Self {
            conn,
            ollama,
            chunk_vecs: Vec::new(),
            chunk_meta: Vec::new(),
            loaded: false,
            embed_cache: std::collections::HashMap::new(),
        })
    }

    /// Genera embedding para un texto usando Ollama
    fn embed(&mut self, text: &str) -> Result<Vec<f32>> {
        if let Some(cached) = self.embed_cache.get(text) {
            let preview: String = text.chars().take(40).collect();
            debug!("Embedding cache hit: '{}'...", preview);
            return Ok(cached.clone());
        }

        let vec = self.ollama.embed(text)?;
        let preview: String = text.chars().take(40).collect();
        debug!(
            "Embedding generado: '{}'... ({}d, Ollama)",
            preview,
            vec.len()
        );

        self.embed_cache.insert(text.to_string(), vec.clone());
        Ok(vec)
    }

    /// Carga todos los embeddings de chunks desde SQLite
    fn load_all_chunks(&mut self) -> Result<()> {
        if self.loaded {
            return Ok(());
        }

        let data = crate::db::chunks::load_all_chunk_embeddings(&self.conn)?;
        for (chunk_id, note_id, vec, header, text) in data {
            if vec.is_empty() {
                continue; // saltar chunks sin embedding
            }
            self.chunk_vecs.push(vec);
            self.chunk_meta.push(ChunkMeta {
                chunk_id,
                note_id,
                header,
                text,
            });
        }

        self.loaded = true;
        debug!("Chunks cargados: {} con embedding.", self.chunk_vecs.len());
        Ok(())
    }

    /// Computa similitud coseno de `query_vec` contra todos los chunks cargados.
    /// Devuelve Vec<(chunk_index, score)> para TODOS los chunks, ordenado por score descendente.
    fn compute_similarities(&self, query_vec: &[f32]) -> Vec<(usize, f64)> {
        let mut sims: Vec<(usize, f64)> = self
            .chunk_vecs
            .iter()
            .enumerate()
            .map(|(i, v)| {
                let sim = vector::cosine_similarity_raw(v, query_vec) as f64;
                (i, sim)
            })
            .collect();
        sims.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));
        sims
    }

    /// Obtiene la ruta del archivo de un nodo desde metadata JSON
    fn get_file(&self, node_id: i64) -> Result<String> {
        let result: std::result::Result<String, _> = self.conn.query_row(
            "SELECT COALESCE(metadata, '{}') FROM nodes WHERE id = ?1",
            rusqlite::params![node_id],
            |row| row.get(0),
        );

        match result {
            Ok(meta_str) => {
                if let Ok(meta) = serde_json::from_str::<serde_json::Value>(&meta_str) {
                    if let Some(path) = meta.get("path").and_then(|f| f.as_str()) {
                        return Ok(path.to_string());
                    }
                }
                Ok(String::new())
            }
            Err(rusqlite::Error::QueryReturnedNoRows) => Ok(String::new()),
            Err(e) => Err(e.into()),
        }
    }

    /// Obtiene el contenido textual de un nodo desde metadata JSON
    fn get_content(&self, node_id: i64) -> Result<String> {
        let result: std::result::Result<String, _> = self.conn.query_row(
            "SELECT COALESCE(metadata, '{}') FROM nodes WHERE id = ?1",
            rusqlite::params![node_id],
            |row| row.get(0),
        );

        match result {
            Ok(meta_str) => {
                if let Ok(meta) = serde_json::from_str::<serde_json::Value>(&meta_str) {
                    if let Some(content) = meta.get("content").and_then(|c| c.as_str()) {
                        return Ok(content.to_string());
                    }
                    if let Some(desc) = meta.get("description").and_then(|d| d.as_str()) {
                        return Ok(desc.to_string());
                    }
                }
                Ok(String::new())
            }
            Err(rusqlite::Error::QueryReturnedNoRows) => Ok(String::new()),
            Err(e) => Err(e.into()),
        }
    }

    /// Obtiene el label de un nodo desde la tabla `nodes`
    fn get_note_label(&self, note_id: i64) -> Result<String> {
        let result: std::result::Result<String, _> = self.conn.query_row(
            "SELECT label FROM nodes WHERE id = ?1",
            rusqlite::params![note_id],
            |row| row.get(0),
        );
        match result {
            Ok(label) => Ok(label),
            Err(rusqlite::Error::QueryReturnedNoRows) => Ok(format!("note#{}", note_id)),
            Err(e) => Err(e.into()),
        }
    }

    /// Obtiene el label de un nodo, aplicando filtros de metadatos.
    ///
    /// Returns `None` si el nodo existe pero no cumple los filtros,
    /// o si el nodo no existe.
    fn get_note_label_with_filters(
        &self,
        note_id: i64,
        filters: &[Filter],
    ) -> Result<Option<String>> {
        if filters.is_empty() {
            // Sin filtros, comportamiento normal
            return Ok(Some(self.get_note_label(note_id)?));
        }

        let mut sql = "SELECT label FROM nodes WHERE id = ?1".to_string();
        let (filter_sql, filter_vals) = crate::search::filter::build_filter_sql(filters);
        sql.push_str(&filter_sql);

        let mut params: Vec<Box<dyn rusqlite::types::ToSql>> = vec![Box::new(note_id)];
        for v in &filter_vals {
            params.push(Box::new(v.clone()));
        }

        let param_refs: Vec<&dyn rusqlite::types::ToSql> =
            params.iter().map(|p| p.as_ref()).collect();

        let result: std::result::Result<String, _> =
            self.conn
                .query_row(&sql, param_refs.as_slice(), |row| row.get(0));

        match result {
            Ok(label) => Ok(Some(label)),
            Err(rusqlite::Error::QueryReturnedNoRows) => Ok(None),
            Err(e) => Err(e.into()),
        }
    }

    /// Búsqueda híbrida completa: 3 fases
    ///
    /// FASE 1 — Búsqueda vectorial: similitud coseno sobre chunks
    /// FASE 2 — Reranking: bonificaciones por título y agrupación por nota
    ///   Si se proporcionan `filters`, se aplican contra el metadata JSON
    ///   de la nota padre. Los chunks cuya nota no cumpla los filtros se omiten.
    /// FASE 3 — Expansión por grafo: CTE recursiva desde la nota padre
    #[allow(clippy::too_many_arguments)]
    pub fn hybrid_search(
        &mut self,
        query: &str,
        k: usize,
        depth: i32,
        alpha: f64,
        min_weight: Option<f64>,
        notes_only: bool,
        filters: &[Filter],
    ) -> Result<Vec<SearchResult>> {
        // === FASE 1: Búsqueda vectorial sobre chunks ===
        let query_vec = self.embed(query)?;
        self.load_all_chunks()?;

        if self.chunk_vecs.is_empty() {
            debug!("No chunks with embeddings found in database.");
            return Ok(Vec::new());
        }

        let query_norm = vector::norm(&query_vec);
        if query_norm == 0.0 {
            return Ok(Vec::new());
        }

        // Calcular similitud coseno con todos los vectores de chunks
        let sims = self.compute_similarities(&query_vec);
        let top_k: Vec<_> = sims.into_iter().take(k).collect();

        if top_k.is_empty() {
            return Ok(Vec::new());
        }

        debug!(
            "FASE 1 — Vector search: top {} de {} chunks. Mejor score: {:.4}",
            top_k.len(),
            self.chunk_vecs.len(),
            top_k.first().map(|(_, s)| s).unwrap_or(&0.0)
        );

        // === FASE 2: Reranking con agrupación por nota ===
        let query_lower = query.to_lowercase();
        let query_words: HashSet<&str> = query_lower.split_whitespace().collect();

        // Cache de labels de notas para evitar queries repetidas
        let mut label_cache: HashMap<i64, String> = HashMap::new();

        /// Candidato interno tras reranking
        struct ChunkCandidate {
            note_id: i64,
            score: f64,
            header: String,
            text: String,
        }

        let mut seen_notes: HashSet<i64> = HashSet::new();
        let mut candidates: Vec<ChunkCandidate> = Vec::new();

        for (idx, vec_score) in &top_k {
            let meta = &self.chunk_meta[*idx];

            // Saltar chunks repetidos de la misma nota (quedamos con el mejor)
            if seen_notes.contains(&meta.note_id) {
                continue;
            }
            seen_notes.insert(meta.note_id);

            // Obtener label de la nota padre, con filtros si existen
            let note_label = match label_cache.get(&meta.note_id) {
                Some(l) => l.clone(),
                None => {
                    match self.get_note_label_with_filters(meta.note_id, filters)? {
                        Some(l) => {
                            label_cache.insert(meta.note_id, l.clone());
                            l
                        }
                        None => {
                            // Nota no cumple filtros → omitir este chunk
                            continue;
                        }
                    }
                }
            };

            // Bonus si el título de la nota contiene palabras de la query
            let label_lower = note_label.to_lowercase();
            let label_words: HashSet<&str> = label_lower.split_whitespace().collect();
            let title_bonus = if query_words.intersection(&label_words).next().is_some() {
                0.1
            } else {
                0.0
            };

            // Bonus por ser nota (los chunks siempre son de notas)
            let type_bonus = 0.2;

            let score = alpha * (*vec_score + title_bonus + type_bonus);
            candidates.push(ChunkCandidate {
                note_id: meta.note_id,
                score,
                header: meta.header.clone(),
                text: meta.text.clone(),
            });
        }

        candidates.sort_by(|a, b| {
            b.score
                .partial_cmp(&a.score)
                .unwrap_or(std::cmp::Ordering::Equal)
        });

        debug!(
            "FASE 2 — Reranking: {} candidatos (agrupados por nota). Mejor: '{}' ({:.4})",
            candidates.len(),
            candidates
                .first()
                .map(|c| { label_cache.get(&c.note_id).cloned().unwrap_or_default() })
                .unwrap_or_default(),
            candidates.first().map(|c| c.score).unwrap_or(0.0)
        );

        // === FASE 3: Expansión por grafo ===
        let mut seen_ids: HashSet<i64> = HashSet::new();
        let mut results: Vec<SearchResult> = Vec::new();

        for candidate in &candidates {
            if seen_ids.contains(&candidate.note_id) {
                continue;
            }
            seen_ids.insert(candidate.note_id);

            let neighbors = if depth > 0 {
                if let Some(min_w) = min_weight {
                    expand::expand_neighbors_weighted(&self.conn, candidate.note_id, depth, min_w)?
                } else {
                    expand::expand_neighbors(&self.conn, candidate.note_id, depth)?
                }
            } else {
                Vec::new()
            };

            let note_label = label_cache
                .get(&candidate.note_id)
                .cloned()
                .unwrap_or_else(|| self.get_note_label(candidate.note_id).unwrap_or_default());

            let result = SearchResult {
                id: candidate.note_id,
                label: note_label.clone(),
                r#type: "note".to_string(),
                score: candidate.score,
                content: clip_text(&candidate.text, 200),
                file: self.get_file(candidate.note_id)?,
                neighbors: neighbors.clone(),
                chunk_header: candidate.header.clone(),
                chunk_text: candidate.text.clone(),
            };
            results.push(result);

            // Añadir vecinos con score reducido
            for n in neighbors.iter().take(3) {
                if seen_ids.contains(&n.id) {
                    continue;
                }
                seen_ids.insert(n.id);

                let neighbor_score = (1.0 - alpha) * candidate.score;
                results.push(SearchResult {
                    id: n.id,
                    label: n.label.clone(),
                    r#type: n.r#type.clone(),
                    score: neighbor_score,
                    content: self.get_content(n.id)?,
                    file: self.get_file(n.id)?,
                    neighbors: Vec::new(),
                    chunk_header: String::new(),
                    chunk_text: String::new(),
                });
            }
        }

        debug!(
            "FASE 3 — Graph expansion: {} resultados finales (depth={})",
            results.len(),
            depth
        );

        // Orden final y límite
        results.sort_by(|a, b| {
            b.score
                .partial_cmp(&a.score)
                .unwrap_or(std::cmp::Ordering::Equal)
        });
        results.truncate(k * 2);

        // Normalizar scores (min-max)
        let (min_score, max_score) = if !results.is_empty() {
            let max_s = results
                .iter()
                .map(|r| r.score)
                .fold(f64::NEG_INFINITY, f64::max);
            let min_s = results
                .iter()
                .map(|r| r.score)
                .fold(f64::INFINITY, f64::min);
            if max_s > min_s {
                for r in &mut results {
                    r.score = (r.score - min_s) / (max_s - min_s);
                }
            }
            (min_s, max_s)
        } else {
            (0.0, 0.0)
        };
        debug!(
            "Normalización min-max: score range [{:.4}, {:.4}]",
            min_score, max_score
        );

        // Filtrar solo notas si se solicitó
        let results = if notes_only {
            results.into_iter().filter(|r| r.r#type == "note").collect()
        } else {
            results
        };

        Ok(results)
    }

    /// Búsqueda solo por grafo desde un nodo
    pub fn graph_only(&self, node_label: &str, depth: i32) -> Result<Vec<Neighbor>> {
        let id: i64 = self
            .conn
            .query_row(
                "SELECT id FROM nodes WHERE key = ?1 OR label = ?1 \
                 ORDER BY (key = ?1) DESC, id ASC LIMIT 1",
                rusqlite::params![node_label],
                |row| row.get(0),
            )
            .map_err(|_| anyhow::anyhow!("Nodo '{}' no encontrado", node_label))?;

        expand::expand_neighbors(&self.conn, id, depth)
    }

    /// Busca notas similares a una nota ya indexada, comparando cada chunk
    /// individualmente (match individual) y agregando por nota destino con score = max.
    /// Sin llamar a Ollama — reusa embeddings almacenados en la BD.
    pub fn similar_by_label(
        &mut self,
        label: &str,
        k: usize,
        depth: i32,
        min_weight: Option<f64>,
        notes_only: bool,
        _filters: &[Filter],
    ) -> Result<Vec<SearchResult>> {
        // 1. Resolver label → note_id
        let note_id: i64 = self
            .conn
            .query_row(
                "SELECT id FROM nodes WHERE key = ?1 OR label = ?1 \
                 ORDER BY (key = ?1) DESC, id ASC LIMIT 1",
                rusqlite::params![label],
                |row| row.get(0),
            )
            .map_err(|_| anyhow::anyhow!("Nodo '{}' no encontrado", label))?;

        // 2. Cargar chunks de la nota origen
        let source_chunks = crate::db::chunks::get_chunks_by_note(&self.conn, note_id)?;

        if source_chunks.is_empty() {
            return Ok(Vec::new());
        }

        // 3. Asegurar que los embeddings de chunks están cargados
        self.load_all_chunks()?;

        if self.chunk_vecs.is_empty() {
            return Ok(Vec::new());
        }

        // 4. Match individual: para cada chunk origen, compute_similarities,
        //    agregar por nota destino con max.
        //    Excluir los chunks de la propia nota origen.
        use std::collections::HashMap;

        let mut note_scores: HashMap<i64, f64> = HashMap::new();
        let mut label_cache: HashMap<i64, String> = HashMap::new();

        for chunk in &source_chunks {
            let emb = crate::vector::blob_to_vector(chunk.embedding.as_deref().unwrap_or(&[]))?;
            if emb.is_empty() {
                continue;
            }

            let sims = self.compute_similarities(&emb);

            for (idx, score) in &sims {
                let target_note_id = self.chunk_meta[*idx].note_id;
                // Excluir la propia nota origen
                if target_note_id == note_id {
                    continue;
                }
                // Actualizar score con el máximo encontrado
                let entry = note_scores.entry(target_note_id).or_insert(0.0);
                if *score > *entry {
                    *entry = *score;
                }
            }
        }

        // 5. Tomar top-K notas por score
        let mut sorted: Vec<(i64, f64)> = note_scores.into_iter().collect();
        sorted.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));
        sorted.truncate(k);

        // 6. Reranking + graph expansion (similar a hybrid_search FASE 3)
        let mut seen_ids: std::collections::HashSet<i64> = std::collections::HashSet::new();
        let mut results: Vec<SearchResult> = Vec::new();

        for (target_note_id, score) in &sorted {
            if seen_ids.contains(target_note_id) {
                continue;
            }
            seen_ids.insert(*target_note_id);

            let neighbors = if depth > 0 {
                if let Some(min_w) = min_weight {
                    crate::graph::expand::expand_neighbors_weighted(
                        &self.conn,
                        *target_note_id,
                        depth,
                        min_w,
                    )?
                } else {
                    crate::graph::expand::expand_neighbors(&self.conn, *target_note_id, depth)?
                }
            } else {
                Vec::new()
            };

            let note_label = match label_cache.get(target_note_id) {
                Some(l) => l.clone(),
                None => {
                    let l = self.get_note_label(*target_note_id)?;
                    label_cache.insert(*target_note_id, l.clone());
                    l
                }
            };

            // Obtener el chunk header/text del mejor match para display
            let chunk_info = {
                let mut best = (String::new(), String::new(), 0.0_f64);
                for chunk in &source_chunks {
                    let emb =
                        crate::vector::blob_to_vector(chunk.embedding.as_deref().unwrap_or(&[]))
                            .ok();
                    if let Some(ref e) = emb {
                        let sims = self.compute_similarities(e);
                        for (idx, s) in &sims {
                            if self.chunk_meta[*idx].note_id == *target_note_id && *s > best.2 {
                                best = (
                                    self.chunk_meta[*idx].header.clone(),
                                    self.chunk_meta[*idx].text.clone(),
                                    *s,
                                );
                            }
                        }
                    }
                }
                best
            };

            results.push(SearchResult {
                id: *target_note_id,
                label: note_label,
                r#type: "note".to_string(),
                score: *score,
                content: clip_text(&chunk_info.1, 200),
                file: self.get_file(*target_note_id)?,
                neighbors,
                chunk_header: chunk_info.0,
                chunk_text: chunk_info.1,
            });
        }

        // Orden final y límite
        results.sort_by(|a, b| {
            b.score
                .partial_cmp(&a.score)
                .unwrap_or(std::cmp::Ordering::Equal)
        });
        results.truncate(k * 2);

        // Normalizar scores (min-max)
        if !results.is_empty() {
            let max_s = results
                .iter()
                .map(|r| r.score)
                .fold(f64::NEG_INFINITY, f64::max);
            let min_s = results
                .iter()
                .map(|r| r.score)
                .fold(f64::INFINITY, f64::min);
            if max_s > min_s {
                for r in &mut results {
                    r.score = (r.score - min_s) / (max_s - min_s);
                }
            }
        }

        if notes_only {
            results.retain(|r| r.r#type == "note");
        }

        Ok(results)
    }

    /// Busca notas similares a un archivo externo (no indexado en la BD),
    /// chonkeándolo, generando embeddings vía Ollama, y comparando
    /// cada chunk individualmente (match individual).
    pub fn similar_by_file(
        &mut self,
        path: &str,
        k: usize,
        depth: i32,
        min_weight: Option<f64>,
        notes_only: bool,
        _filters: &[Filter],
    ) -> Result<Vec<SearchResult>> {
        let content = std::fs::read_to_string(path)
            .map_err(|_| anyhow::anyhow!("Archivo no encontrado: {}", path))?;

        let chunks = crate::chunking::markdown::chunk_document(&content);
        if chunks.is_empty() {
            return Ok(Vec::new());
        }

        // Concatenar header + "\n" + text para cada chunk
        let embed_texts: Vec<String> = chunks
            .iter()
            .map(|c| format!("{}\n{}", c.header, c.text))
            .collect();
        let embed_refs: Vec<&str> = embed_texts.iter().map(|s| s.as_str()).collect();

        // Batch embed via Ollama
        let embeddings = self.ollama.batch_embed(&embed_refs)?;

        // Cargar chunks de la BD
        self.load_all_chunks()?;

        if self.chunk_vecs.is_empty() {
            return Ok(Vec::new());
        }

        // Match individual: para cada chunk origen, compute_similarities,
        // agregar por nota destino con max
        use std::collections::HashMap;
        let mut note_scores: HashMap<i64, f64> = HashMap::new();
        let mut label_cache: HashMap<i64, String> = HashMap::new();

        for emb in &embeddings {
            let sims = self.compute_similarities(emb);

            for (idx, score) in &sims {
                let target_note_id = self.chunk_meta[*idx].note_id;
                let entry = note_scores.entry(target_note_id).or_insert(0.0);
                if *score > *entry {
                    *entry = *score;
                }
            }
        }

        // Tomar top-K
        let mut sorted: Vec<(i64, f64)> = note_scores.into_iter().collect();
        sorted.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));
        sorted.truncate(k);

        // Graph expansion + output assembly (idéntico a similar_by_label)
        let mut seen_ids: std::collections::HashSet<i64> = std::collections::HashSet::new();
        let mut results: Vec<SearchResult> = Vec::new();

        for (target_note_id, score) in &sorted {
            if seen_ids.contains(target_note_id) {
                continue;
            }
            seen_ids.insert(*target_note_id);

            let neighbors = if depth > 0 {
                if let Some(min_w) = min_weight {
                    crate::graph::expand::expand_neighbors_weighted(
                        &self.conn,
                        *target_note_id,
                        depth,
                        min_w,
                    )?
                } else {
                    crate::graph::expand::expand_neighbors(&self.conn, *target_note_id, depth)?
                }
            } else {
                Vec::new()
            };

            let note_label = match label_cache.get(target_note_id) {
                Some(l) => l.clone(),
                None => {
                    let l = self.get_note_label(*target_note_id)?;
                    label_cache.insert(*target_note_id, l.clone());
                    l
                }
            };

            // Mejor chunk de display
            let mut best = (String::new(), String::new(), 0.0_f64);
            for emb in &embeddings {
                let sims = self.compute_similarities(emb);
                for (idx, s) in &sims {
                    if self.chunk_meta[*idx].note_id == *target_note_id && *s > best.2 {
                        best = (
                            self.chunk_meta[*idx].header.clone(),
                            self.chunk_meta[*idx].text.clone(),
                            *s,
                        );
                    }
                }
            }

            results.push(SearchResult {
                id: *target_note_id,
                label: note_label,
                r#type: "note".to_string(),
                score: *score,
                content: clip_text(&best.1, 200),
                file: self.get_file(*target_note_id)?,
                neighbors,
                chunk_header: best.0,
                chunk_text: best.1,
            });
        }

        results.sort_by(|a, b| {
            b.score
                .partial_cmp(&a.score)
                .unwrap_or(std::cmp::Ordering::Equal)
        });
        results.truncate(k * 2);

        if !results.is_empty() {
            let max_s = results
                .iter()
                .map(|r| r.score)
                .fold(f64::NEG_INFINITY, f64::max);
            let min_s = results
                .iter()
                .map(|r| r.score)
                .fold(f64::INFINITY, f64::min);
            if max_s > min_s {
                for r in &mut results {
                    r.score = (r.score - min_s) / (max_s - min_s);
                }
            }
        }

        if notes_only {
            results.retain(|r| r.r#type == "note");
        }

        Ok(results)
    }

    /// Búsqueda FTS5 (textual exacta)
    pub fn fts_search(
        &self,
        query: &str,
        limit: usize,
        notes_only: bool,
    ) -> Result<Vec<SearchResult>> {
        let mut stmt = self.conn.prepare(
            "SELECT n.id, n.label, n.type, n.metadata, rank AS score
             FROM notes_fts
             JOIN nodes n ON notes_fts.rowid = n.id
             WHERE notes_fts MATCH ?1
             ORDER BY rank
             LIMIT ?2",
        )?;

        let rows = stmt.query_map(rusqlite::params![query, limit as i64], |row| {
            let id: i64 = row.get(0)?;
            let label: String = row.get(1)?;
            let type_: String = row.get(2)?;
            let meta_str: String = row.get::<_, Option<String>>(3)?.unwrap_or_default();
            let score: f64 = row.get(4)?;

            let content = serde_json::from_str::<serde_json::Value>(&meta_str)
                .ok()
                .and_then(|v| {
                    v.get("content")
                        .and_then(|c| c.as_str().map(|s| s.to_owned()))
                })
                .unwrap_or_default();

            Ok((id, label, type_, score, content))
        })?;

        let mut results = Vec::new();
        for row in rows {
            let (id, label, type_, score, content) = row?;
            let file = self.get_file(id)?;
            results.push(SearchResult::new(id, label, type_, score, content, file));
        }
        debug!("FTS search: '{}' → {} resultados", query, results.len());

        if notes_only {
            results.retain(|r| r.r#type == "note");
        }

        Ok(results)
    }

    /// Estadísticas de la base de datos
    pub fn stats(&self) -> Result<DbStats> {
        let nodes: i64 = self
            .conn
            .query_row("SELECT COUNT(*) FROM nodes", [], |r| r.get(0))?;
        let notes: i64 =
            self.conn
                .query_row("SELECT COUNT(*) FROM nodes WHERE type='note'", [], |r| {
                    r.get(0)
                })?;
        let entities: i64 = self.conn.query_row(
            "SELECT COUNT(*) FROM nodes WHERE type != 'note' AND type != 'tag'",
            [],
            |r| r.get(0),
        )?;
        let tags: i64 =
            self.conn
                .query_row("SELECT COUNT(*) FROM nodes WHERE type='tag'", [], |r| {
                    r.get(0)
                })?;
        let edges: i64 = self
            .conn
            .query_row("SELECT COUNT(*) FROM edges", [], |r| r.get(0))?;
        let with_emb: i64 = self.conn.query_row(
            "SELECT COUNT(*) FROM chunks WHERE embedding IS NOT NULL",
            [],
            |r| r.get(0),
        )?;
        let chunks: i64 = self
            .conn
            .query_row("SELECT COUNT(*) FROM chunks", [], |r| r.get(0))?;

        Ok(DbStats {
            nodes,
            notes,
            entities,
            tags,
            edges,
            with_embeddings: with_emb,
            chunks,
        })
    }

    #[allow(dead_code)]
    pub fn close(self) {
        drop(self.conn);
    }
}

/// Estadísticas de la base de datos
#[derive(Debug, Serialize)]
pub struct DbStats {
    pub nodes: i64,
    pub notes: i64,
    pub entities: i64,
    pub tags: i64,
    pub edges: i64,
    pub with_embeddings: i64,
    pub chunks: i64,
}

impl std::fmt::Display for DbStats {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "📊 Estadísticas del grafo:\n\
             ├── Nodos totales:     {}\n\
             ├── Notas:             {}\n\
             ├── Entidades:         {}\n\
             ├── Tags:              {}\n\
             ├── Aristas:           {}\n\
             ├── Chunks:            {}\n\
             └── Con embeddings:    {}",
            self.nodes,
            self.notes,
            self.entities,
            self.tags,
            self.edges,
            self.chunks,
            self.with_embeddings
        )
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::db::schema::init_db;
    use crate::search::filter::Filter;
    use crate::vector::vector_to_blob;

    /// Inserta una nota usando el helper real de claves (`note:<path>`).
    fn insert_note(conn: &rusqlite::Connection, id: i64, name: &str, metadata: Option<&str>) {
        match metadata {
            Some(meta) => conn
                .execute(
                    "INSERT INTO nodes (id, key, label, type, metadata) VALUES (?1, ?2, ?3, 'note', ?4)",
                    rusqlite::params![id, crate::db::keys::note_key(name), name, meta],
                )
                .unwrap(),
            None => conn
                .execute(
                    "INSERT INTO nodes (id, key, label, type) VALUES (?1, ?2, ?3, 'note')",
                    rusqlite::params![id, crate::db::keys::note_key(name), name],
                )
                .unwrap(),
        };
    }

    /// Creates an in-memory database with a note and some chunks with embeddings.
    fn setup_db_with_chunks() -> rusqlite::Connection {
        let conn = rusqlite::Connection::open_in_memory().unwrap();
        init_db(&conn).unwrap();

        // Insert a note node with metadata
        insert_note(&conn, 1, "test-note", Some(r#"{"path":"/tmp/test.md"}"#));

        // Insert two chunks with embeddings
        let emb1 = vector_to_blob(&[0.1_f32, 0.2_f32, 0.3_f32]);
        let emb2 = vector_to_blob(&[0.4_f32, 0.5_f32, 0.6_f32]);
        crate::db::chunks::insert_chunk(
            &conn,
            1,
            "Header 1",
            "This is the first chunk text content",
            "header-1",
            Some(&emb1),
            None,
        )
        .unwrap();
        crate::db::chunks::insert_chunk(
            &conn,
            1,
            "Header 2",
            "This is the second chunk with more text content",
            "header-2",
            Some(&emb2),
            None,
        )
        .unwrap();

        conn
    }

    #[test]
    fn test_load_all_chunks_populates_vectors() {
        let conn = setup_db_with_chunks();

        // Create HybridSearch with a mock-like approach:
        // We can't easily construct HybridSearch without an Ollama client,
        // so we test load_all_chunks indirectly via the public API.
        // Instead, test the data insertion directly.
        let data = crate::db::chunks::load_all_chunk_embeddings(&conn).unwrap();
        assert_eq!(data.len(), 2, "should load 2 chunks");

        let (id1, note_id1, emb1, header1, text1) = &data[0];
        assert_eq!(*note_id1, 1);
        assert_eq!(emb1.len(), 3);
        assert_eq!(header1, "Header 1");
        assert_eq!(text1, "This is the first chunk text content");
        assert!(*id1 > 0);

        let (id2, note_id2, emb2, header2, text2) = &data[1];
        assert_eq!(*note_id2, 1);
        assert_eq!(emb2.len(), 3);
        assert_eq!(header2, "Header 2");
        assert_eq!(text2, "This is the second chunk with more text content");
        assert!(*id2 > *id1, "second chunk should have a larger id");
    }

    #[test]
    fn test_load_all_chunks_empty_db() {
        let conn = rusqlite::Connection::open_in_memory().unwrap();
        init_db(&conn).unwrap();

        let data = crate::db::chunks::load_all_chunk_embeddings(&conn).unwrap();
        assert!(data.is_empty(), "empty db should return no chunks");
    }

    #[test]
    fn test_load_all_chunks_skips_null_embeddings() {
        let conn = setup_db_with_chunks();

        // Add a chunk without embedding
        crate::db::chunks::insert_chunk(
            &conn,
            1,
            "No Embed",
            "This chunk has no embedding",
            "no-embed",
            None,
            None,
        )
        .unwrap();

        let data = crate::db::chunks::load_all_chunk_embeddings(&conn).unwrap();
        // All three chunks are returned, but the third has empty vec
        assert_eq!(data.len(), 3);
        assert!(data[2].2.is_empty(), "third chunk should have empty vec");
    }

    #[test]
    fn test_get_note_label() {
        let conn = rusqlite::Connection::open_in_memory().unwrap();
        init_db(&conn).unwrap();

        conn.execute(
            "INSERT INTO nodes (id, key, label, type) VALUES (?1, ?2, ?3, 'note')",
            rusqlite::params![42i64, crate::db::keys::note_key("My Note"), "My Note"],
        )
        .unwrap();

        // We can test via a direct query (HybridSearch::get_note_label is private)
        let label: String = conn
            .query_row(
                "SELECT label FROM nodes WHERE id = ?1",
                rusqlite::params![42i64],
                |row| row.get(0),
            )
            .unwrap();
        assert_eq!(label, "My Note");
    }

    #[test]
    fn test_clip_text_short() {
        assert_eq!(clip_text("hello", 10), "hello");
    }

    #[test]
    fn test_clip_text_long() {
        let long = "a".repeat(300);
        let clipped = clip_text(&long, 200);
        assert_eq!(clipped.len(), 203); // 200 + "..."
        assert!(clipped.ends_with("..."));
    }

    #[test]
    fn test_clip_text_unicode() {
        let long = "í".repeat(300); // 'í' is 2 bytes in UTF-8
        let clipped = clip_text(&long, 200);
        assert_eq!(clipped.chars().count(), 203); // 200 chars + "..."
        assert!(clipped.ends_with("..."));
        // Verify it doesn't panic at byte boundaries
        let _ = clip_text(&"aéíóuñ".repeat(50), 200);
    }

    #[test]
    fn test_search_result_chunk_fields() {
        let r = SearchResult {
            id: 1,
            label: "test".into(),
            r#type: "note".into(),
            score: 0.9,
            content: "preview text".into(),
            file: "/path/file.md".into(),
            neighbors: vec![],
            chunk_header: "Introduction".into(),
            chunk_text: "Full chunk text here".into(),
        };
        assert_eq!(r.chunk_header, "Introduction");
        assert_eq!(r.chunk_text, "Full chunk text here");
        assert_eq!(r.content, "preview text");
    }

    #[test]
    fn test_get_note_label_with_filters_matching() {
        let conn = rusqlite::Connection::open_in_memory().unwrap();
        init_db(&conn).unwrap();
        insert_note(
            &conn,
            42,
            "My Note",
            Some(r#"{"date": 2023, "category": "tutorial"}"#),
        );

        let ollama = crate::embed::ollama::OllamaClient::new("http://localhost:11434", "test");
        let hs = HybridSearch::new_internal(conn, ollama).unwrap();

        let filters = vec![Filter {
            field: "category".into(),
            operator: "=".into(),
            value: "tutorial".into(),
        }];
        let result = hs.get_note_label_with_filters(42, &filters).unwrap();
        assert_eq!(result, Some("My Note".to_string()));
    }

    #[test]
    fn test_get_note_label_with_filters_non_matching() {
        let conn = rusqlite::Connection::open_in_memory().unwrap();
        init_db(&conn).unwrap();
        insert_note(
            &conn,
            42,
            "My Note",
            Some(r#"{"date": 2021, "category": "guide"}"#),
        );

        let ollama = crate::embed::ollama::OllamaClient::new("http://localhost:11434", "test");
        let hs = HybridSearch::new_internal(conn, ollama).unwrap();

        let filters = vec![Filter {
            field: "date".into(),
            operator: ">=".into(),
            value: "2023".into(),
        }];
        let result = hs.get_note_label_with_filters(42, &filters).unwrap();
        assert_eq!(result, None);
    }

    #[test]
    fn test_get_note_label_with_filters_no_filters() {
        let conn = rusqlite::Connection::open_in_memory().unwrap();
        init_db(&conn).unwrap();
        insert_note(&conn, 42, "My Note", Some(r#"{"date": 2023}"#));

        let ollama = crate::embed::ollama::OllamaClient::new("http://localhost:11434", "test");
        let hs = HybridSearch::new_internal(conn, ollama).unwrap();

        // No filters → should return the label
        let result = hs.get_note_label_with_filters(42, &[]).unwrap();
        assert_eq!(result, Some("My Note".to_string()));
    }

    #[test]
    fn test_get_note_label_with_filters_non_existent_field() {
        let conn = rusqlite::Connection::open_in_memory().unwrap();
        init_db(&conn).unwrap();
        insert_note(&conn, 42, "My Note", Some(r#"{"date": 2023}"#));

        let ollama = crate::embed::ollama::OllamaClient::new("http://localhost:11434", "test");
        let hs = HybridSearch::new_internal(conn, ollama).unwrap();

        // Non-existent field → should not match (null comparison)
        let filters = vec![Filter {
            field: "nonexistent".into(),
            operator: "=".into(),
            value: "value".into(),
        }];
        let result = hs.get_note_label_with_filters(42, &filters).unwrap();
        assert_eq!(result, None);
    }

    #[test]
    fn test_get_note_label_with_filters_multiple_and() {
        let conn = rusqlite::Connection::open_in_memory().unwrap();
        init_db(&conn).unwrap();
        insert_note(
            &conn,
            42,
            "Tutorial Note",
            Some(r#"{"date": 2023, "category": "tutorial", "status": "published"}"#),
        );

        let ollama = crate::embed::ollama::OllamaClient::new("http://localhost:11434", "test");
        let hs = HybridSearch::new_internal(conn, ollama).unwrap();

        // Both filters must match
        let filters = vec![
            Filter {
                field: "category".into(),
                operator: "=".into(),
                value: "tutorial".into(),
            },
            Filter {
                field: "date".into(),
                operator: ">=".into(),
                value: "2023".into(),
            },
        ];
        let result = hs.get_note_label_with_filters(42, &filters).unwrap();
        assert_eq!(result, Some("Tutorial Note".to_string()));
    }

    #[test]
    fn test_get_note_label_with_filters_multiple_and_one_fails() {
        let conn = rusqlite::Connection::open_in_memory().unwrap();
        init_db(&conn).unwrap();
        insert_note(
            &conn,
            42,
            "Old Guide",
            Some(r#"{"date": 2021, "category": "guide", "status": "archived"}"#),
        );

        let ollama = crate::embed::ollama::OllamaClient::new("http://localhost:11434", "test");
        let hs = HybridSearch::new_internal(conn, ollama).unwrap();

        // First filter matches, second fails → should return None
        let filters = vec![
            Filter {
                field: "category".into(),
                operator: "=".into(),
                value: "guide".into(),
            },
            Filter {
                field: "date".into(),
                operator: ">=".into(),
                value: "2023".into(),
            },
        ];
        let result = hs.get_note_label_with_filters(42, &filters).unwrap();
        assert_eq!(result, None);
    }

    #[test]
    fn test_get_note_label_with_filters_note_not_found() {
        let conn = rusqlite::Connection::open_in_memory().unwrap();
        init_db(&conn).unwrap();
        // No nodes inserted

        let ollama = crate::embed::ollama::OllamaClient::new("http://localhost:11434", "test");
        let hs = HybridSearch::new_internal(conn, ollama).unwrap();

        let filters = vec![Filter {
            field: "date".into(),
            operator: ">=".into(),
            value: "2023".into(),
        }];
        let result = hs.get_note_label_with_filters(999, &filters).unwrap();
        assert_eq!(result, None);
    }

    #[test]
    #[ignore = "needs Ollama"]
    fn test_search_empty_chunks() {
        let conn = rusqlite::Connection::open_in_memory().unwrap();
        init_db(&conn).unwrap();

        // Insert a note node (so FTS expansion doesn't crash) but NO chunks.
        insert_note(&conn, 1, "test-note", Some(r#"{"path":"/tmp/test.md"}"#));

        let ollama = crate::embed::ollama::OllamaClient::new("http://localhost:11434", "test");
        let mut hs = HybridSearch::new_internal(conn, ollama).unwrap();

        let results = hs
            .hybrid_search("test query", 5, 2, 0.7, None, false, &[])
            .unwrap();
        assert!(
            results.is_empty(),
            "expected no results when chunks table is empty, got {}",
            results.len()
        );
    }

    #[test]
    #[ignore = "needs Ollama"]
    fn test_search_ollama_unreachable() {
        let conn = rusqlite::Connection::open_in_memory().unwrap();
        init_db(&conn).unwrap();

        // Point at a port where nothing is listening → connection refused.
        let ollama = crate::embed::ollama::OllamaClient::new("http://127.0.0.1:1", "test-model");
        let mut hs = HybridSearch::new_internal(conn, ollama).unwrap();

        let result = hs.hybrid_search("query", 5, 2, 0.7, None, false, &[]);
        assert!(result.is_err(), "expected error when Ollama is unreachable");
        let err = result.unwrap_err().to_string();
        assert!(
            err.contains("Error connecting to Ollama")
                || err.contains("Connection refused")
                || err.contains("error trying to connect"),
            "unexpected error: {}",
            err
        );
    }

    #[test]
    fn test_similar_by_label_returns_results() {
        let conn = setup_db_with_chunks();

        // Añadir una segunda nota con chunks para que haya algo con qué comparar
        insert_note(
            &conn,
            2,
            "second-note",
            Some(r#"{"path":"/tmp/second.md"}"#),
        );

        // Chunk con embedding similar al chunk 1 de test-note (más cercano a [0.1,0.2,0.3])
        let emb_similar = crate::vector::vector_to_blob(&[0.11_f32, 0.21_f32, 0.31_f32]);
        crate::db::chunks::insert_chunk(
            &conn,
            2,
            "Similar Header",
            "similar text content",
            "similar",
            Some(&emb_similar),
            None,
        )
        .unwrap();

        let ollama = crate::embed::ollama::OllamaClient::new("http://localhost:11434", "test");
        let mut hs = HybridSearch::new_internal(conn, ollama).unwrap();

        let results = hs
            .similar_by_label("test-note", 5, 0, None, false, &[])
            .unwrap();
        assert!(!results.is_empty(), "should return at least one result");
        assert!(
            results.iter().any(|r| r.label == "second-note"),
            "second-note should be among results"
        );
    }

    #[test]
    fn test_similar_by_label_not_found() {
        let conn = rusqlite::Connection::open_in_memory().unwrap();
        crate::db::schema::init_db(&conn).unwrap();

        let ollama = crate::embed::ollama::OllamaClient::new("http://localhost:11434", "test");
        let mut hs = HybridSearch::new_internal(conn, ollama).unwrap();

        let result = hs.similar_by_label("NonExistent", 5, 0, None, false, &[]);
        assert!(result.is_err(), "should error for non-existent label");
        let err = result.unwrap_err().to_string();
        assert!(
            err.contains("no encontrado") || err.contains("NonExistent"),
            "error should mention the label: {}",
            err
        );
    }

    #[test]
    fn test_similar_by_file_not_found() {
        let conn = rusqlite::Connection::open_in_memory().unwrap();
        crate::db::schema::init_db(&conn).unwrap();

        let ollama = crate::embed::ollama::OllamaClient::new("http://localhost:11434", "test");
        let mut hs = HybridSearch::new_internal(conn, ollama).unwrap();

        let result = hs.similar_by_file("/tmp/nonexistent_file_xyz.md", 5, 0, None, false, &[]);
        assert!(result.is_err(), "should error for non-existent file");
        let err = result.unwrap_err().to_string();
        assert!(
            err.contains("no encontrado") || err.contains("NotFound"),
            "error should mention file not found: {}",
            err
        );
    }

    #[test]
    fn test_graph_only_resolution_is_deterministic_prefers_key() {
        let conn = rusqlite::Connection::open_in_memory().unwrap();
        init_db(&conn).unwrap();

        // Dos notas con el MISMO `label` ("Dup") pero distinta `key`.
        conn.execute(
            "INSERT INTO nodes (id, key, label, type) VALUES (?1, ?2, 'Dup', 'note')",
            rusqlite::params![1i64, crate::db::keys::note_key("a/dup.md")],
        )
        .unwrap();
        conn.execute(
            "INSERT INTO nodes (id, key, label, type) VALUES (?1, ?2, 'Dup', 'note')",
            rusqlite::params![2i64, crate::db::keys::note_key("b/dup.md")],
        )
        .unwrap();

        // Entidad enlazada SOLO con la nota 2.
        conn.execute(
            "INSERT INTO nodes (id, key, label, type) VALUES (?1, ?2, 'Entidad', 'concept')",
            rusqlite::params![3i64, crate::db::keys::node_key("Entidad")],
        )
        .unwrap();
        conn.execute(
            "INSERT INTO edges (source_id, target_id, type, weight) VALUES (2, 3, 'mentions', 1.0)",
            [],
        )
        .unwrap();

        let ollama = crate::embed::ollama::OllamaClient::new("http://localhost:11434", "test");
        let hs = HybridSearch::new_internal(conn, ollama).unwrap();

        // Resolver por la key exacta de la nota 2 → debe elegir la nota 2,
        // expandiendo hasta su vecino 'Entidad'.
        let by_key = hs
            .graph_only(&crate::db::keys::note_key("b/dup.md"), 1)
            .unwrap();
        assert_eq!(by_key.len(), 1, "key exacta debe resolver la nota 2");
        assert_eq!(by_key[0].label, "Entidad");

        // Resolver por el label ambiguo no debe fallar y es determinista:
        // gana la fila de menor `id` (la nota 1, sin aristas).
        let by_label = hs.graph_only("Dup", 1).unwrap();
        assert!(
            by_label.is_empty(),
            "label ambiguo → se resuelve al menor id (sin vecinos)"
        );
    }
}