redis-tower-modules 0.1.0

High-level ergonomic client interfaces for Redis Stack modules
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
//! # Search Client
//!
//! Ergonomic, typed client over RediSearch. Query results are deserialized
//! into caller-supplied Rust types with `serde_json`, hiding the raw
//! [`Frame`] reply structure.
//!
//! # Example
//!
//! ```no_run
//! use redis_tower::RedisClient;
//! use redis_tower_modules::search::{IndexBuilder, SearchClient, SearchQuery, SortDir};
//! use serde::Deserialize;
//!
//! #[derive(Deserialize)]
//! struct Product {
//!     name: String,
//!     price: String,
//! }
//!
//! # async fn run() -> Result<(), Box<dyn std::error::Error>> {
//! let mut client = RedisClient::connect_url("redis://127.0.0.1:6379").await?;
//! let mut search = SearchClient::new(&mut client);
//!
//! search
//!     .create_index(
//!         IndexBuilder::new("products_idx")
//!             .on_hash()
//!             .prefix("product:")
//!             .text_field("name")
//!             .numeric_field("price"),
//!     )
//!     .await?;
//!
//! let results = search
//!     .search::<Product>(
//!         SearchQuery::new("products_idx", "shoes")
//!             .sort_by("price", SortDir::Asc)
//!             .limit(0, 10),
//!     )
//!     .await?;
//!
//! for doc in &results.docs {
//!     println!("{}: {} ({})", doc.key, doc.doc.name, doc.doc.price);
//! }
//! # Ok(())
//! # }
//! ```

use std::collections::HashMap;

use redis_tower::RedisExecutor;
use redis_tower::commands::{
    FieldType, FtAggregate, FtAliasAdd, FtAliasDel, FtAliasUpdate, FtAlter, FtConfigGet,
    FtConfigSet, FtCreate, FtDropIndex, FtInfo, FtList, FtSearch, FtSpellCheck, FtSugAdd, FtSugDel,
    FtSugGet, FtSugLen, SchemaField, SortOrder,
};
use redis_tower_core::{Frame, RedisError};
use serde::de::DeserializeOwned;

/// Sort direction for search and aggregate results.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SortDir {
    /// Ascending order.
    Asc,
    /// Descending order.
    Desc,
}

impl From<SortDir> for SortOrder {
    fn from(dir: SortDir) -> Self {
        match dir {
            SortDir::Asc => SortOrder::Asc,
            SortDir::Desc => SortOrder::Desc,
        }
    }
}

/// Fluent builder for an `FT.CREATE` index definition.
///
/// Wraps the low-level [`FtCreate`] command and exposes typed field helpers
/// that map onto RediSearch [`FieldType`] variants.
pub struct IndexBuilder {
    inner: FtCreate,
}

impl IndexBuilder {
    /// Start building an index with the given name.
    pub fn new(name: impl Into<String>) -> Self {
        Self {
            inner: FtCreate::new(name),
        }
    }

    /// Index `HASH` keys.
    pub fn on_hash(mut self) -> Self {
        self.inner = self.inner.on_hash();
        self
    }

    /// Index `JSON` keys.
    pub fn on_json(mut self) -> Self {
        self.inner = self.inner.on_json();
        self
    }

    /// Restrict the index to keys matching the given prefix.
    pub fn prefix(mut self, prefix: impl Into<String>) -> Self {
        self.inner = self.inner.prefix(prefix);
        self
    }

    /// Add a full-text `TEXT` field.
    pub fn text_field(mut self, name: impl Into<String>) -> Self {
        self.inner = self.inner.field(name, FieldType::Text);
        self
    }

    /// Add a `NUMERIC` field.
    pub fn numeric_field(mut self, name: impl Into<String>) -> Self {
        self.inner = self.inner.field(name, FieldType::Numeric);
        self
    }

    /// Add a `TAG` field.
    pub fn tag_field(mut self, name: impl Into<String>) -> Self {
        self.inner = self.inner.field(name, FieldType::Tag);
        self
    }

    /// Add a `GEO` field.
    pub fn geo_field(mut self, name: impl Into<String>) -> Self {
        self.inner = self.inner.field(name, FieldType::Geo);
        self
    }

    /// Consume the builder and produce the underlying [`FtCreate`] command.
    pub fn build(self) -> FtCreate {
        self.inner
    }
}

/// Fluent builder for an `FT.SEARCH` query with typed result deserialization.
pub struct SearchQuery {
    index: String,
    query: String,
    filters: Vec<String>,
    sort_field: Option<String>,
    sort_dir: Option<SortDir>,
    return_fields: Vec<String>,
    offset: Option<u64>,
    count: Option<u64>,
    nocontent: bool,
    verbatim: bool,
    withscores: bool,
}

impl SearchQuery {
    /// Create a new search query for the given index and query string.
    pub fn new(index: impl Into<String>, query: impl Into<String>) -> Self {
        Self {
            index: index.into(),
            query: query.into(),
            filters: Vec::new(),
            sort_field: None,
            sort_dir: None,
            return_fields: Vec::new(),
            offset: None,
            count: None,
            nocontent: false,
            verbatim: false,
            withscores: false,
        }
    }

    /// Append a filter expression to the query (joined with a space).
    pub fn filter(mut self, filter: impl Into<String>) -> Self {
        self.filters.push(filter.into());
        self
    }

    /// Sort results by a field in the given direction.
    pub fn sort_by(mut self, field: impl Into<String>, dir: SortDir) -> Self {
        self.sort_field = Some(field.into());
        self.sort_dir = Some(dir);
        self
    }

    /// Specify which fields to return in results.
    pub fn return_fields(mut self, fields: &[&str]) -> Self {
        self.return_fields = fields.iter().map(|f| (*f).to_string()).collect();
        self
    }

    /// Limit results with an offset and count.
    pub fn limit(mut self, offset: u64, count: u64) -> Self {
        self.offset = Some(offset);
        self.count = Some(count);
        self
    }

    /// Return only document IDs, not field content.
    pub fn nocontent(mut self) -> Self {
        self.nocontent = true;
        self
    }

    /// Disable stemming for query expansion.
    pub fn verbatim(mut self) -> Self {
        self.verbatim = true;
        self
    }

    /// Include relevance scores in the results.
    pub fn withscores(mut self) -> Self {
        self.withscores = true;
        self
    }

    /// Build the effective query string by appending filters.
    fn effective_query(&self) -> String {
        if self.filters.is_empty() {
            return self.query.clone();
        }
        let mut q = self.query.clone();
        for filter in &self.filters {
            q.push(' ');
            q.push_str(filter);
        }
        q
    }

    /// Build the underlying [`FtSearch`] command from builder state.
    fn build(&self) -> FtSearch {
        let query = self.effective_query();
        let mut cmd = FtSearch::new(&self.index, &query);

        if let Some(offset) = self.offset {
            cmd = cmd.limit(offset, self.count.unwrap_or(10));
        }

        if !self.return_fields.is_empty() {
            let field_refs: Vec<&str> = self.return_fields.iter().map(String::as_str).collect();
            cmd = cmd.return_fields(&field_refs);
        }

        if let Some(field) = &self.sort_field {
            cmd = cmd.sortby(field, self.sort_dir.unwrap_or(SortDir::Asc).into());
        }

        if self.nocontent {
            cmd = cmd.nocontent();
        }
        if self.verbatim {
            cmd = cmd.verbatim();
        }
        if self.withscores {
            cmd = cmd.withscores();
        }

        cmd
    }
}

/// Search results containing deserialized documents.
#[derive(Debug)]
pub struct SearchResults<T> {
    /// Total number of matching documents (may exceed `docs.len()`).
    pub total: i64,
    /// The matching documents.
    pub docs: Vec<SearchDoc<T>>,
}

/// A single document from search results.
#[derive(Debug)]
pub struct SearchDoc<T> {
    /// The document key in Redis.
    pub key: String,
    /// The deserialized document.
    pub doc: T,
    /// Optional relevance score (present when `WITHSCORES` was used).
    pub score: Option<f64>,
}

/// Parsed statistics from an `FT.INFO` reply.
#[derive(Debug, Clone, Default, PartialEq)]
pub struct IndexInfo {
    /// The index name.
    pub index_name: String,
    /// Number of documents indexed.
    pub num_docs: u64,
    /// Number of distinct terms.
    pub num_terms: u64,
    /// Number of inverted-index records.
    pub num_records: u64,
    /// Size of the inverted index in megabytes.
    pub inverted_sz_mb: f64,
    /// Whether the index is currently being (re)built.
    pub indexing: bool,
    /// Percentage of the index that has been built (0.0 – 1.0).
    pub percent_indexed: f64,
}

/// Fluent builder for an `FT.AGGREGATE` query.
///
/// Aggregation replies are returned as a raw [`Frame`]; typed deserialization
/// is intentionally out of scope.
pub struct AggregateQuery {
    inner: FtAggregate,
}

impl AggregateQuery {
    /// Create a new aggregate query for the given index and query string.
    pub fn new(index: impl Into<String>, query: impl Into<String>) -> Self {
        Self {
            inner: FtAggregate::new(index, query),
        }
    }

    /// Group results by the given properties.
    pub fn groupby(mut self, properties: &[&str]) -> Self {
        self.inner = self.inner.groupby(properties);
        self
    }

    /// Add a `REDUCE` function with arguments and an optional alias.
    pub fn reduce(mut self, func: impl Into<String>, args: &[&str], alias: Option<&str>) -> Self {
        self.inner = self.inner.reduce(func, args, alias);
        self
    }

    /// Sort results by a field in the given direction.
    pub fn sort_by(mut self, field: impl Into<String>, dir: SortDir) -> Self {
        self.inner = self.inner.sortby(field, dir.into());
        self
    }

    /// Limit results with an offset and count.
    pub fn limit(mut self, offset: u64, count: u64) -> Self {
        self.inner = self.inner.limit(offset, count);
        self
    }

    /// Add an `APPLY` expression with an alias.
    pub fn apply(mut self, expr: impl Into<String>, alias: impl Into<String>) -> Self {
        self.inner = self.inner.apply(expr, alias);
        self
    }

    /// Consume the builder and produce the underlying [`FtAggregate`] command.
    fn build(self) -> FtAggregate {
        self.inner
    }
}

/// A single auto-complete suggestion from `FT.SUGGET`.
#[derive(Debug, Clone, PartialEq)]
pub struct Suggestion {
    /// The suggested string.
    pub string: String,
    /// The score (present when `WITHSCORES` was requested).
    pub score: Option<f64>,
    /// The payload (present when `WITHPAYLOADS` was requested).
    pub payload: Option<String>,
}

/// Options for an `FT.SUGGET` request.
#[derive(Debug, Clone, Default)]
pub struct SugGetOptions {
    /// Perform fuzzy prefix matching.
    pub fuzzy: bool,
    /// Include scores in the response.
    pub withscores: bool,
    /// Include payloads in the response.
    pub withpayloads: bool,
    /// Maximum number of suggestions to return.
    pub max: Option<u64>,
}

/// Options for an `FT.SPELLCHECK` request.
#[derive(Debug, Clone, Default)]
pub struct SpellCheckOptions {
    /// Maximum Levenshtein distance for suggestions (1–4).
    pub distance: Option<u64>,
    /// Name of a dictionary whose terms should be included.
    pub include_terms: Option<String>,
    /// Name of a dictionary whose terms should be excluded.
    pub exclude_terms: Option<String>,
}

/// A spelling-correction result for one misspelled term.
#[derive(Debug, Clone, PartialEq)]
pub struct SpellCheckResult {
    /// The misspelled term.
    pub term: String,
    /// Candidate corrections as `(score, suggestion)` pairs.
    pub suggestions: Vec<(f64, String)>,
}

/// High-level client for RediSearch operations.
///
/// Borrows an underlying executor `C` for the duration of the operations and
/// exposes index lifecycle management, typed search, aggregation,
/// auto-complete, spellcheck, alias, and config operations.
pub struct SearchClient<'a, C> {
    conn: &'a mut C,
}

impl<'a, C: RedisExecutor> SearchClient<'a, C> {
    /// Create a new [`SearchClient`] borrowing the given executor.
    pub fn new(conn: &'a mut C) -> Self {
        Self { conn }
    }

    // -- Index lifecycle --

    /// Create a search index from an [`IndexBuilder`].
    pub async fn create_index(&mut self, builder: IndexBuilder) -> Result<(), RedisError> {
        self.conn.execute(builder.build()).await
    }

    /// Drop a search index, optionally deleting the indexed documents.
    pub async fn drop_index(&mut self, index: &str, delete_docs: bool) -> Result<(), RedisError> {
        let mut cmd = FtDropIndex::new(index);
        if delete_docs {
            cmd = cmd.dd();
        }
        self.conn.execute(cmd).await
    }

    /// Add new fields to an existing index schema.
    pub async fn alter_index(
        &mut self,
        index: &str,
        fields: Vec<SchemaField>,
    ) -> Result<(), RedisError> {
        let mut cmd = FtAlter::new(index);
        for field in fields {
            cmd = cmd.schema_field(field);
        }
        self.conn.execute(cmd).await
    }

    /// Fetch and parse statistics about an index.
    pub async fn index_info(&mut self, index: &str) -> Result<IndexInfo, RedisError> {
        let frame = self.conn.execute(FtInfo::new(index)).await?;
        parse_index_info(frame)
    }

    /// List all existing index names.
    pub async fn list_indexes(&mut self) -> Result<Vec<String>, RedisError> {
        let names = self.conn.execute(FtList::new()).await?;
        names
            .into_iter()
            .map(|b| String::from_utf8(b.to_vec()).map_err(|e| RedisError::Redis(format!("{e}"))))
            .collect()
    }

    // -- Search --

    /// Execute a search query, deserializing each document into `T`.
    pub async fn search<T: DeserializeOwned>(
        &mut self,
        query: SearchQuery,
    ) -> Result<SearchResults<T>, RedisError> {
        let withscores = query.withscores;
        let cmd = query.build();
        let frame = self.conn.execute(cmd).await?;
        parse_search_results(frame, withscores)
    }

    // -- Aggregate --

    /// Execute an aggregate query, returning the raw reply [`Frame`].
    pub async fn aggregate(&mut self, query: AggregateQuery) -> Result<Frame, RedisError> {
        self.conn.execute(query.build()).await
    }

    // -- Autocomplete --

    /// Add a suggestion string to an auto-complete dictionary.
    ///
    /// Returns the current size of the dictionary.
    pub async fn sug_add(
        &mut self,
        key: &str,
        string: &str,
        score: f64,
    ) -> Result<i64, RedisError> {
        self.conn.execute(FtSugAdd::new(key, string, score)).await
    }

    /// Get completion suggestions for a prefix.
    pub async fn sug_get(
        &mut self,
        key: &str,
        prefix: &str,
        options: SugGetOptions,
    ) -> Result<Vec<Suggestion>, RedisError> {
        let mut cmd = FtSugGet::new(key, prefix);
        if options.fuzzy {
            cmd = cmd.fuzzy();
        }
        if options.withscores {
            cmd = cmd.withscores();
        }
        if options.withpayloads {
            cmd = cmd.withpayloads();
        }
        if let Some(max) = options.max {
            cmd = cmd.max(max);
        }
        let frame = self.conn.execute(cmd).await?;
        parse_suggestions(frame, options.withscores, options.withpayloads)
    }

    /// Delete a string from an auto-complete dictionary.
    ///
    /// Returns `true` if the string was found and removed.
    pub async fn sug_del(&mut self, key: &str, string: &str) -> Result<bool, RedisError> {
        self.conn.execute(FtSugDel::new(key, string)).await
    }

    /// Return the number of entries in an auto-complete dictionary.
    pub async fn sug_len(&mut self, key: &str) -> Result<i64, RedisError> {
        self.conn.execute(FtSugLen::new(key)).await
    }

    // -- Spellcheck --

    /// Run spelling correction over a query, returning suggestions per term.
    pub async fn spellcheck(
        &mut self,
        index: &str,
        query: &str,
        options: SpellCheckOptions,
    ) -> Result<Vec<SpellCheckResult>, RedisError> {
        let mut cmd = FtSpellCheck::new(index, query);
        if let Some(dist) = options.distance {
            cmd = cmd.distance(dist);
        }
        if let Some(dict) = options.include_terms {
            cmd = cmd.include_terms(dict);
        }
        if let Some(dict) = options.exclude_terms {
            cmd = cmd.exclude_terms(dict);
        }
        let frame = self.conn.execute(cmd).await?;
        parse_spellcheck(frame)
    }

    // -- Alias management --

    /// Add an alias pointing to an index.
    pub async fn alias_add(&mut self, alias: &str, index: &str) -> Result<(), RedisError> {
        self.conn.execute(FtAliasAdd::new(alias, index)).await
    }

    /// Remove an alias.
    pub async fn alias_del(&mut self, alias: &str) -> Result<(), RedisError> {
        self.conn.execute(FtAliasDel::new(alias)).await
    }

    /// Update an alias to point to a different index.
    pub async fn alias_update(&mut self, alias: &str, index: &str) -> Result<(), RedisError> {
        self.conn.execute(FtAliasUpdate::new(alias, index)).await
    }

    // -- Config --

    /// Set a RediSearch configuration option.
    pub async fn config_set(&mut self, option: &str, value: &str) -> Result<(), RedisError> {
        self.conn.execute(FtConfigSet::new(option, value)).await
    }

    /// Get a RediSearch configuration option as a raw reply [`Frame`].
    pub async fn config_get(&mut self, option: &str) -> Result<Frame, RedisError> {
        self.conn.execute(FtConfigGet::new(option)).await
    }
}

// -- Parsing helpers --

/// Extract a UTF-8 string from a scalar (string-like, integer, or double) frame.
fn frame_to_string(frame: &Frame) -> Result<String, RedisError> {
    match frame {
        Frame::BulkString(Some(data)) | Frame::SimpleString(data) => {
            String::from_utf8(data.to_vec()).map_err(|e| RedisError::Redis(format!("{e}")))
        }
        Frame::Integer(n) => Ok(n.to_string()),
        Frame::Double(d) => Ok(d.to_string()),
        other => Err(RedisError::UnexpectedResponse {
            expected: "string-like frame",
            actual: format!("{other:?}"),
        }),
    }
}

/// Extract an `f64` from a numeric or string-like frame, if possible.
fn frame_to_f64(frame: &Frame) -> Option<f64> {
    match frame {
        Frame::Double(d) => Some(*d),
        Frame::Integer(n) => Some(*n as f64),
        _ => frame_to_string(frame).ok().and_then(|s| s.parse().ok()),
    }
}

/// Parse an `FT.SEARCH` reply into typed search results.
///
/// With RESP2, `FT.SEARCH` returns:
/// `[total, key1, [field1, val1, ...], key2, [...], ...]`
///
/// With `WITHSCORES` a score is interleaved after each key:
/// `[total, key1, score1, [field1, val1, ...], ...]`
///
/// With RESP3, the top-level response is a map containing `total_results`
/// and a `results` array. Each result is itself a map containing `id`,
/// `extra_attributes`, and optionally `score`.
fn parse_search_results<T: DeserializeOwned>(
    frame: Frame,
    withscores: bool,
) -> Result<SearchResults<T>, RedisError> {
    match frame {
        Frame::Array(Some(items)) => parse_resp2_search_results(items, withscores),
        Frame::Map(entries) | Frame::StreamedMap(entries) => {
            parse_resp3_search_results(entries, withscores)
        }
        other => Err(RedisError::UnexpectedResponse {
            expected: "array or map",
            actual: format!("{other:?}"),
        }),
    }
}

fn deserialize_search_doc<'a, T: DeserializeOwned>(
    fields: impl IntoIterator<Item = (&'a Frame, &'a Frame)>,
) -> Result<T, RedisError> {
    let mut map = serde_json::Map::new();
    for (key, value) in fields {
        map.insert(
            frame_to_string(key)?,
            serde_json::Value::String(frame_to_string(value)?),
        );
    }

    serde_json::from_value(serde_json::Value::Object(map))
        .map_err(|e| RedisError::Redis(format!("deserialize error: {e}")))
}

fn parse_resp2_search_results<T: DeserializeOwned>(
    items: Vec<Frame>,
    withscores: bool,
) -> Result<SearchResults<T>, RedisError> {
    if items.is_empty() {
        return Err(RedisError::UnexpectedResponse {
            expected: "non-empty array with total count",
            actual: "empty array".to_string(),
        });
    }

    let total = match &items[0] {
        Frame::Integer(n) => *n,
        other => {
            return Err(RedisError::UnexpectedResponse {
                expected: "integer (total count)",
                actual: format!("{other:?}"),
            });
        }
    };

    let mut docs = Vec::new();
    let mut i = 1;

    while i < items.len() {
        let key = frame_to_string(&items[i])?;
        i += 1;

        let score = if withscores {
            if i >= items.len() {
                return Err(RedisError::UnexpectedResponse {
                    expected: "score value",
                    actual: "end of array".to_string(),
                });
            }
            let s = frame_to_f64(&items[i]).unwrap_or(0.0);
            i += 1;
            Some(s)
        } else {
            None
        };

        if i >= items.len() {
            return Err(RedisError::UnexpectedResponse {
                expected: "field/value array",
                actual: "end of array".to_string(),
            });
        }

        let fields = match &items[i] {
            Frame::Array(Some(fields)) => fields,
            other => {
                return Err(RedisError::UnexpectedResponse {
                    expected: "array of field/value pairs",
                    actual: format!("{other:?}"),
                });
            }
        };
        i += 1;

        let doc = deserialize_search_doc(
            fields
                .chunks(2)
                .filter(|chunk| chunk.len() == 2)
                .map(|chunk| (&chunk[0], &chunk[1])),
        )?;

        docs.push(SearchDoc { key, doc, score });
    }

    Ok(SearchResults { total, docs })
}

fn parse_resp3_search_results<T: DeserializeOwned>(
    entries: Vec<(Frame, Frame)>,
    withscores: bool,
) -> Result<SearchResults<T>, RedisError> {
    let mut response = HashMap::new();
    for (key, value) in entries {
        response.insert(frame_to_string(&key)?, value);
    }

    let total = match response.remove("total_results") {
        Some(Frame::Integer(total)) => total,
        Some(other) => {
            return Err(RedisError::UnexpectedResponse {
                expected: "integer total_results",
                actual: format!("{other:?}"),
            });
        }
        None => {
            return Err(RedisError::UnexpectedResponse {
                expected: "total_results field",
                actual: "field not found".to_string(),
            });
        }
    };

    let results = match response.remove("results") {
        Some(Frame::Array(Some(results))) | Some(Frame::StreamedArray(results)) => results,
        Some(other) => {
            return Err(RedisError::UnexpectedResponse {
                expected: "results array",
                actual: format!("{other:?}"),
            });
        }
        None => {
            return Err(RedisError::UnexpectedResponse {
                expected: "results field",
                actual: "field not found".to_string(),
            });
        }
    };

    let mut docs = Vec::with_capacity(results.len());
    for result in results {
        let entries = match result {
            Frame::Map(entries) | Frame::StreamedMap(entries) => entries,
            other => {
                return Err(RedisError::UnexpectedResponse {
                    expected: "result map",
                    actual: format!("{other:?}"),
                });
            }
        };
        let mut result = HashMap::new();
        for (key, value) in entries {
            result.insert(frame_to_string(&key)?, value);
        }

        let key = result
            .remove("id")
            .ok_or_else(|| RedisError::UnexpectedResponse {
                expected: "result id field",
                actual: "field not found".to_string(),
            })
            .and_then(|frame| frame_to_string(&frame))?;

        let score = if withscores {
            Some(
                result
                    .remove("score")
                    .as_ref()
                    .and_then(frame_to_f64)
                    .unwrap_or(0.0),
            )
        } else {
            None
        };

        let fields = match result.remove("extra_attributes") {
            Some(Frame::Map(fields)) | Some(Frame::StreamedMap(fields)) => fields,
            Some(other) => {
                return Err(RedisError::UnexpectedResponse {
                    expected: "extra_attributes map",
                    actual: format!("{other:?}"),
                });
            }
            None => {
                return Err(RedisError::UnexpectedResponse {
                    expected: "extra_attributes field",
                    actual: "field not found".to_string(),
                });
            }
        };
        let doc = deserialize_search_doc(fields.iter().map(|(key, value)| (key, value)))?;
        docs.push(SearchDoc { key, doc, score });
    }

    Ok(SearchResults { total, docs })
}

/// Build a flat string key/value map from an `FT.INFO`-style reply.
///
/// Accepts both the RESP2 flat alternating array and the RESP3 map form.
/// Non-scalar values (nested arrays) are skipped.
fn frame_to_kv_map(frame: Frame) -> Result<HashMap<String, String>, RedisError> {
    let mut map = HashMap::new();
    match frame {
        Frame::Array(Some(items)) => {
            for chunk in items.chunks(2) {
                if chunk.len() < 2 {
                    continue;
                }
                let key = frame_to_string(&chunk[0])?;
                if let Ok(value) = frame_to_string(&chunk[1]) {
                    map.insert(key, value);
                }
            }
            Ok(map)
        }
        Frame::Map(pairs) => {
            for (k, v) in &pairs {
                let key = frame_to_string(k)?;
                if let Ok(value) = frame_to_string(v) {
                    map.insert(key, value);
                }
            }
            Ok(map)
        }
        other => Err(RedisError::UnexpectedResponse {
            expected: "array or map",
            actual: format!("{other:?}"),
        }),
    }
}

/// Parse an `FT.INFO` reply into an [`IndexInfo`], filling missing fields with
/// defaults.
fn parse_index_info(frame: Frame) -> Result<IndexInfo, RedisError> {
    let map = frame_to_kv_map(frame)?;
    let parse_u64 = |k: &str| map.get(k).and_then(|s| s.parse::<u64>().ok()).unwrap_or(0);
    let parse_f64 = |k: &str| {
        map.get(k)
            .and_then(|s| s.parse::<f64>().ok())
            .unwrap_or(0.0)
    };
    Ok(IndexInfo {
        index_name: map.get("index_name").cloned().unwrap_or_default(),
        num_docs: parse_u64("num_docs"),
        num_terms: parse_u64("num_terms"),
        num_records: parse_u64("num_records"),
        inverted_sz_mb: parse_f64("inverted_sz_mb"),
        indexing: map
            .get("indexing")
            .map(|s| s != "0" && !s.eq_ignore_ascii_case("false"))
            .unwrap_or(false),
        percent_indexed: parse_f64("percent_indexed"),
    })
}

/// Parse an `FT.SUGGET` reply into a list of [`Suggestion`]s.
///
/// The layout depends on the requested options:
/// - neither: `[str1, str2, ...]`
/// - scores: `[str1, score1, ...]`
/// - payloads: `[str1, payload1, ...]`
/// - both: `[str1, score1, payload1, ...]`
fn parse_suggestions(
    frame: Frame,
    withscores: bool,
    withpayloads: bool,
) -> Result<Vec<Suggestion>, RedisError> {
    let items = match frame {
        Frame::Array(Some(items)) => items,
        Frame::Array(None) | Frame::Null => return Ok(Vec::new()),
        other => {
            return Err(RedisError::UnexpectedResponse {
                expected: "array",
                actual: format!("{other:?}"),
            });
        }
    };

    let stride = 1 + usize::from(withscores) + usize::from(withpayloads);
    let mut out = Vec::new();
    let mut i = 0;
    while i < items.len() {
        let string = frame_to_string(&items[i])?;
        let mut j = i + 1;

        let score = if withscores {
            let s = items.get(j).and_then(frame_to_f64);
            j += 1;
            s
        } else {
            None
        };

        let payload = if withpayloads {
            let p = match items.get(j) {
                Some(f) => Some(frame_to_string(f)?),
                None => None,
            };
            j += 1;
            p
        } else {
            None
        };

        let _ = j;
        out.push(Suggestion {
            string,
            score,
            payload,
        });
        i += stride;
    }

    Ok(out)
}

/// Parse an `FT.SPELLCHECK` reply into a list of [`SpellCheckResult`]s.
///
/// Each entry is `["TERM", term, [[score1, suggestion1], ...]]`.
fn parse_spellcheck(frame: Frame) -> Result<Vec<SpellCheckResult>, RedisError> {
    let items = match frame {
        Frame::Array(Some(items)) => items,
        Frame::Array(None) | Frame::Null => return Ok(Vec::new()),
        other => {
            return Err(RedisError::UnexpectedResponse {
                expected: "array",
                actual: format!("{other:?}"),
            });
        }
    };

    let mut out = Vec::new();
    for item in &items {
        let entry = match item {
            Frame::Array(Some(entry)) if entry.len() >= 3 => entry,
            _ => continue,
        };

        let term = frame_to_string(&entry[1])?;
        let suggestions = match &entry[2] {
            Frame::Array(Some(pairs)) => {
                let mut v = Vec::new();
                for pair in pairs {
                    if let Frame::Array(Some(pair)) = pair
                        && pair.len() >= 2
                    {
                        let score = frame_to_f64(&pair[0]).unwrap_or(0.0);
                        let suggestion = frame_to_string(&pair[1])?;
                        v.push((score, suggestion));
                    }
                }
                v
            }
            _ => Vec::new(),
        };

        out.push(SpellCheckResult { term, suggestions });
    }

    Ok(out)
}

#[cfg(test)]
mod tests {
    use super::*;
    use redis_tower_core::Command;
    use serde::Deserialize;
    use std::collections::VecDeque;

    /// A mock executor that returns pre-configured frames in order.
    struct MockRedis {
        responses: VecDeque<Frame>,
    }

    impl MockRedis {
        fn new(responses: Vec<Frame>) -> Self {
            Self {
                responses: VecDeque::from(responses),
            }
        }
    }

    impl RedisExecutor for MockRedis {
        fn execute<Cmd: Command>(
            &mut self,
            cmd: Cmd,
        ) -> impl std::future::Future<Output = Result<Cmd::Response, RedisError>> + Send {
            let frame = self.responses.pop_front().unwrap_or(Frame::Null);
            async move { cmd.parse_response(frame) }
        }
    }

    /// Build a `BulkString` frame from a `&str` (Bytes type inferred).
    fn bs(s: &str) -> Frame {
        Frame::BulkString(Some(s.as_bytes().to_vec().into()))
    }

    /// Build a `SimpleString` frame from a `&str`.
    fn ss(s: &str) -> Frame {
        Frame::SimpleString(s.as_bytes().to_vec().into())
    }

    #[derive(Debug, Deserialize, PartialEq)]
    struct Doc {
        name: String,
    }

    #[tokio::test]
    async fn search_basic() {
        let mut mock = MockRedis::new(vec![Frame::Array(Some(vec![
            Frame::Integer(2),
            bs("doc:1"),
            Frame::Array(Some(vec![bs("name"), bs("Widget")])),
            bs("doc:2"),
            Frame::Array(Some(vec![bs("name"), bs("Gizmo")])),
        ]))]);
        let mut client = SearchClient::new(&mut mock);

        let results: SearchResults<Doc> =
            client.search(SearchQuery::new("idx", "*")).await.unwrap();

        assert_eq!(results.total, 2);
        assert_eq!(results.docs.len(), 2);
        assert_eq!(results.docs[0].key, "doc:1");
        assert_eq!(results.docs[0].doc.name, "Widget");
        assert!(results.docs[0].score.is_none());
        assert_eq!(results.docs[1].key, "doc:2");
        assert_eq!(results.docs[1].doc.name, "Gizmo");
    }

    #[tokio::test]
    async fn search_empty() {
        let mut mock = MockRedis::new(vec![Frame::Array(Some(vec![Frame::Integer(0)]))]);
        let mut client = SearchClient::new(&mut mock);

        let results: SearchResults<Doc> = client
            .search(SearchQuery::new("idx", "nomatch"))
            .await
            .unwrap();

        assert_eq!(results.total, 0);
        assert!(results.docs.is_empty());
    }

    #[tokio::test]
    async fn search_withscores() {
        let mut mock = MockRedis::new(vec![Frame::Array(Some(vec![
            Frame::Integer(1),
            bs("doc:1"),
            bs("0.75"),
            Frame::Array(Some(vec![bs("name"), bs("Widget")])),
        ]))]);
        let mut client = SearchClient::new(&mut mock);

        let results: SearchResults<Doc> = client
            .search(SearchQuery::new("idx", "*").withscores())
            .await
            .unwrap();

        assert_eq!(results.total, 1);
        assert_eq!(results.docs.len(), 1);
        assert!((results.docs[0].score.unwrap() - 0.75).abs() < f64::EPSILON);
        assert_eq!(results.docs[0].doc.name, "Widget");
    }

    #[tokio::test]
    async fn search_resp3_map() {
        let mut mock = MockRedis::new(vec![Frame::Map(vec![
            (ss("attributes"), Frame::Array(Some(vec![]))),
            (ss("format"), ss("STRING")),
            (
                ss("results"),
                Frame::Array(Some(vec![Frame::Map(vec![
                    (ss("id"), bs("doc:1")),
                    (
                        ss("extra_attributes"),
                        Frame::Map(vec![(bs("name"), bs("Widget"))]),
                    ),
                    (ss("values"), Frame::Array(Some(vec![]))),
                ])])),
            ),
            (ss("total_results"), Frame::Integer(1)),
            (ss("warning"), Frame::Array(Some(vec![]))),
        ])]);
        let mut client = SearchClient::new(&mut mock);

        let results: SearchResults<Doc> =
            client.search(SearchQuery::new("idx", "*")).await.unwrap();

        assert_eq!(results.total, 1);
        assert_eq!(results.docs.len(), 1);
        assert_eq!(results.docs[0].key, "doc:1");
        assert_eq!(results.docs[0].doc.name, "Widget");
        assert!(results.docs[0].score.is_none());
    }

    #[tokio::test]
    async fn search_resp3_map_withscores() {
        let mut mock = MockRedis::new(vec![Frame::Map(vec![
            (
                ss("results"),
                Frame::Array(Some(vec![Frame::Map(vec![
                    (ss("id"), bs("doc:1")),
                    (ss("score"), Frame::Double(0.75)),
                    (
                        ss("extra_attributes"),
                        Frame::Map(vec![(bs("name"), bs("Widget"))]),
                    ),
                ])])),
            ),
            (ss("total_results"), Frame::Integer(1)),
        ])]);
        let mut client = SearchClient::new(&mut mock);

        let results: SearchResults<Doc> = client
            .search(SearchQuery::new("idx", "*").withscores())
            .await
            .unwrap();

        assert_eq!(results.total, 1);
        assert_eq!(results.docs.len(), 1);
        assert!((results.docs[0].score.unwrap() - 0.75).abs() < f64::EPSILON);
        assert_eq!(results.docs[0].doc.name, "Widget");
    }

    #[tokio::test]
    async fn create_index_ok() {
        let mut mock = MockRedis::new(vec![ss("OK")]);
        let mut client = SearchClient::new(&mut mock);

        let result = client
            .create_index(
                IndexBuilder::new("idx")
                    .on_hash()
                    .prefix("doc:")
                    .text_field("name")
                    .numeric_field("price"),
            )
            .await;

        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn drop_index_ok() {
        let mut mock = MockRedis::new(vec![ss("OK")]);
        let mut client = SearchClient::new(&mut mock);

        let result = client.drop_index("idx", true).await;
        assert!(result.is_ok());
    }

    #[tokio::test]
    async fn sug_add_returns_count() {
        let mut mock = MockRedis::new(vec![Frame::Integer(5)]);
        let mut client = SearchClient::new(&mut mock);

        let count = client.sug_add("sug", "hello", 1.0).await.unwrap();
        assert_eq!(count, 5);
    }

    #[tokio::test]
    async fn sug_get_basic() {
        let mut mock = MockRedis::new(vec![Frame::Array(Some(vec![bs("hello"), bs("help")]))]);
        let mut client = SearchClient::new(&mut mock);

        let suggestions = client
            .sug_get("sug", "hel", SugGetOptions::default())
            .await
            .unwrap();

        assert_eq!(suggestions.len(), 2);
        assert_eq!(suggestions[0].string, "hello");
        assert!(suggestions[0].score.is_none());
        assert!(suggestions[0].payload.is_none());
        assert_eq!(suggestions[1].string, "help");
    }

    #[tokio::test]
    async fn index_info_parses_fields() {
        let mut mock = MockRedis::new(vec![Frame::Array(Some(vec![
            bs("index_name"),
            bs("idx"),
            bs("num_docs"),
            Frame::Integer(42),
            bs("num_terms"),
            Frame::Integer(7),
            bs("num_records"),
            Frame::Integer(100),
            bs("inverted_sz_mb"),
            bs("1.5"),
            bs("indexing"),
            Frame::Integer(0),
            bs("percent_indexed"),
            bs("1"),
        ]))]);
        let mut client = SearchClient::new(&mut mock);

        let info = client.index_info("idx").await.unwrap();

        assert_eq!(info.index_name, "idx");
        assert_eq!(info.num_docs, 42);
        assert_eq!(info.num_terms, 7);
        assert_eq!(info.num_records, 100);
        assert!((info.inverted_sz_mb - 1.5).abs() < f64::EPSILON);
        assert!(!info.indexing);
        assert!((info.percent_indexed - 1.0).abs() < f64::EPSILON);
    }
}