Skip to main content

rust_scout/
engine.rs

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    /// 无索引信息的删除;CollectionEngine 跨索引删除,ElasticsearchEngine 仅作用于
11    /// default 索引——需要精确语义请用 [`Self::delete_in`]。
12    fn delete<'a>(&'a self, ids: &'a [String]) -> EngineFuture<'a, ()>;
13    /// 仅从指定索引删除文档。
14    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>;
30    fn flush<'a>(&'a self, index: &'a str) -> EngineFuture<'a, ()>;
31    fn create_index<'a>(
32        &'a self,
33        index: &'a str,
34        settings: serde_json::Value,
35    ) -> EngineFuture<'a, ()>;
36    fn delete_index<'a>(&'a self, index: &'a str) -> EngineFuture<'a, ()>;
37    /// 批量写入;默认实现逐条调用 [`Self::update`]。
38    fn update_bulk<'a>(&'a self, docs: &'a [SearchDocument]) -> EngineFuture<'a, ()> {
39        Box::pin(async move {
40            for doc in docs {
41                self.update(std::slice::from_ref(doc)).await?;
42            }
43            Ok(())
44        })
45    }
46    /// 批量删除;默认实现逐条调用 [`Self::delete_in`]。
47    fn delete_bulk<'a>(&'a self, index: &'a str, ids: &'a [String]) -> EngineFuture<'a, ()> {
48        Box::pin(async move {
49            for id in ids {
50                self.delete_in(index, std::slice::from_ref(id)).await?;
51            }
52            Ok(())
53        })
54    }
55    /// 软删除:给文档打上 `__soft_deleted: true` 标记,配合
56    /// `SearchBuilder::with_trashed()` / `only_trashed()` 过滤。
57    fn soft_delete<'a>(&'a self, ids: &'a [String]) -> EngineFuture<'a, ()> {
58        let _ = ids;
59        Box::pin(async move {
60            Err(crate::ScoutError::Unsupported(
61                "soft_delete not implemented by this engine".to_string(),
62            ))
63        })
64    }
65    /// 重建索引:把 from 索引的内容复制到 to 索引。语义因引擎而异:
66    /// CollectionEngine 直接替换 to 索引的既有内容;ElasticsearchEngine 委托
67    /// 后端 `_reindex`(合并进 to,发生冲突时整批中止)。
68    fn reindex<'a>(&'a self, from: &'a str, to: &'a str) -> EngineFuture<'a, ()> {
69        let _ = (from, to);
70        Box::pin(async move {
71            Err(crate::ScoutError::Unsupported(
72                "reindex not implemented by this engine".to_string(),
73            ))
74        })
75    }
76}