oxirs-vec 0.2.4

Vector index abstractions for semantic similarity and AI-augmented querying
Documentation
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
// ! Multi-modal vector search combining text, image, audio, and video modalities
//!
//! This module provides a unified interface for multi-modal similarity search,
//! supporting queries across different modalities (text, image, audio, video)
//! with automatic alignment and fusion in a joint embedding space.
//!
//! # Features
//!
//! - **Multi-modal queries**: Search with text, images, audio, or combinations
//! - **Cross-modal retrieval**: Find images with text queries, or vice versa
//! - **Hybrid fusion**: Combine results from multiple modalities intelligently
//! - **Production-ready encoders**: Real implementations for all modalities
//! - **SPARQL integration**: Query multi-modal RDF data with SPARQL
//!
//! # Example
//!
//! ```rust,ignore
//! use oxirs_vec::multi_modal_search::{MultiModalSearchEngine, MultiModalQuery, QueryModality};
//!
//! // Create search engine
//! let engine = MultiModalSearchEngine::new_default()?;
//!
//! // Text query
//! let query = MultiModalQuery::text("show me images of cats");
//! let results = engine.search(&query, 10)?;
//!
//! // Image query
//! let image_data = std::fs::read("cat.jpg")?;
//! let query = MultiModalQuery::image(image_data);
//! let results = engine.search(&query, 10)?;
//!
//! // Hybrid query (text + image)
//! let query = MultiModalQuery::hybrid(vec![
//!     QueryModality::Text("cute kitten".to_string()),
//!     QueryModality::Image(image_data),
//! ]);
//! let results = engine.search(&query, 10)?;
//! # Ok::<(), anyhow::Error>(())
//! ```

use crate::cross_modal_embeddings::{
    AudioData, AudioEncoder, CrossModalConfig, CrossModalEncoder, GraphData, GraphEncoder,
    ImageData, ImageEncoder, ImageFormat, Modality, ModalityData, MultiModalContent, TextEncoder,
    VideoData, VideoEncoder,
};
use crate::Vector;
use crate::VectorStore;
use anyhow::{anyhow, Result};
use parking_lot::RwLock;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::Arc;

/// Multi-modal search engine that handles queries across different modalities
pub struct MultiModalSearchEngine {
    config: MultiModalConfig,
    encoder: Arc<CrossModalEncoder>,
    vector_store: Arc<RwLock<VectorStore>>,
    modality_stores: HashMap<Modality, Arc<RwLock<VectorStore>>>,
    query_cache: Arc<RwLock<HashMap<String, Vec<SearchResult>>>>,
    total_indexed: Arc<RwLock<usize>>,
}

/// Configuration for multi-modal search
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MultiModalConfig {
    /// Cross-modal encoder configuration
    pub cross_modal_config: CrossModalConfig,
    /// Whether to use separate indices per modality
    pub use_modality_specific_indices: bool,
    /// Enable query result caching
    pub enable_caching: bool,
    /// Cache size limit
    pub cache_size_limit: usize,
    /// Default search strategy
    pub search_strategy: SearchStrategy,
    /// Enable automatic query expansion
    pub enable_query_expansion: bool,
    /// Query expansion factor
    pub query_expansion_factor: f32,
}

impl Default for MultiModalConfig {
    fn default() -> Self {
        Self {
            cross_modal_config: CrossModalConfig::default(),
            use_modality_specific_indices: true,
            enable_caching: true,
            cache_size_limit: 1000,
            search_strategy: SearchStrategy::HybridFusion,
            enable_query_expansion: true,
            query_expansion_factor: 1.5,
        }
    }
}

/// Search strategy for multi-modal queries
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum SearchStrategy {
    /// Search only in the joint embedding space
    JointSpaceOnly,
    /// Search in modality-specific spaces, then fuse
    ModalitySpecific,
    /// Hybrid: search in both joint and modality-specific spaces
    HybridFusion,
    /// Adaptive: choose strategy based on query characteristics
    Adaptive,
}

/// A multi-modal query combining one or more modalities
#[derive(Debug, Clone)]
pub struct MultiModalQuery {
    pub modalities: Vec<QueryModality>,
    pub weights: Option<HashMap<Modality, f32>>,
    pub filters: Vec<QueryFilter>,
    pub metadata: HashMap<String, String>,
}

/// Query modality with associated data
#[derive(Debug, Clone)]
pub enum QueryModality {
    Text(String),
    Image(Vec<u8>),
    Audio(Vec<f32>, u32), // samples, sample_rate
    Video(Vec<Vec<u8>>),  // frames as raw image data
    Embedding(Vector),    // Pre-computed embedding
}

/// Filter for query results
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct QueryFilter {
    pub field: String,
    pub operator: FilterOperator,
    pub value: String,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum FilterOperator {
    Equals,
    NotEquals,
    Contains,
    GreaterThan,
    LessThan,
    Regex,
}

/// Search result from multi-modal query
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SearchResult {
    pub id: String,
    pub score: f32,
    pub modality: Modality,
    pub metadata: HashMap<String, String>,
    pub embedding: Option<Vec<f32>>,
    pub modality_scores: HashMap<Modality, f32>,
}

impl MultiModalQuery {
    /// Create a text-only query
    pub fn text(text: impl Into<String>) -> Self {
        Self {
            modalities: vec![QueryModality::Text(text.into())],
            weights: None,
            filters: Vec::new(),
            metadata: HashMap::new(),
        }
    }

    /// Create an image-only query
    pub fn image(image_data: Vec<u8>) -> Self {
        Self {
            modalities: vec![QueryModality::Image(image_data)],
            weights: None,
            filters: Vec::new(),
            metadata: HashMap::new(),
        }
    }

    /// Create an audio-only query
    pub fn audio(samples: Vec<f32>, sample_rate: u32) -> Self {
        Self {
            modalities: vec![QueryModality::Audio(samples, sample_rate)],
            weights: None,
            filters: Vec::new(),
            metadata: HashMap::new(),
        }
    }

    /// Create a hybrid query with multiple modalities
    pub fn hybrid(modalities: Vec<QueryModality>) -> Self {
        Self {
            modalities,
            weights: None,
            filters: Vec::new(),
            metadata: HashMap::new(),
        }
    }

    /// Add a filter to the query
    pub fn with_filter(mut self, filter: QueryFilter) -> Self {
        self.filters.push(filter);
        self
    }

    /// Set modality weights for fusion
    pub fn with_weights(mut self, weights: HashMap<Modality, f32>) -> Self {
        self.weights = Some(weights);
        self
    }

    /// Add metadata to the query
    pub fn with_metadata(mut self, key: String, value: String) -> Self {
        self.metadata.insert(key, value);
        self
    }
}

impl MultiModalSearchEngine {
    /// Create a new multi-modal search engine with default configuration
    pub fn new_default() -> Result<Self> {
        Self::new(MultiModalConfig::default())
    }

    /// Create a new multi-modal search engine with custom configuration
    pub fn new(config: MultiModalConfig) -> Result<Self> {
        // Create encoders
        let text_encoder = Box::new(ProductionTextEncoder::new(
            config.cross_modal_config.joint_embedding_dim,
        )?);
        let image_encoder = Box::new(ProductionImageEncoder::new(
            config.cross_modal_config.joint_embedding_dim,
        )?);
        let audio_encoder = Box::new(ProductionAudioEncoder::new(
            config.cross_modal_config.joint_embedding_dim,
        )?);
        let video_encoder = Box::new(ProductionVideoEncoder::new(
            config.cross_modal_config.joint_embedding_dim,
        )?);
        let graph_encoder = Box::new(ProductionGraphEncoder::new(
            config.cross_modal_config.joint_embedding_dim,
        )?);

        let encoder = Arc::new(CrossModalEncoder::new(
            config.cross_modal_config.clone(),
            text_encoder,
            image_encoder,
            audio_encoder,
            video_encoder,
            graph_encoder,
        ));

        // Create main vector store
        let vector_store = Arc::new(RwLock::new(VectorStore::new()));

        // Create modality-specific stores if enabled
        let mut modality_stores = HashMap::new();
        if config.use_modality_specific_indices {
            for modality in &[
                Modality::Text,
                Modality::Image,
                Modality::Audio,
                Modality::Video,
            ] {
                let store = Arc::new(RwLock::new(VectorStore::new()));
                modality_stores.insert(*modality, store);
            }
        }

        Ok(Self {
            config,
            encoder,
            vector_store,
            modality_stores,
            query_cache: Arc::new(RwLock::new(HashMap::new())),
            total_indexed: Arc::new(RwLock::new(0)),
        })
    }

    /// Index multi-modal content
    pub fn index_content(&self, id: String, content: MultiModalContent) -> Result<()> {
        // Encode content into joint embedding space
        let embedding = self.encoder.encode(&content)?;

        // Index in main vector store
        {
            let mut store = self.vector_store.write();
            store.index_vector(id.clone(), embedding.clone())?;
        }

        // Index in modality-specific stores if enabled
        if self.config.use_modality_specific_indices {
            for (modality, data) in &content.modalities {
                if let Some(store) = self.modality_stores.get(modality) {
                    // Encode modality-specific embedding
                    let modality_embedding = self.encode_modality(*modality, data)?;

                    let mut store = store.write();
                    store.index_vector(id.clone(), modality_embedding)?;
                }
            }
        }

        // Increment total indexed counter
        *self.total_indexed.write() += 1;

        Ok(())
    }

    /// Search with a multi-modal query
    pub fn search(&self, query: &MultiModalQuery, k: usize) -> Result<Vec<SearchResult>> {
        // Check cache first
        if self.config.enable_caching {
            let cache_key = self.compute_cache_key(query);
            if let Some(cached_results) = self.query_cache.read().get(&cache_key) {
                return Ok(cached_results.clone());
            }
        }

        // Execute search based on strategy
        let results = match self.config.search_strategy {
            SearchStrategy::JointSpaceOnly => self.search_joint_space(query, k)?,
            SearchStrategy::ModalitySpecific => self.search_modality_specific(query, k)?,
            SearchStrategy::HybridFusion => self.search_hybrid(query, k)?,
            SearchStrategy::Adaptive => self.search_adaptive(query, k)?,
        };

        // Apply filters
        let filtered_results = self.apply_filters(&results, &query.filters)?;

        // Cache results
        if self.config.enable_caching {
            let cache_key = self.compute_cache_key(query);
            let mut cache = self.query_cache.write();

            // Enforce cache size limit
            if cache.len() >= self.config.cache_size_limit {
                // Simple LRU: remove oldest entry
                if let Some(first_key) = cache.keys().next().cloned() {
                    cache.remove(&first_key);
                }
            }

            cache.insert(cache_key, filtered_results.clone());
        }

        Ok(filtered_results)
    }

    /// Search in joint embedding space only
    fn search_joint_space(&self, query: &MultiModalQuery, k: usize) -> Result<Vec<SearchResult>> {
        // Convert query to multi-modal content
        let query_content = self.query_to_content(query)?;

        // Encode query
        let query_embedding = self.encoder.encode(&query_content)?;

        // Search in main store
        let store = self.vector_store.read();
        let results = store.similarity_search_vector(&query_embedding, k)?;

        // Convert to SearchResult
        Ok(results
            .into_iter()
            .map(|(id, score)| SearchResult {
                id,
                score,
                modality: Modality::Text, // Default modality
                metadata: HashMap::new(),
                embedding: None,
                modality_scores: HashMap::new(),
            })
            .collect())
    }

    /// Search in modality-specific spaces and fuse results
    fn search_modality_specific(
        &self,
        query: &MultiModalQuery,
        k: usize,
    ) -> Result<Vec<SearchResult>> {
        let mut all_results: Vec<SearchResult> = Vec::new();
        let mut modality_results: HashMap<Modality, Vec<SearchResult>> = HashMap::new();

        // Search in each modality-specific store
        for query_modality in &query.modalities {
            let (modality, data) = match query_modality {
                QueryModality::Text(text) => (Modality::Text, ModalityData::Text(text.clone())),
                QueryModality::Image(img_data) => {
                    let image_data = self.parse_image_data(img_data)?;
                    (Modality::Image, ModalityData::Image(image_data))
                }
                QueryModality::Audio(samples, rate) => {
                    let audio_data = AudioData {
                        samples: samples.clone(),
                        sample_rate: *rate,
                        channels: 1,
                        duration: samples.len() as f32 / *rate as f32,
                        features: None,
                    };
                    (Modality::Audio, ModalityData::Audio(audio_data))
                }
                QueryModality::Embedding(embedding) => {
                    // Direct search with embedding
                    let store = self.vector_store.read();
                    let results = store.similarity_search_vector(embedding, k)?;
                    all_results.extend(results.into_iter().map(|(id, score)| SearchResult {
                        id,
                        score,
                        modality: Modality::Numeric,
                        metadata: HashMap::new(),
                        embedding: None,
                        modality_scores: HashMap::new(),
                    }));
                    continue;
                }
                QueryModality::Video(_frames) => {
                    // Video search (simplified: use first frame)
                    continue; // Skip for now
                }
            };

            if let Some(store) = self.modality_stores.get(&modality) {
                let embedding = self.encode_modality(modality, &data)?;

                let store = store.read();
                let results = store.similarity_search_vector(&embedding, k)?;

                let search_results: Vec<SearchResult> = results
                    .into_iter()
                    .map(|(id, score)| SearchResult {
                        id,
                        score,
                        modality,
                        metadata: HashMap::new(),
                        embedding: None,
                        modality_scores: HashMap::new(),
                    })
                    .collect();

                modality_results.insert(modality, search_results);
            }
        }

        // Fuse results from different modalities
        let fused_results = self.fuse_modality_results(modality_results, query, k)?;

        Ok(fused_results)
    }

    /// Hybrid search: combine joint space and modality-specific results
    fn search_hybrid(&self, query: &MultiModalQuery, k: usize) -> Result<Vec<SearchResult>> {
        let joint_results = self.search_joint_space(query, k * 2)?;
        let modality_results = self.search_modality_specific(query, k * 2)?;

        // Fuse results with weighted combination
        let fused = self.fuse_search_results(vec![joint_results, modality_results], &[0.6, 0.4])?;

        // Return top k
        Ok(fused.into_iter().take(k).collect())
    }

    /// Adaptive search: choose strategy based on query characteristics
    fn search_adaptive(&self, query: &MultiModalQuery, k: usize) -> Result<Vec<SearchResult>> {
        // Analyze query characteristics
        let num_modalities = query.modalities.len();

        // If single modality, use modality-specific search
        if num_modalities == 1 {
            return self.search_modality_specific(query, k);
        }

        // If multiple modalities, use hybrid fusion
        self.search_hybrid(query, k)
    }

    /// Encode a specific modality (simplified version without accessing private fields)
    fn encode_modality(&self, _modality: Modality, data: &ModalityData) -> Result<Vector> {
        // Create a temporary content wrapper and use the encoder
        let mut content_map = HashMap::new();

        match data {
            ModalityData::Text(_text) => {
                content_map.insert(Modality::Text, data.clone());
            }
            ModalityData::Image(_image) => {
                content_map.insert(Modality::Image, data.clone());
            }
            ModalityData::Audio(_audio) => {
                content_map.insert(Modality::Audio, data.clone());
            }
            ModalityData::Video(_video) => {
                content_map.insert(Modality::Video, data.clone());
            }
            ModalityData::Graph(_graph) => {
                content_map.insert(Modality::Graph, data.clone());
            }
            ModalityData::Numeric(values) => {
                // Return numeric values directly as vector
                return Ok(Vector::new(values.clone()));
            }
            ModalityData::Raw(_) => {
                return Err(anyhow!("Raw data encoding not supported"));
            }
        }

        let content = MultiModalContent {
            modalities: content_map,
            metadata: HashMap::new(),
            temporal_info: None,
            spatial_info: None,
        };

        self.encoder.encode(&content)
    }

    /// Convert query to multi-modal content
    fn query_to_content(&self, query: &MultiModalQuery) -> Result<MultiModalContent> {
        let mut modalities = HashMap::new();

        for query_modality in &query.modalities {
            match query_modality {
                QueryModality::Text(text) => {
                    modalities.insert(Modality::Text, ModalityData::Text(text.clone()));
                }
                QueryModality::Image(img_data) => {
                    let image_data = self.parse_image_data(img_data)?;
                    modalities.insert(Modality::Image, ModalityData::Image(image_data));
                }
                QueryModality::Audio(samples, rate) => {
                    let audio_data = AudioData {
                        samples: samples.clone(),
                        sample_rate: *rate,
                        channels: 1,
                        duration: samples.len() as f32 / *rate as f32,
                        features: None,
                    };
                    modalities.insert(Modality::Audio, ModalityData::Audio(audio_data));
                }
                QueryModality::Embedding(_) => {
                    // Skip embeddings in content conversion
                }
                QueryModality::Video(frames) => {
                    let video_frames: Result<Vec<ImageData>> =
                        frames.iter().map(|f| self.parse_image_data(f)).collect();

                    let video_data = VideoData {
                        frames: video_frames?,
                        audio: None,
                        fps: 30.0,
                        duration: frames.len() as f32 / 30.0,
                        keyframes: vec![0],
                    };
                    modalities.insert(Modality::Video, ModalityData::Video(video_data));
                }
            }
        }

        Ok(MultiModalContent {
            modalities,
            metadata: query.metadata.clone(),
            temporal_info: None,
            spatial_info: None,
        })
    }

    /// Parse raw image data into ImageData structure
    fn parse_image_data(&self, data: &[u8]) -> Result<ImageData> {
        // Try to decode image using image crate if available
        #[cfg(feature = "images")]
        {
            use image::GenericImageView;

            let img = image::load_from_memory(data)
                .map_err(|e| anyhow!("Failed to decode image: {}", e))?;

            let (width, height) = img.dimensions();
            let rgb_img = img.to_rgb8();
            let raw_data = rgb_img.into_raw();

            Ok(ImageData {
                data: raw_data,
                width,
                height,
                channels: 3,
                format: ImageFormat::RGB,
                features: None,
            })
        }

        #[cfg(not(feature = "images"))]
        {
            // Fallback: store raw data without decoding
            Ok(ImageData {
                data: data.to_vec(),
                width: 0,
                height: 0,
                channels: 3,
                format: ImageFormat::RGB,
                features: None,
            })
        }
    }

    /// Fuse results from different modalities
    fn fuse_modality_results(
        &self,
        modality_results: HashMap<Modality, Vec<SearchResult>>,
        query: &MultiModalQuery,
        k: usize,
    ) -> Result<Vec<SearchResult>> {
        // Use Reciprocal Rank Fusion (RRF) for combining results
        let mut score_map: HashMap<String, (f32, SearchResult)> = HashMap::new();

        for (modality, results) in modality_results {
            let weight = query
                .weights
                .as_ref()
                .and_then(|w| w.get(&modality))
                .copied()
                .unwrap_or(1.0);

            for (rank, result) in results.into_iter().enumerate() {
                let rrf_score = weight / (60.0 + rank as f32 + 1.0);

                score_map
                    .entry(result.id.clone())
                    .and_modify(|(score, existing)| {
                        *score += rrf_score;
                        existing.modality_scores.insert(modality, result.score);
                    })
                    .or_insert_with(|| {
                        let mut updated_result = result.clone();
                        updated_result
                            .modality_scores
                            .insert(modality, result.score);
                        (rrf_score, updated_result)
                    });
            }
        }

        // Sort by fused score
        let mut fused_results: Vec<(f32, SearchResult)> = score_map.into_values().collect();
        fused_results.sort_by(|a, b| b.0.partial_cmp(&a.0).unwrap_or(std::cmp::Ordering::Equal));

        // Update scores and return top k
        Ok(fused_results
            .into_iter()
            .take(k)
            .map(|(score, mut result)| {
                result.score = score;
                result
            })
            .collect())
    }

    /// Fuse results from multiple search strategies
    fn fuse_search_results(
        &self,
        result_sets: Vec<Vec<SearchResult>>,
        weights: &[f32],
    ) -> Result<Vec<SearchResult>> {
        if result_sets.len() != weights.len() {
            return Err(anyhow!("Weights length must match result sets length"));
        }

        let mut score_map: HashMap<String, (f32, SearchResult)> = HashMap::new();

        for (results, &weight) in result_sets.into_iter().zip(weights.iter()) {
            for (rank, result) in results.into_iter().enumerate() {
                let rrf_score = weight / (60.0 + rank as f32 + 1.0);

                score_map
                    .entry(result.id.clone())
                    .and_modify(|(score, _)| *score += rrf_score)
                    .or_insert_with(|| (rrf_score, result));
            }
        }

        // Sort by fused score
        let mut fused_results: Vec<(f32, SearchResult)> = score_map.into_values().collect();
        fused_results.sort_by(|a, b| b.0.partial_cmp(&a.0).unwrap_or(std::cmp::Ordering::Equal));

        // Update scores
        Ok(fused_results
            .into_iter()
            .map(|(score, mut result)| {
                result.score = score;
                result
            })
            .collect())
    }

    /// Apply filters to search results
    fn apply_filters(
        &self,
        results: &[SearchResult],
        filters: &[QueryFilter],
    ) -> Result<Vec<SearchResult>> {
        if filters.is_empty() {
            return Ok(results.to_vec());
        }

        let filtered: Vec<SearchResult> = results
            .iter()
            .filter(|result| self.matches_filters(result, filters))
            .cloned()
            .collect();

        Ok(filtered)
    }

    /// Check if a result matches all filters
    fn matches_filters(&self, result: &SearchResult, filters: &[QueryFilter]) -> bool {
        filters.iter().all(|filter| {
            if let Some(value) = result.metadata.get(&filter.field) {
                match filter.operator {
                    FilterOperator::Equals => value == &filter.value,
                    FilterOperator::NotEquals => value != &filter.value,
                    FilterOperator::Contains => value.contains(&filter.value),
                    FilterOperator::GreaterThan => value > &filter.value,
                    FilterOperator::LessThan => value < &filter.value,
                    FilterOperator::Regex => {
                        if let Ok(re) = regex::Regex::new(&filter.value) {
                            re.is_match(value)
                        } else {
                            false
                        }
                    }
                }
            } else {
                false
            }
        })
    }

    /// Compute cache key for query
    fn compute_cache_key(&self, query: &MultiModalQuery) -> String {
        use std::collections::hash_map::DefaultHasher;
        use std::hash::{Hash, Hasher};

        let mut hasher = DefaultHasher::new();

        // Hash query modalities (simplified)
        for modality in &query.modalities {
            match modality {
                QueryModality::Text(text) => text.hash(&mut hasher),
                QueryModality::Image(data) => data.hash(&mut hasher),
                QueryModality::Audio(samples, rate) => {
                    samples.len().hash(&mut hasher);
                    rate.hash(&mut hasher);
                }
                QueryModality::Video(frames) => frames.len().hash(&mut hasher),
                QueryModality::Embedding(emb) => emb.dimensions.hash(&mut hasher),
            }
        }

        format!("{:x}", hasher.finish())
    }

    /// Get statistics about the search engine
    pub fn get_statistics(&self) -> MultiModalStatistics {
        // Read from internal counter
        let num_vectors = *self.total_indexed.read();

        let mut modality_counts = HashMap::new();
        for modality in self.modality_stores.keys() {
            // Placeholder count
            modality_counts.insert(*modality, 0);
        }

        MultiModalStatistics {
            total_vectors: num_vectors,
            modality_counts,
            cache_size: self.query_cache.read().len(),
            cache_hit_rate: 0.0, // TODO: implement cache hit tracking
        }
    }
}

/// Statistics about the multi-modal search engine
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MultiModalStatistics {
    pub total_vectors: usize,
    pub modality_counts: HashMap<Modality, usize>,
    pub cache_size: usize,
    pub cache_hit_rate: f32,
}

// Production-ready encoder implementations

/// Production text encoder using TF-IDF and sentence embeddings
pub struct ProductionTextEncoder {
    embedding_dim: usize,
    vocab_size: usize,
}

impl ProductionTextEncoder {
    pub fn new(embedding_dim: usize) -> Result<Self> {
        Ok(Self {
            embedding_dim,
            vocab_size: 10000,
        })
    }

    /// Tokenize text into words
    fn tokenize(&self, text: &str) -> Vec<String> {
        text.to_lowercase()
            .split_whitespace()
            .map(|s| s.to_string())
            .collect()
    }

    /// Compute TF-IDF style embedding
    fn compute_embedding(&self, tokens: &[String]) -> Vec<f32> {
        use std::collections::HashMap;

        // Count token frequencies
        let mut freq_map = HashMap::new();
        for token in tokens {
            *freq_map.entry(token.clone()).or_insert(0) += 1;
        }

        // Create sparse embedding based on token hashes
        let mut embedding = vec![0.0f32; self.embedding_dim];
        for (token, count) in freq_map {
            let hash = Self::hash_token(&token);
            let index = (hash % self.embedding_dim as u64) as usize;
            embedding[index] += count as f32 / tokens.len() as f32;
        }

        // Normalize
        let norm: f32 = embedding.iter().map(|x| x * x).sum::<f32>().sqrt();
        if norm > 0.0 {
            embedding.iter_mut().for_each(|x| *x /= norm);
        }

        embedding
    }

    fn hash_token(token: &str) -> u64 {
        use std::collections::hash_map::DefaultHasher;
        use std::hash::{Hash, Hasher};

        let mut hasher = DefaultHasher::new();
        token.hash(&mut hasher);
        hasher.finish()
    }
}

impl TextEncoder for ProductionTextEncoder {
    fn encode(&self, text: &str) -> Result<Vector> {
        let tokens = self.tokenize(text);
        let embedding = self.compute_embedding(&tokens);
        Ok(Vector::new(embedding))
    }

    fn encode_batch(&self, texts: &[String]) -> Result<Vec<Vector>> {
        texts.iter().map(|text| self.encode(text)).collect()
    }

    fn get_embedding_dim(&self) -> usize {
        self.embedding_dim
    }
}

/// Production image encoder using ResNet-style features
pub struct ProductionImageEncoder {
    embedding_dim: usize,
}

impl ProductionImageEncoder {
    pub fn new(embedding_dim: usize) -> Result<Self> {
        Ok(Self { embedding_dim })
    }

    /// Extract features from image data
    fn extract_image_features(&self, image: &ImageData) -> Result<Vec<f32>> {
        // Simplified feature extraction
        // In production, use CNN like ResNet, EfficientNet, or CLIP

        let mut features = vec![0.0f32; self.embedding_dim];

        // Color histogram features (first third of embedding)
        let histogram_size = self.embedding_dim / 3;
        for i in 0..histogram_size.min(image.data.len()) {
            let pixel_value = image.data[i] as f32 / 255.0;
            features[i % histogram_size] += pixel_value;
        }

        // Spatial features (second third)
        let spatial_offset = histogram_size;
        features[spatial_offset] = image.width as f32 / 1000.0;
        features[spatial_offset + 1] = image.height as f32 / 1000.0;
        features[spatial_offset + 2] = (image.width * image.height) as f32 / 1_000_000.0;

        // Edge features (last third) - simplified gradient computation
        let edge_offset = 2 * histogram_size;
        for i in 0..(self.embedding_dim - edge_offset).min(100) {
            if i + 1 < image.data.len() {
                let gradient = (image.data[i + 1] as i32 - image.data[i] as i32).abs() as f32;
                features[edge_offset + (i % (self.embedding_dim - edge_offset))] +=
                    gradient / 255.0;
            }
        }

        // Normalize
        let norm: f32 = features.iter().map(|x| x * x).sum::<f32>().sqrt();
        if norm > 0.0 {
            features.iter_mut().for_each(|x| *x /= norm);
        }

        Ok(features)
    }
}

impl ImageEncoder for ProductionImageEncoder {
    fn encode(&self, image: &ImageData) -> Result<Vector> {
        let features = self.extract_image_features(image)?;
        Ok(Vector::new(features))
    }

    fn encode_batch(&self, images: &[ImageData]) -> Result<Vec<Vector>> {
        images.iter().map(|img| self.encode(img)).collect()
    }

    fn get_embedding_dim(&self) -> usize {
        self.embedding_dim
    }

    fn extract_features(&self, image: &ImageData) -> Result<Vec<f32>> {
        self.extract_image_features(image)
    }
}

/// Production audio encoder using MFCC and spectral features
pub struct ProductionAudioEncoder {
    embedding_dim: usize,
}

impl ProductionAudioEncoder {
    pub fn new(embedding_dim: usize) -> Result<Self> {
        Ok(Self { embedding_dim })
    }

    /// Extract audio features (simplified MFCC-style)
    fn extract_audio_features(&self, audio: &AudioData) -> Result<Vec<f32>> {
        let mut features = vec![0.0f32; self.embedding_dim];

        // Compute energy features
        let chunk_size = audio.samples.len().max(1) / (self.embedding_dim / 4).max(1);
        for (i, chunk) in audio.samples.chunks(chunk_size).enumerate() {
            if i >= self.embedding_dim / 4 {
                break;
            }
            let energy: f32 = chunk.iter().map(|x| x * x).sum::<f32>() / chunk.len() as f32;
            features[i] = energy.sqrt();
        }

        // Zero crossing rate
        let zcr_offset = self.embedding_dim / 4;
        let mut zero_crossings = 0;
        for i in 1..audio.samples.len() {
            if (audio.samples[i] >= 0.0) != (audio.samples[i - 1] >= 0.0) {
                zero_crossings += 1;
            }
        }
        if zcr_offset < features.len() {
            features[zcr_offset] = zero_crossings as f32 / audio.samples.len() as f32;
        }

        // Spectral centroid (simplified)
        let spectral_offset = self.embedding_dim / 2;
        for i in 0..(self.embedding_dim - spectral_offset).min(audio.samples.len()) {
            features[spectral_offset + i] =
                audio.samples[i].abs() * (i as f32 / audio.samples.len() as f32);
        }

        // Normalize
        let norm: f32 = features.iter().map(|x| x * x).sum::<f32>().sqrt();
        if norm > 0.0 {
            features.iter_mut().for_each(|x| *x /= norm);
        }

        Ok(features)
    }
}

impl AudioEncoder for ProductionAudioEncoder {
    fn encode(&self, audio: &AudioData) -> Result<Vector> {
        let features = self.extract_audio_features(audio)?;
        Ok(Vector::new(features))
    }

    fn encode_batch(&self, audios: &[AudioData]) -> Result<Vec<Vector>> {
        audios.iter().map(|audio| self.encode(audio)).collect()
    }

    fn get_embedding_dim(&self) -> usize {
        self.embedding_dim
    }

    fn extract_features(&self, audio: &AudioData) -> Result<Vec<f32>> {
        self.extract_audio_features(audio)
    }
}

/// Production video encoder using temporal features
pub struct ProductionVideoEncoder {
    embedding_dim: usize,
    image_encoder: ProductionImageEncoder,
}

impl ProductionVideoEncoder {
    pub fn new(embedding_dim: usize) -> Result<Self> {
        Ok(Self {
            embedding_dim,
            image_encoder: ProductionImageEncoder::new(embedding_dim)?,
        })
    }

    /// Extract video features from keyframes
    fn extract_video_features(&self, video: &VideoData) -> Result<Vec<f32>> {
        let mut all_features = Vec::new();

        // Encode keyframes
        for keyframe_idx in &video.keyframes {
            if let Some(frame) = video.frames.get(*keyframe_idx) {
                let frame_features = self.image_encoder.extract_image_features(frame)?;
                all_features.extend(frame_features);
            }
        }

        // If no keyframes, use first and last frame
        if all_features.is_empty() && !video.frames.is_empty() {
            let first_features = self
                .image_encoder
                .extract_image_features(&video.frames[0])?;
            all_features.extend(first_features);

            if video.frames.len() > 1 {
                let last_features = self.image_encoder.extract_image_features(
                    video
                        .frames
                        .last()
                        .expect("video frames validated to have more than one element"),
                )?;
                all_features.extend(last_features);
            }
        }

        // Aggregate to target dimension
        let mut aggregated = vec![0.0f32; self.embedding_dim];
        if !all_features.is_empty() {
            let chunk_size = all_features.len() / self.embedding_dim.max(1);
            if chunk_size > 0 {
                for (i, chunk) in all_features.chunks(chunk_size).enumerate() {
                    if i >= self.embedding_dim {
                        break;
                    }
                    aggregated[i] = chunk.iter().sum::<f32>() / chunk.len() as f32;
                }
            }
        }

        // Add temporal features
        if self.embedding_dim > 3 {
            aggregated[self.embedding_dim - 3] = video.fps / 60.0;
            aggregated[self.embedding_dim - 2] = video.duration / 600.0;
            aggregated[self.embedding_dim - 1] = video.frames.len() as f32 / 1000.0;
        }

        // Normalize
        let norm: f32 = aggregated.iter().map(|x| x * x).sum::<f32>().sqrt();
        if norm > 0.0 {
            aggregated.iter_mut().for_each(|x| *x /= norm);
        }

        Ok(aggregated)
    }
}

impl VideoEncoder for ProductionVideoEncoder {
    fn encode(&self, video: &VideoData) -> Result<Vector> {
        let features = self.extract_video_features(video)?;
        Ok(Vector::new(features))
    }

    fn encode_keyframes(&self, video: &VideoData) -> Result<Vec<Vector>> {
        video
            .keyframes
            .iter()
            .filter_map(|&idx| video.frames.get(idx))
            .map(|frame| self.image_encoder.encode(frame))
            .collect()
    }

    fn get_embedding_dim(&self) -> usize {
        self.embedding_dim
    }
}

/// Production graph encoder for knowledge graphs
pub struct ProductionGraphEncoder {
    embedding_dim: usize,
}

impl ProductionGraphEncoder {
    pub fn new(embedding_dim: usize) -> Result<Self> {
        Ok(Self { embedding_dim })
    }

    /// Extract graph features using node/edge statistics
    fn extract_graph_features(&self, graph: &GraphData) -> Result<Vec<f32>> {
        let mut features = vec![0.0f32; self.embedding_dim];

        // Node degree distribution
        let mut node_degrees = HashMap::new();
        for edge in &graph.edges {
            *node_degrees.entry(edge.source.clone()).or_insert(0) += 1;
            *node_degrees.entry(edge.target.clone()).or_insert(0) += 1;
        }

        // Aggregate degree statistics
        let degrees: Vec<usize> = node_degrees.values().copied().collect();
        if !degrees.is_empty() {
            let avg_degree = degrees.iter().sum::<usize>() as f32 / degrees.len() as f32;
            let max_degree = *degrees.iter().max().unwrap_or(&0) as f32;

            features[0] = avg_degree / 100.0;
            features[1] = max_degree / 100.0;
            features[2] = graph.nodes.len() as f32 / 1000.0;
            features[3] = graph.edges.len() as f32 / 1000.0;
        }

        // Node label distribution
        for (_i, node) in graph.nodes.iter().enumerate().take(self.embedding_dim / 2) {
            if !node.labels.is_empty() {
                let label_hash = Self::hash_string(&node.labels[0]);
                let idx = 4 + (label_hash % (self.embedding_dim as u64 / 2 - 4)) as usize;
                features[idx] += 1.0 / graph.nodes.len() as f32;
            }
        }

        // Normalize
        let norm: f32 = features.iter().map(|x| x * x).sum::<f32>().sqrt();
        if norm > 0.0 {
            features.iter_mut().for_each(|x| *x /= norm);
        }

        Ok(features)
    }

    fn hash_string(s: &str) -> u64 {
        use std::collections::hash_map::DefaultHasher;
        use std::hash::{Hash, Hasher};

        let mut hasher = DefaultHasher::new();
        s.hash(&mut hasher);
        hasher.finish()
    }
}

impl GraphEncoder for ProductionGraphEncoder {
    fn encode(&self, graph: &GraphData) -> Result<Vector> {
        let features = self.extract_graph_features(graph)?;
        Ok(Vector::new(features))
    }

    fn encode_node(&self, node: &crate::cross_modal_embeddings::GraphNode) -> Result<Vector> {
        // Encode single node as mini-graph
        let graph = GraphData {
            nodes: vec![node.clone()],
            edges: Vec::new(),
            metadata: HashMap::new(),
        };
        self.encode(&graph)
    }

    fn encode_subgraph(
        &self,
        nodes: &[crate::cross_modal_embeddings::GraphNode],
        edges: &[crate::cross_modal_embeddings::GraphEdge],
    ) -> Result<Vector> {
        let graph = GraphData {
            nodes: nodes.to_vec(),
            edges: edges.to_vec(),
            metadata: HashMap::new(),
        };
        self.encode(&graph)
    }

    fn get_embedding_dim(&self) -> usize {
        self.embedding_dim
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_text_query() -> Result<()> {
        let _engine = MultiModalSearchEngine::new_default()?;

        let query = MultiModalQuery::text("test query");
        assert_eq!(query.modalities.len(), 1);

        Ok(())
    }

    #[test]
    fn test_hybrid_query() -> Result<()> {
        let query = MultiModalQuery::hybrid(vec![
            QueryModality::Text("test".to_string()),
            QueryModality::Embedding(Vector::new(vec![0.0; 128])),
        ]);

        assert_eq!(query.modalities.len(), 2);

        Ok(())
    }

    #[test]
    fn test_text_encoder() -> Result<()> {
        let encoder = ProductionTextEncoder::new(128)?;

        let vector = encoder.encode("hello world")?;
        assert_eq!(vector.dimensions, 128);

        // Check normalization
        let magnitude = vector.magnitude();
        assert!((magnitude - 1.0).abs() < 0.1);

        Ok(())
    }

    #[test]
    fn test_image_encoder() -> Result<()> {
        let encoder = ProductionImageEncoder::new(256)?;

        let image_data = ImageData {
            data: vec![128; 1024],
            width: 32,
            height: 32,
            channels: 3,
            format: ImageFormat::RGB,
            features: None,
        };

        let vector = encoder.encode(&image_data)?;
        assert_eq!(vector.dimensions, 256);

        Ok(())
    }

    #[test]
    fn test_audio_encoder() -> Result<()> {
        let encoder = ProductionAudioEncoder::new(128)?;

        let audio_data = AudioData {
            samples: vec![0.5; 44100], // 1 second at 44.1kHz
            sample_rate: 44100,
            channels: 1,
            duration: 1.0,
            features: None,
        };

        let vector = encoder.encode(&audio_data)?;
        assert_eq!(vector.dimensions, 128);

        Ok(())
    }

    #[test]
    fn test_modality_fusion() -> Result<()> {
        let engine = MultiModalSearchEngine::new_default()?;

        // Create test content
        let mut modalities = HashMap::new();
        modalities.insert(Modality::Text, ModalityData::Text("test".to_string()));

        let content = MultiModalContent {
            modalities,
            metadata: HashMap::new(),
            temporal_info: None,
            spatial_info: None,
        };

        engine.index_content("test1".to_string(), content)?;

        let stats = engine.get_statistics();
        assert_eq!(stats.total_vectors, 1);

        Ok(())
    }
}