1use std::future::Future;
2use std::pin::Pin;
3
4use crate::{Result, SearchBuilder, SearchDocument, SearchResult};
5
6pub type EngineFuture<'a, T> = Pin<Box<dyn Future<Output = Result<T>> + Send + 'a>>;
7
8pub trait Engine: Send + Sync {
9 fn update<'a>(&'a self, docs: &'a [SearchDocument]) -> EngineFuture<'a, ()>;
10 fn delete<'a>(&'a self, ids: &'a [String]) -> EngineFuture<'a, ()>;
13 fn delete_in<'a>(&'a self, index: &'a str, ids: &'a [String]) -> EngineFuture<'a, ()> {
15 let _ = (index, ids);
16 Box::pin(async move {
17 Err(crate::ScoutError::Unsupported(
18 "delete_in not implemented by this engine".to_string(),
19 ))
20 })
21 }
22 fn search<'a>(&'a self, builder: &'a SearchBuilder) -> EngineFuture<'a, SearchResult>;
23 fn paginate<'a>(
24 &'a self,
25 builder: &'a SearchBuilder,
26 page: usize,
27 per_page: usize,
28 ) -> EngineFuture<'a, SearchResult>;
29 fn map_ids(&self, result: &SearchResult) -> Vec<String> {
32 result.ids()
33 }
34 fn flush<'a>(&'a self, index: &'a str) -> EngineFuture<'a, ()>;
35 fn create_index<'a>(
36 &'a self,
37 index: &'a str,
38 settings: serde_json::Value,
39 ) -> EngineFuture<'a, ()>;
40 fn delete_index<'a>(&'a self, index: &'a str) -> EngineFuture<'a, ()>;
41 fn update_bulk<'a>(&'a self, docs: &'a [SearchDocument]) -> EngineFuture<'a, ()> {
43 Box::pin(async move {
44 for doc in docs {
45 self.update(std::slice::from_ref(doc)).await?;
46 }
47 Ok(())
48 })
49 }
50 fn delete_bulk<'a>(&'a self, index: &'a str, ids: &'a [String]) -> EngineFuture<'a, ()> {
52 Box::pin(async move {
53 for id in ids {
54 self.delete_in(index, std::slice::from_ref(id)).await?;
55 }
56 Ok(())
57 })
58 }
59 fn soft_delete<'a>(&'a self, ids: &'a [String]) -> EngineFuture<'a, ()> {
67 let _ = ids;
68 Box::pin(async move {
69 Err(crate::ScoutError::Unsupported(
70 "soft_delete (index-less) is not supported by this engine; \
71 use soft_delete_in(index, ids) instead"
72 .to_string(),
73 ))
74 })
75 }
76 fn soft_delete_in<'a>(&'a self, index: &'a str, ids: &'a [String]) -> EngineFuture<'a, ()> {
78 let _ = (index, ids);
79 Box::pin(async move {
80 Err(crate::ScoutError::Unsupported(
81 "soft_delete_in not implemented by this engine".to_string(),
82 ))
83 })
84 }
85 fn reindex<'a>(&'a self, from: &'a str, to: &'a str) -> EngineFuture<'a, ()> {
89 let _ = (from, to);
90 Box::pin(async move {
91 Err(crate::ScoutError::Unsupported(
92 "reindex not implemented by this engine".to_string(),
93 ))
94 })
95 }
96}