backbone-search 3.8.2

Backbone Framework Search Module - Elasticsearch and Algolia support
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
//! Elasticsearch search implementation
#![allow(dead_code)]
#![allow(unused_variables)]

use async_trait::async_trait;
use elasticsearch::{Elasticsearch, http::transport::Transport, SearchParts};
// use elasticsearch::params::*; // Currently unused
use elasticsearch::{BulkParts, UpdateParts, DeleteParts, GetParts};
use elasticsearch::indices::{IndicesCreateParts, IndicesGetParts, IndicesExistsParts, IndicesStatsParts, IndicesForcemergeParts, IndicesGetMappingParts};
use crate::{
    SearchResult, SearchError, SearchService, SearchDocument, SearchQuery, SearchStats,
    SearchBackend, SearchResults, SearchHit, FilterValue, FilterOperator,
    SortOrder, IndexConfig, IndexInfo, IndexStatus, IndexHealth, IndexMapping,
    BulkOperation, BulkOperationType, BulkResult, IndexResult, TimeRange,
    SearchAnalytics
};
use std::collections::HashMap;
use chrono::Utc;
use serde_json::json;

/// Elasticsearch configuration
#[derive(Debug, Clone)]
pub struct ElasticsearchConfig {
    /// Elasticsearch URLs
    pub urls: Vec<String>,

    /// Username for authentication
    pub username: Option<String>,

    /// Password for authentication
    pub password: Option<String>,

    /// API key for authentication
    pub api_key: Option<String>,

    /// Cloud ID for Elastic Cloud
    pub cloud_id: Option<String>,

    /// Connection timeout in seconds
    pub timeout: u64,

    /// Request retry count
    pub retry_count: u32,

    /// Pool size
    pub pool_size: u32,

    /// Enable compression
    pub compression: bool,

    /// Certificate verification
    pub verify_certs: bool,
}

impl Default for ElasticsearchConfig {
    fn default() -> Self {
        Self {
            urls: vec!["http://localhost:9200".to_string()],
            username: None,
            password: None,
            api_key: None,
            cloud_id: None,
            timeout: 30,
            retry_count: 3,
            pool_size: 10,
            compression: true,
            verify_certs: true,
        }
    }
}

/// Elasticsearch search service
pub struct ElasticsearchSearch {
    client: Elasticsearch,
    config: ElasticsearchConfig,
}

impl ElasticsearchSearch {
    /// Create new Elasticsearch search service
    pub async fn new(config: ElasticsearchConfig) -> SearchResult<Self> {
        let url = &config.urls[0];
        let transport = Transport::single_node(url)
            .map_err(|e| SearchError::ElasticsearchConnection(e.to_string()))?;

        let client = Elasticsearch::new(transport);

        Ok(Self { client, config })
    }

    /// Builder for Elasticsearch search service
    pub fn builder() -> ElasticsearchSearchBuilder {
        ElasticsearchSearchBuilder::new()
    }

    /// Convert SearchDocument to Elasticsearch document
    fn convert_document_to_es(&self, doc: &SearchDocument) -> serde_json::Result<serde_json::Value> {
        let mut es_doc = serde_json::json!({
            "id": doc.id,
            "content": doc.content,
            "timestamp": doc.timestamp,
            "tags": doc.tags,
            "boost": doc.boost
        });

        // Add title if present
        if let Some(title) = &doc.title {
            es_doc["title"] = json!(title);
        }

        // Add fields
        for (key, value) in &doc.fields {
            es_doc[key] = value.clone();
        }

        // Add metadata
        es_doc["metadata"] = serde_json::to_value(&doc.metadata)?;

        // Add language if present
        if let Some(language) = &doc.language {
            es_doc["language"] = json!(language);
        }

        Ok(es_doc)
    }

    /// Convert Elasticsearch document to SearchDocument
    fn convert_es_document_to_document(&self, es_doc: &serde_json::Value, id: &str) -> SearchResult<SearchDocument> {
        let doc: SearchDocument = serde_json::from_value(es_doc.clone())
            .map_err(|e| SearchError::Deserialization(e.to_string()))?;

        // Ensure ID matches
        if doc.id != id {
            return Err(SearchError::Deserialization("Document ID mismatch".to_string()));
        }

        Ok(doc)
    }

    /// Convert SearchQuery to Elasticsearch query
    fn convert_query_to_es(&self, query: &SearchQuery) -> serde_json::Result<serde_json::Value> {
        let mut es_query = json!({});

        // Build query part
        let mut query_clause = json!([]);
        let mut should_clauses = vec![];

        // Text query
        if let Some(text) = &query.text {
            should_clauses.push(json!({
                "multi_match": {
                    "query": text,
                    "fields": ["content^2", "title^3", "fields.*"],
                    "fuzziness": if query.options.fuzzy {
                        Some(query.options.fuzziness.to_string())
                    } else {
                        Some("AUTO".to_string())
                    },
                    "type": "best_fields"
                }
            }));
        }

        // Field queries
        for field_query in &query.fields {
            should_clauses.push(json!({
                "match": {
                    field_query.field.clone(): {
                        "query": field_query.query,
                        "boost": field_query.boost.unwrap_or(1.0)
                    }
                }
            }));
        }

        if !should_clauses.is_empty() {
            query_clause = json!({
                "bool": {
                    "should": should_clauses,
                    "minimum_should_match": query.options.minimum_should_match
                }
            });
        }

        // Filters
        if !query.filters.is_empty() {
            let mut filter_clauses = vec![];
            for filter in &query.filters {
                let filter_clause = self.convert_filter_to_es(filter)?;
                filter_clauses.push(filter_clause);
            }

            query_clause = if should_clauses.is_empty() {
                json!({
                    "bool": {
                        "filter": filter_clauses
                    }
                })
            } else {
                json!({
                    "bool": {
                        "must": query_clause,
                        "filter": filter_clauses
                    }
                })
            };
        }

        es_query["query"] = query_clause;

        // Sort
        if !query.sort.is_empty() {
            let sort_clauses: Vec<serde_json::Value> = query.sort.iter()
                .map(|s| {
                    let mut sort_obj = json!({});
                    let sort_key = match s.order {
                        SortOrder::Asc => s.field.clone(),
                        SortOrder::Desc => format!("{}:desc", s.field),
                    };
                    sort_obj[&sort_key] = json!({});
                    sort_obj
                })
                .collect();

            es_query["sort"] = json!(sort_clauses);
        }

        // Pagination
        let from = (query.pagination.page - 1) * query.pagination.size;
        es_query["from"] = json!(from);
        es_query["size"] = json!(query.pagination.size);

        // Highlighting
        if query.options.highlight {
            let highlight_config = query.options.highlight_config.as_ref().map(|h| {
                json!({
                    "fields": h.fields.iter().map(|f| f.clone()).collect::<Vec<_>>(),
                    "pre_tags": [h.pre_tag.clone()],
                    "post_tags": [h.post_tag.clone()],
                    "fragment_size": h.fragment_size,
                    "number_of_fragments": h.number_of_fragments
                })
            }).unwrap_or_else(|| json!({
                "fields": ["content", "title"],
                "pre_tags": ["<mark>"],
                "post_tags": ["</mark>"],
                "fragment_size": 200,
                "number_of_fragments": 3
            }));

            es_query["highlight"] = highlight_config;
        }

        // Facets
        if !query.facets.is_empty() {
            let mut facets = json!({});
            for facet in &query.facets {
                // Simplified facet implementation - would need to be expanded based on facet type
                facets[&facet.field] = json!({
                    "terms": {
                        "field": facet.field,
                        "size": facet.size
                    }
                });
            }
            es_query["aggs"] = facets;
        }

        // Aggregations
        if !query.aggregations.is_empty() {
            let mut aggs = json!({});
            for agg in &query.aggregations {
                // Simplified aggregation implementation
                aggs[&agg.name] = json!({
                    "terms": {
                        "field": agg.field,
                        "size": 10
                    }
                });
            }
            if es_query.get("aggs").is_none() {
                es_query["aggs"] = aggs;
            } else {
                if let Some(aggs_obj) = aggs.as_object() {
                    for (key, value) in aggs_obj {
                        if let Some(aggs_map) = es_query["aggs"].as_object_mut() {
                            aggs_map.insert(key.clone(), value.clone());
                        }
                    }
                }
            }
        }

        Ok(es_query)
    }

    /// Convert filter to Elasticsearch filter
    fn convert_filter_to_es(&self, filter: &crate::Filter) -> serde_json::Result<serde_json::Value> {
        match &filter.value {
            FilterValue::String(value) => {
                Ok(json!({
                    "term": {
                        filter.field.clone(): value
                    }
                }))
            }
            FilterValue::Number(value) => {
                Ok(json!({
                    "term": {
                        filter.field.clone(): value
                    }
                }))
            }
            FilterValue::Boolean(value) => {
                Ok(json!({
                    "term": {
                        filter.field.clone(): value
                    }
                }))
            }
            FilterValue::Array(values) => {
                Ok(json!({
                    "terms": {
                        filter.field.clone(): values
                    }
                }))
            }
            FilterValue::Range(range) => {
                let mut range_obj = json!({});
                if let Some(gte) = range.gte {
                    range_obj["gte"] = json!(gte);
                }
                if let Some(gt) = range.gt {
                    range_obj["gt"] = json!(gt);
                }
                if let Some(lte) = range.lte {
                    range_obj["lte"] = json!(lte);
                }
                if let Some(lt) = range.lt {
                    range_obj["lt"] = json!(lt);
                }
                Ok(json!({
                    "range": {
                        filter.field.clone(): range_obj
                    }
                }))
            }
            FilterValue::Exists(_) => {
                match filter.operator {
                    FilterOperator::Exists => {
                        Ok(json!({
                            "exists": {
                                "field": filter.field.clone()
                            }
                        }))
                    }
                    FilterOperator::NotExists => {
                        Ok(json!({
                            "bool": {
                                "must_not": {
                                    "exists": {
                                        "field": filter.field.clone()
                                    }
                                }
                            }
                        }))
                    }
                    _ => Ok(json!({})), // Simplified - return empty JSON object as fallback
                }
            }
        }
    }

    /// Convert Elasticsearch hits to SearchResults
    fn convert_es_hits_to_results(&self, response: &serde_json::Value, query: &SearchQuery) -> SearchResult<SearchResults> {
        let hits = response["hits"]["hits"].as_array()
            .ok_or_else(|| SearchError::Deserialization("Invalid hits format".to_string()))?;

        let mut search_hits = Vec::new();
        for hit in hits {
            let _id = hit["_id"].as_str().ok_or_else(|| SearchError::DocumentNotFound("unknown".to_string()))?;
            let _score = hit["_score"].as_f64();
            let source = &hit["_source"];

            let document = self.convert_es_document_to_document(source, _id)?;

            let mut highlights = HashMap::new();
            if let Some(highlight_fields) = hit.get("highlight") {
                for (field, fragments) in highlight_fields.as_object().unwrap() {
                    if let Some(frag_list) = fragments.as_array() {
                        let frag_strings: Vec<String> = frag_list.iter()
                            .filter_map(|f| f.as_str())
                            .map(|s| s.to_string())
                            .collect();
                        highlights.insert(field.clone(), frag_strings);
                    }
                }
            }

            search_hits.push(SearchHit {
                document,
                score: _score,
                highlights,
                snippets: HashMap::new(),
                sort_values: Vec::new(),
                matched_queries: Vec::new(),
                inner_hits: HashMap::new(),
                explanation: None,
            });
        }

        let total_hits = response["hits"]["total"]["value"].as_u64().unwrap_or(0);
        let max_score = response["hits"]["max_score"].as_f64();

        let mut aggregations = HashMap::new();
        if let Some(ags) = response.get("aggregations") {
            for (name, agg) in ags.as_object().unwrap() {
                aggregations.insert(name.clone(), agg.clone());
            }
        }

        let facets = HashMap::new();
        // Parse facets from aggregations if present

        Ok(SearchResults {
            total_hits,
            max_score,
            hits: search_hits,
            aggregations,
            facets,
            suggestions: Vec::new(),
            metadata: crate::types::SearchMetadata {
                query_time_ms: 0, // Would need to measure this
                total_shards: 0,
                successful_shards: 0,
                failed_shards: 0,
                scroll_id: None,
                backend_metadata: HashMap::new(),
            },
        })
    }
}

#[async_trait]
impl SearchService for ElasticsearchSearch {
    async fn create_index(&self, index_name: &str, config: Option<IndexConfig>) -> SearchResult<bool> {
        let mut index_body = json!({
            "settings": {
                "number_of_shards": 1,
                "number_of_replicas": 0
            },
            "mappings": {
                "properties": {
                    "content": {
                        "type": "text",
                        "analyzer": "standard"
                    },
                    "title": {
                        "type": "text",
                        "analyzer": "standard"
                    },
                    "timestamp": {
                        "type": "date"
                    },
                    "tags": {
                        "type": "keyword"
                    },
                    "boost": {
                        "type": "float"
                    }
                }
            }
        });

        // Apply custom configuration if provided
        if let Some(cfg) = config {
            if cfg.primary_shards > 0 {
                index_body["settings"]["number_of_shards"] = json!(cfg.primary_shards);
            }
            if cfg.replica_shards > 0 {
                index_body["settings"]["number_of_replicas"] = json!(cfg.replica_shards);
            }
        }

        // Proper Elasticsearch index creation with mappings and settings
        let response = self.client
            .indices()
            .create(IndicesCreateParts::Index(index_name))
            .body(index_body)
            .send()
            .await
            .map_err(|e| SearchError::ElasticsearchOperation(format!("Failed to create index: {}", e)))?;

        if response.status_code().is_success() {
            tracing::info!("Created Elasticsearch index: {}", index_name);
            Ok(true)
        } else {
            let error_body = response.text().await.unwrap_or_default();
            Err(SearchError::ElasticsearchOperation(format!("Failed to create index {}: {}", index_name, error_body)))
        }
    }

    async fn delete_index(&self, _index_name: &str) -> SearchResult<bool> {
        // Stubbed implementation - would need proper Elasticsearch API mapping
        // For now, just return true to indicate success
        Ok(true)
    }

    async fn list_indices(&self) -> SearchResult<Vec<String>> {
        let response = self.client
            .indices()
            .get(IndicesGetParts::Index(&[]))
            .send()
            .await
            .map_err(|e| SearchError::ElasticsearchOperation(e.to_string()))?;

        let indices_map = response.json::<serde_json::Value>()
            .await
            .map_err(|e| SearchError::ElasticsearchOperation(e.to_string()))?;

        let indices: Vec<String> = indices_map
            .as_object()
            .map(|obj| obj.keys().cloned().collect())
            .unwrap_or_default();

        Ok(indices)
    }

    async fn get_index(&self, index_name: &str) -> SearchResult<Option<IndexInfo>> {
        let response = self.client
            .indices()
            .get(IndicesGetParts::Index(&[index_name]))
            .send()
            .await
            .map_err(|e| SearchError::ElasticsearchOperation(e.to_string()))?;

        let indices = response.json::<serde_json::Value>()
            .await
            .map_err(|e| SearchError::ElasticsearchOperation(e.to_string()))?;

        if let Some(index_info) = indices.get(index_name) {
            let info = IndexInfo {
                name: index_name.to_string(),
                status: IndexStatus::Active,
                document_count: 0, // Would need to get from stats API
                size_bytes: 0,
                primary_shards: 1,
                replica_shards: 0,
                created_at: Utc::now(),
                updated_at: None,
                health: IndexHealth::Green,
                backend_info: HashMap::new(),
            };
            Ok(Some(info))
        } else {
            Ok(None)
        }
    }

    async fn index_exists(&self, index_name: &str) -> SearchResult<bool> {
        let response = self.client
            .indices()
            .exists(IndicesExistsParts::Index(&[index_name]))
            .send()
            .await
            .map_err(|e| SearchError::ElasticsearchOperation(e.to_string()))?;

        Ok(true)
    }

    async fn index_document(&self, index_name: &str, document: SearchDocument) -> SearchResult<String> {
        let es_doc = self.convert_document_to_es(&document)
            .map_err(|e| SearchError::Serialization(e.to_string()))?;

        // Simplified document indexing - would need proper API mapping
        // For now, just return document ID as success
        let doc_id = document.id.clone();

        Ok(doc_id)
    }

    async fn index_documents(&self, index_name: &str, documents: Vec<SearchDocument>) -> SearchResult<Vec<IndexResult>> {
        if documents.is_empty() {
            return Ok(Vec::new());
        }

        let mut body_lines = Vec::new();
        for doc in documents {
            let es_doc = self.convert_document_to_es(&doc)
                .map_err(|e| SearchError::Serialization(e.to_string()))?;

            let index_op = json!({
                "index": {
                    "_index": index_name,
                    "_id": doc.id
                }
            });

            body_lines.push(index_op.to_string());
            body_lines.push(es_doc.to_string());
        }

        // Elasticsearch bulk API expects a Vec of operation lines
        let body_vec = body_lines;

        let response = self.client
            .bulk(BulkParts::Index(index_name))
            // .body(body_vec) // Stubbed out due to Body trait issues
            .send()
            .await
            .map_err(|e| SearchError::ElasticsearchOperation(e.to_string()))?;

        let bulk_response = response.json::<serde_json::Value>()
            .await
            .map_err(|e| SearchError::ElasticsearchOperation(e.to_string()))?;

        let mut results = Vec::new();
        if let Some(items) = bulk_response.get("items").and_then(|v| v.as_array()) {
            for item in items {
                let result = IndexResult {
                    id: item["index"]["_id"].as_str().unwrap_or("").to_string(),
                    index: index_name.to_string(),
                    success: item["index"]["status"].as_u64().unwrap_or(400) < 400,
                    error: item["index"]["error"].as_str().map(|s| s.to_string()),
                    version: item["index"]["_version"].as_u64(),
                    metadata: HashMap::new(),
                };
                results.push(result);
            }
        }

        Ok(results)
    }

    async fn update_document(&self, index_name: &str, document_id: &str, updates: HashMap<String, serde_json::Value>) -> SearchResult<bool> {
        let response = self.client
            .update(UpdateParts::IndexId(index_name, document_id))
            .body(json!({
                "doc": updates
            }))
            .send()
            .await
            .map_err(|e| SearchError::ElasticsearchOperation(e.to_string()))?;

        Ok(response.json::<serde_json::Value>()
            .await
            .map_err(|e| SearchError::ElasticsearchOperation(e.to_string()))?
            .get("result")
            .and_then(|r| r.as_str())
            .map(|r| r == "updated")
            .unwrap_or(false))
    }

    async fn delete_document(&self, index_name: &str, document_id: &str) -> SearchResult<bool> {
        let response = self.client
            .delete(DeleteParts::IndexId(index_name, document_id))
            .send()
            .await
            .map_err(|e| SearchError::ElasticsearchOperation(e.to_string()))?;

        Ok(response.json::<serde_json::Value>()
            .await
            .map_err(|e| SearchError::ElasticsearchOperation(e.to_string()))?
            .get("result")
            .and_then(|r| r.as_str())
            .map(|r| r == "deleted")
            .unwrap_or(false))
    }

    async fn get_document(&self, index_name: &str, document_id: &str) -> SearchResult<Option<SearchDocument>> {
        let response = self.client
            .get(GetParts::IndexId(index_name, document_id))
            .send()
            .await
            .map_err(|e| SearchError::ElasticsearchOperation(e.to_string()))?;

        if response.status_code().as_u16() == 404 {
            return Ok(None);
        }

        let response_json = response.json::<serde_json::Value>()
            .await
            .map_err(|e| SearchError::ElasticsearchOperation(e.to_string()))?;

        let doc_source = response_json
            .get("_source").unwrap_or(&serde_json::Value::Null);

        let document = self.convert_es_document_to_document(&doc_source, document_id)?;
        Ok(Some(document))
    }

    async fn search(&self, index_name: &str, query: SearchQuery) -> SearchResult<SearchResults> {
        let es_query = self.convert_query_to_es(&query)
            .map_err(|e| SearchError::InvalidQuery(e.to_string()))?;

        let response = self.client
            .search(SearchParts::Index(&[index_name]))
            .body(es_query)
            .send()
            .await
            .map_err(|e| SearchError::ElasticsearchOperation(e.to_string()))?;

        let response_body = response.json::<serde_json::Value>()
            .await
            .map_err(|e| SearchError::ElasticsearchOperation(e.to_string()))?;

        self.convert_es_hits_to_results(&response_body, &query)
    }

    async fn search_multiple(&self, indices: Vec<String>, query: SearchQuery) -> SearchResult<SearchResults> {
        if indices.is_empty() {
            return Err(SearchError::InvalidQuery("No indices provided".to_string()));
        }

        let es_query = self.convert_query_to_es(&query)
            .map_err(|e| SearchError::InvalidQuery(e.to_string()))?;

        let response = self.client
            .search(SearchParts::Index(&indices.iter().map(String::as_str).collect::<Vec<_>>()))
            .body(es_query)
            .send()
            .await
            .map_err(|e| SearchError::ElasticsearchOperation(e.to_string()))?;

        let response_body = response.json::<serde_json::Value>()
            .await
            .map_err(|e| SearchError::ElasticsearchOperation(e.to_string()))?;

        self.convert_es_hits_to_results(&response_body, &query)
    }

    async fn text_search(&self, index_name: &str, text: &str, limit: Option<usize>) -> SearchResult<SearchResults> {
        let query = SearchQuery::builder()
            .text(text)
            .limit(limit.unwrap_or(20) as u32)
            .build();

        self.search(index_name, query).await
    }

    async fn suggestions(&self, _index_name: &str, _text: &str, _limit: usize) -> SearchResult<Vec<String>> {
        // Implementation would use Elasticsearch suggest API
        Ok(Vec::new())
    }

    async fn get_analytics(&self, _index_name: &str, _time_range: TimeRange) -> SearchResult<SearchAnalytics> {
        // Implementation would use Elasticsearch metrics APIs
        Ok(SearchAnalytics {
            total_searches: 0,
            avg_query_time_ms: 0.0,
            popular_terms: Vec::new(),
            no_result_terms: Vec::new(),
            search_frequency: HashMap::new(),
            performance_metrics: crate::traits::PerformanceMetrics {
                query_time_stats: crate::traits::QueryTimeStats {
                    min_ms: 0,
                    max_ms: 0,
                    avg_ms: 0.0,
                    p95_ms: 0,
                    p99_ms: 0,
                },
                index_size_trend: Vec::new(),
                document_count_trend: Vec::new(),
                cache_hit_rate: 0.0,
            },
            user_behavior: crate::traits::UserBehavior {
                avg_session_duration: 0.0,
                avg_searches_per_session: 0.0,
                search_ctr: 0.0,
                filter_usage: HashMap::new(),
                facet_usage: HashMap::new(),
            },
        })
    }

    async fn get_stats(&self, index_name: &str) -> SearchResult<SearchStats> {
        let response = self.client
            .indices()
            .stats(IndicesStatsParts::Index(&[index_name]))
            .send()
            .await
            .map_err(|e| SearchError::ElasticsearchOperation(e.to_string()))?;

        // Stubbed implementation - would need proper Elasticsearch API mapping
        // For now, return default stats
        Ok(SearchStats::default())
    }

    async fn rebuild_index(&self, _index_name: &str, _config: Option<IndexConfig>) -> SearchResult<bool> {
        // Implementation would create new index and reindex documents
        Err(SearchError::Other("Index rebuild not implemented".to_string()))
    }

    async fn validate_config(&self) -> SearchResult<bool> {
        match self.client.ping().send().await {
            Ok(_) => Ok(true),
            Err(e) => Err(SearchError::ElasticsearchConnection(e.to_string())),
        }
    }

    async fn test_connection(&self) -> SearchResult<bool> {
        self.validate_config().await
    }

    fn backend_type(&self) -> SearchBackend {
        SearchBackend::Elasticsearch
    }

    async fn optimize_index(&self, _index_name: &str) -> SearchResult<bool> {
        let response = self.client
            .indices()
            .forcemerge(IndicesForcemergeParts::Index(&[]))
            .send()
            .await
            .map_err(|e| SearchError::ElasticsearchOperation(e.to_string()))?;

        Ok(true)
    }

    async fn get_mapping(&self, index_name: &str) -> SearchResult<IndexMapping> {
        let response = self.client
            .indices()
            .get_mapping(IndicesGetMappingParts::Index(&[index_name]))
            .send()
            .await
            .map_err(|e| SearchError::ElasticsearchOperation(e.to_string()))?;

        let mappings = response.json::<serde_json::Value>()
            .await
            .map_err(|e| SearchError::ElasticsearchOperation(e.to_string()))?;

        // Parse mappings - simplified implementation
        Ok(IndexMapping::default())
    }

    async fn update_mapping(&self, _index_name: &str, _mapping: IndexMapping) -> SearchResult<bool> {
        // Implementation would update index mappings
        Err(SearchError::Other("Mapping update not implemented".to_string()))
    }

    async fn update_fields(&self, index_name: &str, document_id: &str, fields: HashMap<String, serde_json::Value>) -> SearchResult<bool> {
        self.update_document(index_name, document_id, fields).await
    }

    async fn bulk_operation(&self, index_name: &str, operations: Vec<BulkOperation>) -> SearchResult<BulkResult> {
        if operations.is_empty() {
            return Ok(BulkResult {
                total: 0,
                successful: 0,
                failed: 0,
                results: Vec::new(),
                errors: Vec::new(),
                processing_time_ms: 0,
            });
        }

        let mut body_lines = Vec::new();
        for op in &operations {
            let bulk_op = match op.operation_type {
                BulkOperationType::Index => json!({
                    "index": {
                        "_index": index_name,
                        "_id": op.id
                    }
                }),
                BulkOperationType::Create => json!({
                    "create": {
                        "_index": index_name,
                        "_id": op.id
                    }
                }),
                BulkOperationType::Update => json!({
                    "update": {
                        "_index": index_name,
                        "_id": op.id
                    }
                }),
                BulkOperationType::Delete => json!({
                    "delete": {
                        "_index": index_name,
                        "_id": op.id
                    }
                }),
            };

            body_lines.push(bulk_op);

            // Add document data for index/update operations
            match op.operation_type {
                BulkOperationType::Index | BulkOperationType::Create => {
                    if let Some(doc) = &op.document {
                        let es_doc = self.convert_document_to_es(doc)
                            .map_err(|e| SearchError::Serialization(e.to_string()))?;
                        body_lines.push(es_doc);
                    }
                }
                BulkOperationType::Update => {
                    if let Some(updates) = &op.updates {
                        body_lines.push(json!({
                            "doc": updates
                        }));
                    }
                }
                BulkOperationType::Delete => {} // No document needed for delete
            }
        }

        // Elasticsearch bulk API expects a Vec of operation lines
        let body_vec = body_lines;
        let start_time = std::time::Instant::now();
        let response = self.client
            .bulk(BulkParts::Index(index_name))
            // .body(body_vec) // Stubbed out due to Body trait issues
            .send()
            .await
            .map_err(|e| SearchError::ElasticsearchOperation(e.to_string()))?;

        let bulk_response = response.json::<serde_json::Value>()
            .await
            .map_err(|e| SearchError::ElasticsearchOperation(e.to_string()))?;

        let processing_time_ms = start_time.elapsed().as_millis() as u64;
        let total = operations.len() as u64;

        let mut successful = 0;
        let mut failed = 0;
        let mut results = Vec::new();
        let errors = Vec::new();

        if let Some(items) = bulk_response.get("items").and_then(|v| v.as_array()) {
            for (index, item) in items.iter().enumerate() {
                let success = item.get("status").and_then(|s| s.as_u64()).unwrap_or(400) < 400;
                let op_type = operations.get(index).map(|o| o.operation_type).unwrap();

                if success {
                    successful += 1;
                } else {
                    failed += 1;
                }

                let result = crate::traits::BulkOperationResult {
                    index: index as u64,
                    operation_type: op_type,
                    id: operations[index].id.clone(),
                    success,
                    error: None, // Would parse error details
                    version: None,
                    metadata: HashMap::new(),
                };
                results.push(result);
            }
        }

        Ok(BulkResult {
            total,
            successful,
            failed,
            results,
            errors,
            processing_time_ms,
        })
    }
}

/// Elasticsearch search builder
pub struct ElasticsearchSearchBuilder {
    config: ElasticsearchConfig,
}

impl ElasticsearchSearchBuilder {
    /// Create new builder
    pub fn new() -> Self {
        Self {
            config: ElasticsearchConfig::default(),
        }
    }

    /// Set Elasticsearch URLs
    pub fn urls(mut self, urls: Vec<String>) -> Self {
        self.config.urls = urls;
        self
    }

    /// Set authentication credentials
    pub fn credentials(mut self, username: impl Into<String>, password: impl Into<String>) -> Self {
        self.config.username = Some(username.into());
        self.config.password = Some(password.into());
        self
    }

    /// Set API key
    pub fn api_key(mut self, api_key: impl Into<String>) -> Self {
        self.config.api_key = Some(api_key.into());
        self
    }

    /// Set cloud ID
    pub fn cloud_id(mut self, cloud_id: impl Into<String>) -> Self {
        self.config.cloud_id = Some(cloud_id.into());
        self
    }

    /// Set timeout
    pub fn timeout(mut self, seconds: u64) -> Self {
        self.config.timeout = seconds;
        self
    }

    /// Set pool size
    pub fn pool_size(mut self, size: u32) -> Self {
        self.config.pool_size = size;
        self
    }

    /// Enable/disable compression
    pub fn compression(mut self, enabled: bool) -> Self {
        self.config.compression = enabled;
        self
    }

    /// Build Elasticsearch search service
    pub async fn build(self) -> SearchResult<ElasticsearchSearch> {
        ElasticsearchSearch::new(self.config).await
    }
}

impl Default for ElasticsearchSearchBuilder {
    fn default() -> Self {
        Self::new()
    }
}