1#![allow(dead_code)]
3
4use async_trait::async_trait;
5use crate::{
6 SearchResult, SearchDocument, SearchQuery, SearchStats,
7 SearchBackend, SearchResults
8};
9use std::collections::HashMap;
10
11#[async_trait]
13pub trait SearchService: Send + Sync {
14 async fn create_index(&self, index_name: &str, config: Option<IndexConfig>) -> SearchResult<bool>;
16
17 async fn delete_index(&self, index_name: &str) -> SearchResult<bool>;
19
20 async fn list_indices(&self) -> SearchResult<Vec<String>>;
22
23 async fn get_index(&self, index_name: &str) -> SearchResult<Option<IndexInfo>>;
25
26 async fn index_exists(&self, index_name: &str) -> SearchResult<bool>;
28
29 async fn index_document(&self, index_name: &str, document: SearchDocument) -> SearchResult<String>;
31
32 async fn index_documents(&self, index_name: &str, documents: Vec<SearchDocument>) -> SearchResult<Vec<IndexResult>>;
34
35 async fn update_document(&self, index_name: &str, document_id: &str, updates: HashMap<String, serde_json::Value>) -> SearchResult<bool>;
37
38 async fn delete_document(&self, index_name: &str, document_id: &str) -> SearchResult<bool>;
40
41 async fn get_document(&self, index_name: &str, document_id: &str) -> SearchResult<Option<SearchDocument>>;
43
44 async fn update_fields(&self, index_name: &str, document_id: &str, fields: HashMap<String, serde_json::Value>) -> SearchResult<bool>;
46
47 async fn search(&self, index_name: &str, query: SearchQuery) -> SearchResult<SearchResults>;
49
50 async fn search_multiple(&self, indices: Vec<String>, query: SearchQuery) -> SearchResult<SearchResults>;
52
53 async fn text_search(&self, index_name: &str, text: &str, limit: Option<usize>) -> SearchResult<SearchResults>;
55
56 async fn suggestions(&self, index_name: &str, text: &str, limit: usize) -> SearchResult<Vec<String>>;
58
59 async fn get_analytics(&self, index_name: &str, time_range: TimeRange) -> SearchResult<SearchAnalytics>;
61
62 async fn get_stats(&self, index_name: &str) -> SearchResult<SearchStats>;
64
65 async fn rebuild_index(&self, index_name: &str, config: Option<IndexConfig>) -> SearchResult<bool>;
67
68 async fn validate_config(&self) -> SearchResult<bool>;
70
71 async fn test_connection(&self) -> SearchResult<bool>;
73
74 fn backend_type(&self) -> SearchBackend;
76
77 async fn optimize_index(&self, index_name: &str) -> SearchResult<bool>;
79
80 async fn get_mapping(&self, index_name: &str) -> SearchResult<IndexMapping>;
82
83 async fn update_mapping(&self, index_name: &str, mapping: IndexMapping) -> SearchResult<bool>;
85
86 async fn bulk_operation(&self, index_name: &str, operations: Vec<BulkOperation>) -> SearchResult<BulkResult>;
88}
89
90#[derive(Debug, Clone)]
92pub struct IndexConfig {
93 pub primary_shards: u32,
95
96 pub replica_shards: u32,
98
99 pub settings: HashMap<String, serde_json::Value>,
101
102 pub mappings: IndexMapping,
104
105 pub analyzers: HashMap<String, Analyzer>,
107
108 pub aliases: Vec<String>,
110
111 pub template: Option<String>,
113}
114
115impl Default for IndexConfig {
116 fn default() -> Self {
117 Self {
118 primary_shards: 1,
119 replica_shards: 1,
120 settings: HashMap::new(),
121 mappings: IndexMapping::default(),
122 analyzers: HashMap::new(),
123 aliases: Vec::new(),
124 template: None,
125 }
126 }
127}
128
129#[derive(Debug, Clone)]
131pub struct IndexInfo {
132 pub name: String,
134
135 pub status: IndexStatus,
137
138 pub document_count: u64,
140
141 pub size_bytes: u64,
143
144 pub primary_shards: u32,
146
147 pub replica_shards: u32,
149
150 pub created_at: chrono::DateTime<chrono::Utc>,
152
153 pub updated_at: Option<chrono::DateTime<chrono::Utc>>,
155
156 pub health: IndexHealth,
158
159 pub backend_info: HashMap<String, serde_json::Value>,
161}
162
163#[derive(Debug, Clone, Copy, PartialEq, Eq)]
165pub enum IndexStatus {
166 Creating,
167 Active,
168 Deleting,
169 Closed,
170 Error,
171}
172
173#[derive(Debug, Clone, Copy, PartialEq, Eq)]
175pub enum IndexHealth {
176 Green,
177 Yellow,
178 Red,
179}
180
181#[derive(Debug, Clone, Default)]
183pub struct IndexMapping {
184 pub properties: HashMap<String, FieldMapping>,
186
187 pub dynamic: Option<DynamicMapping>,
189
190 pub date_formats: Vec<String>,
192
193 pub custom_types: HashMap<String, serde_json::Value>,
195}
196
197#[derive(Debug, Clone)]
199pub struct FieldMapping {
200 pub field_type: FieldType,
202
203 pub indexed: bool,
205
206 pub stored: bool,
208
209 pub analyzed: bool,
211
212 pub analyzer: Option<String>,
214
215 pub format: Option<String>,
217
218 pub boost: Option<f32>,
220
221 pub properties: HashMap<String, serde_json::Value>,
223}
224
225#[derive(Debug, Clone, PartialEq, Eq)]
227pub enum FieldType {
228 Text,
229 Keyword,
230 Integer,
231 Long,
232 Float,
233 Double,
234 Boolean,
235 Date,
236 Object,
237 Array,
238 Binary,
239 GeoPoint,
240 GeoShape,
241 Ip,
242 Completion,
243 Nested,
244}
245
246#[derive(Debug, Clone, Copy, PartialEq, Eq)]
248pub enum DynamicMapping {
249 True,
250 False,
251 Strict,
252}
253
254#[derive(Debug, Clone)]
256pub struct Analyzer {
257 pub tokenizer: String,
259
260 pub char_filters: Vec<String>,
262
263 pub token_filters: Vec<String>,
265
266 pub analyzer_type: Option<String>,
268}
269
270#[derive(Debug, Clone)]
272pub struct IndexResult {
273 pub id: String,
275
276 pub index: String,
278
279 pub success: bool,
281
282 pub error: Option<String>,
284
285 pub version: Option<u64>,
287
288 pub metadata: HashMap<String, serde_json::Value>,
290}
291
292#[derive(Debug, Clone)]
294pub struct BulkOperation {
295 pub operation_type: BulkOperationType,
297
298 pub id: String,
300
301 pub document: Option<SearchDocument>,
303
304 pub updates: Option<HashMap<String, serde_json::Value>>,
306
307 pub routing: Option<String>,
309
310 pub timestamp: Option<chrono::DateTime<chrono::Utc>>,
312}
313
314#[derive(Debug, Clone, Copy, PartialEq, Eq)]
316pub enum BulkOperationType {
317 Index,
318 Create,
319 Update,
320 Delete,
321}
322
323#[derive(Debug, Clone)]
325pub struct BulkResult {
326 pub total: u64,
328
329 pub successful: u64,
331
332 pub failed: u64,
334
335 pub results: Vec<BulkOperationResult>,
337
338 pub errors: Vec<BulkError>,
340
341 pub processing_time_ms: u64,
343}
344
345#[derive(Debug, Clone)]
347pub struct BulkOperationResult {
348 pub index: u64,
350
351 pub operation_type: BulkOperationType,
353
354 pub id: String,
356
357 pub success: bool,
359
360 pub error: Option<BulkError>,
362
363 pub version: Option<u64>,
365
366 pub metadata: HashMap<String, serde_json::Value>,
368}
369
370#[derive(Debug, Clone)]
372pub struct BulkError {
373 pub error_type: String,
375
376 pub reason: String,
378
379 pub details: HashMap<String, serde_json::Value>,
381}
382
383#[derive(Debug, Clone)]
385pub struct TimeRange {
386 pub start: chrono::DateTime<chrono::Utc>,
388
389 pub end: chrono::DateTime<chrono::Utc>,
391
392 pub interval: Option<TimeInterval>,
394}
395
396#[derive(Debug, Clone, Copy, PartialEq, Eq)]
398pub enum TimeInterval {
399 Minute,
400 Hour,
401 Day,
402 Week,
403 Month,
404 Year,
405}
406
407#[derive(Debug, Clone)]
409pub struct SearchAnalytics {
410 pub total_searches: u64,
412
413 pub avg_query_time_ms: f64,
415
416 pub popular_terms: Vec<SearchTerm>,
418
419 pub no_result_terms: Vec<SearchTerm>,
421
422 pub search_frequency: HashMap<String, u64>,
424
425 pub performance_metrics: PerformanceMetrics,
427
428 pub user_behavior: UserBehavior,
430}
431
432#[derive(Debug, Clone)]
434pub struct SearchTerm {
435 pub term: String,
437
438 pub count: u64,
440
441 pub avg_results: f64,
443
444 pub ctr: f64,
446}
447
448#[derive(Debug, Clone)]
450pub struct PerformanceMetrics {
451 pub query_time_stats: QueryTimeStats,
453
454 pub index_size_trend: Vec<SizePoint>,
456
457 pub document_count_trend: Vec<CountPoint>,
459
460 pub cache_hit_rate: f64,
462}
463
464#[derive(Debug, Clone)]
466pub struct QueryTimeStats {
467 pub min_ms: u64,
469
470 pub max_ms: u64,
472
473 pub avg_ms: f64,
475
476 pub p95_ms: u64,
478
479 pub p99_ms: u64,
481}
482
483#[derive(Debug, Clone)]
485pub struct SizePoint {
486 pub timestamp: chrono::DateTime<chrono::Utc>,
488
489 pub size_bytes: u64,
491}
492
493#[derive(Debug, Clone)]
495pub struct CountPoint {
496 pub timestamp: chrono::DateTime<chrono::Utc>,
498
499 pub count: u64,
501}
502
503#[derive(Debug, Clone)]
505pub struct UserBehavior {
506 pub avg_session_duration: f64,
508
509 pub avg_searches_per_session: f64,
511
512 pub search_ctr: f64,
514
515 pub filter_usage: HashMap<String, u64>,
517
518 pub facet_usage: HashMap<String, u64>,
520}