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
//! Search service traits
#![allow(dead_code)]

use async_trait::async_trait;
use crate::{
    SearchResult, SearchDocument, SearchQuery, SearchStats,
    SearchBackend, SearchResults
};
use std::collections::HashMap;

/// Generic search service trait
#[async_trait]
pub trait SearchService: Send + Sync {
    /// Create search index
    async fn create_index(&self, index_name: &str, config: Option<IndexConfig>) -> SearchResult<bool>;

    /// Delete search index
    async fn delete_index(&self, index_name: &str) -> SearchResult<bool>;

    /// List all indices
    async fn list_indices(&self) -> SearchResult<Vec<String>>;

    /// Get index information
    async fn get_index(&self, index_name: &str) -> SearchResult<Option<IndexInfo>>;

    /// Check if index exists
    async fn index_exists(&self, index_name: &str) -> SearchResult<bool>;

    /// Index a single document
    async fn index_document(&self, index_name: &str, document: SearchDocument) -> SearchResult<String>;

    /// Index multiple documents
    async fn index_documents(&self, index_name: &str, documents: Vec<SearchDocument>) -> SearchResult<Vec<IndexResult>>;

    /// Update a document
    async fn update_document(&self, index_name: &str, document_id: &str, updates: HashMap<String, serde_json::Value>) -> SearchResult<bool>;

    /// Delete a document
    async fn delete_document(&self, index_name: &str, document_id: &str) -> SearchResult<bool>;

    /// Get document by ID
    async fn get_document(&self, index_name: &str, document_id: &str) -> SearchResult<Option<SearchDocument>>;

    /// Update document fields
    async fn update_fields(&self, index_name: &str, document_id: &str, fields: HashMap<String, serde_json::Value>) -> SearchResult<bool>;

    /// Search documents
    async fn search(&self, index_name: &str, query: SearchQuery) -> SearchResult<SearchResults>;

    /// Search across multiple indices
    async fn search_multiple(&self, indices: Vec<String>, query: SearchQuery) -> SearchResult<SearchResults>;

    /// Simple text search
    async fn text_search(&self, index_name: &str, text: &str, limit: Option<usize>) -> SearchResult<SearchResults>;

    /// Get search suggestions
    async fn suggestions(&self, index_name: &str, text: &str, limit: usize) -> SearchResult<Vec<String>>;

    /// Get search analytics
    async fn get_analytics(&self, index_name: &str, time_range: TimeRange) -> SearchResult<SearchAnalytics>;

    /// Get search statistics
    async fn get_stats(&self, index_name: &str) -> SearchResult<SearchStats>;

    /// Rebuild index
    async fn rebuild_index(&self, index_name: &str, config: Option<IndexConfig>) -> SearchResult<bool>;

    /// Validate search configuration
    async fn validate_config(&self) -> SearchResult<bool>;

    /// Test search connection
    async fn test_connection(&self) -> SearchResult<bool>;

    /// Get backend type
    fn backend_type(&self) -> SearchBackend;

    /// Optimize index for search performance
    async fn optimize_index(&self, index_name: &str) -> SearchResult<bool>;

    /// Get mapping for index
    async fn get_mapping(&self, index_name: &str) -> SearchResult<IndexMapping>;

    /// Update index mapping
    async fn update_mapping(&self, index_name: &str, mapping: IndexMapping) -> SearchResult<bool>;

    /// Bulk operation (index, update, delete)
    async fn bulk_operation(&self, index_name: &str, operations: Vec<BulkOperation>) -> SearchResult<BulkResult>;
}

/// Search index configuration
#[derive(Debug, Clone)]
pub struct IndexConfig {
    /// Number of primary shards
    pub primary_shards: u32,

    /// Number of replica shards
    pub replica_shards: u32,

    /// Index settings
    pub settings: HashMap<String, serde_json::Value>,

    /// Field mappings
    pub mappings: IndexMapping,

    /// Custom analyzers
    pub analyzers: HashMap<String, Analyzer>,

    /// Index aliases
    pub aliases: Vec<String>,

    /// Index template
    pub template: Option<String>,
}

impl Default for IndexConfig {
    fn default() -> Self {
        Self {
            primary_shards: 1,
            replica_shards: 1,
            settings: HashMap::new(),
            mappings: IndexMapping::default(),
            analyzers: HashMap::new(),
            aliases: Vec::new(),
            template: None,
        }
    }
}

/// Index information
#[derive(Debug, Clone)]
pub struct IndexInfo {
    /// Index name
    pub name: String,

    /// Index status
    pub status: IndexStatus,

    /// Document count
    pub document_count: u64,

    /// Index size in bytes
    pub size_bytes: u64,

    /// Primary shards
    pub primary_shards: u32,

    /// Replica shards
    pub replica_shards: u32,

    /// Creation timestamp
    pub created_at: chrono::DateTime<chrono::Utc>,

    /// Last updated timestamp
    pub updated_at: Option<chrono::DateTime<chrono::Utc>>,

    /// Health status
    pub health: IndexHealth,

    /// Backend-specific info
    pub backend_info: HashMap<String, serde_json::Value>,
}

/// Index status
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum IndexStatus {
    Creating,
    Active,
    Deleting,
    Closed,
    Error,
}

/// Index health
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum IndexHealth {
    Green,
    Yellow,
    Red,
}

/// Index mapping
#[derive(Debug, Clone, Default)]
pub struct IndexMapping {
    /// Field mappings
    pub properties: HashMap<String, FieldMapping>,

    /// Dynamic mapping rules
    pub dynamic: Option<DynamicMapping>,

    /// Date formats
    pub date_formats: Vec<String>,

    /// Custom field types
    pub custom_types: HashMap<String, serde_json::Value>,
}

/// Field mapping
#[derive(Debug, Clone)]
pub struct FieldMapping {
    /// Field type
    pub field_type: FieldType,

    /// Whether field is indexed
    pub indexed: bool,

    /// Whether field is stored
    pub stored: bool,

    /// Whether field is analyzed
    pub analyzed: bool,

    /// Field analyzer
    pub analyzer: Option<String>,

    /// Field format
    pub format: Option<String>,

    /// Field boost
    pub boost: Option<f32>,

    /// Field properties
    pub properties: HashMap<String, serde_json::Value>,
}

/// Field type
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum FieldType {
    Text,
    Keyword,
    Integer,
    Long,
    Float,
    Double,
    Boolean,
    Date,
    Object,
    Array,
    Binary,
    GeoPoint,
    GeoShape,
    Ip,
    Completion,
    Nested,
}

/// Dynamic mapping
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DynamicMapping {
    True,
    False,
    Strict,
}

/// Text analyzer
#[derive(Debug, Clone)]
pub struct Analyzer {
    /// Tokenizer
    pub tokenizer: String,

    /// Character filters
    pub char_filters: Vec<String>,

    /// Token filters
    pub token_filters: Vec<String>,

    /// Analyzer type
    pub analyzer_type: Option<String>,
}

/// Index operation result
#[derive(Debug, Clone)]
pub struct IndexResult {
    /// Document ID
    pub id: String,

    /// Index name
    pub index: String,

    /// Operation status
    pub success: bool,

    /// Error message (if failed)
    pub error: Option<String>,

    /// Version number
    pub version: Option<u64>,

    /// Operation metadata
    pub metadata: HashMap<String, serde_json::Value>,
}

/// Bulk operation
#[derive(Debug, Clone)]
pub struct BulkOperation {
    /// Operation type
    pub operation_type: BulkOperationType,

    /// Document ID
    pub id: String,

    /// Document data (for index/update operations)
    pub document: Option<SearchDocument>,

    /// Updates (for update operations)
    pub updates: Option<HashMap<String, serde_json::Value>>,

    /// Routing value
    pub routing: Option<String>,

    /// Timestamp
    pub timestamp: Option<chrono::DateTime<chrono::Utc>>,
}

/// Bulk operation type
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum BulkOperationType {
    Index,
    Create,
    Update,
    Delete,
}

/// Bulk operation result
#[derive(Debug, Clone)]
pub struct BulkResult {
    /// Total operations
    pub total: u64,

    /// Successful operations
    pub successful: u64,

    /// Failed operations
    pub failed: u64,

    /// Operation results
    pub results: Vec<BulkOperationResult>,

    /// Errors
    pub errors: Vec<BulkError>,

    /// Processing time in milliseconds
    pub processing_time_ms: u64,
}

/// Bulk operation result
#[derive(Debug, Clone)]
pub struct BulkOperationResult {
    /// Operation index
    pub index: u64,

    /// Operation type
    pub operation_type: BulkOperationType,

    /// Document ID
    pub id: String,

    /// Success status
    pub success: bool,

    /// Error details (if failed)
    pub error: Option<BulkError>,

    /// Version number
    pub version: Option<u64>,

    /// Result metadata
    pub metadata: HashMap<String, serde_json::Value>,
}

/// Bulk error
#[derive(Debug, Clone)]
pub struct BulkError {
    /// Error type
    pub error_type: String,

    /// Error reason
    pub reason: String,

    /// Error details
    pub details: HashMap<String, serde_json::Value>,
}

/// Time range for analytics
#[derive(Debug, Clone)]
pub struct TimeRange {
    /// Start time
    pub start: chrono::DateTime<chrono::Utc>,

    /// End time
    pub end: chrono::DateTime<chrono::Utc>,

    /// Time interval
    pub interval: Option<TimeInterval>,
}

/// Time interval
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum TimeInterval {
    Minute,
    Hour,
    Day,
    Week,
    Month,
    Year,
}

/// Search analytics
#[derive(Debug, Clone)]
pub struct SearchAnalytics {
    /// Total number of searches
    pub total_searches: u64,

    /// Average query time in milliseconds
    pub avg_query_time_ms: f64,

    /// Popular search terms
    pub popular_terms: Vec<SearchTerm>,

    /// No-result searches
    pub no_result_terms: Vec<SearchTerm>,

    /// Search frequency by time
    pub search_frequency: HashMap<String, u64>,

    /// Performance metrics
    pub performance_metrics: PerformanceMetrics,

    /// User behavior analytics
    pub user_behavior: UserBehavior,
}

/// Search term
#[derive(Debug, Clone)]
pub struct SearchTerm {
    /// Term text
    pub term: String,

    /// Search count
    pub count: u64,

    /// Average result count
    pub avg_results: f64,

    /// Click-through rate
    pub ctr: f64,
}

/// Performance metrics
#[derive(Debug, Clone)]
pub struct PerformanceMetrics {
    /// Query time statistics
    pub query_time_stats: QueryTimeStats,

    /// Index size over time
    pub index_size_trend: Vec<SizePoint>,

    /// Document count over time
    pub document_count_trend: Vec<CountPoint>,

    /// Cache hit rate
    pub cache_hit_rate: f64,
}

/// Query time statistics
#[derive(Debug, Clone)]
pub struct QueryTimeStats {
    /// Minimum query time
    pub min_ms: u64,

    /// Maximum query time
    pub max_ms: u64,

    /// Average query time
    pub avg_ms: f64,

    /// 95th percentile query time
    pub p95_ms: u64,

    /// 99th percentile query time
    pub p99_ms: u64,
}

/// Size point over time
#[derive(Debug, Clone)]
pub struct SizePoint {
    /// Timestamp
    pub timestamp: chrono::DateTime<chrono::Utc>,

    /// Size in bytes
    pub size_bytes: u64,
}

/// Count point over time
#[derive(Debug, Clone)]
pub struct CountPoint {
    /// Timestamp
    pub timestamp: chrono::DateTime<chrono::Utc>,

    /// Count
    pub count: u64,
}

/// User behavior analytics
#[derive(Debug, Clone)]
pub struct UserBehavior {
    /// Average session duration
    pub avg_session_duration: f64,

    /// Average searches per session
    pub avg_searches_per_session: f64,

    /// Search result click-through rate
    pub search_ctr: f64,

    /// Filter usage statistics
    pub filter_usage: HashMap<String, u64>,

    /// Facet usage statistics
    pub facet_usage: HashMap<String, u64>,
}