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    /// 刷新索引,让已写入的文档对搜索可见。
30    ///
31    /// **不是清空索引** —— 要删除索引用 [`Self::delete_index`]。默认实现是
32    /// no-op(校验索引名后直接返回)。只有两个驱动需要覆写:
33    /// ES 走 `_refresh`,XunSearch 发 `CMD_INDEX_COMMIT`;其余后端写入即对
34    /// 查询可见,没有「刷新」这一步。
35    ///
36    /// 曾有三个驱动把它接到了清空接口上(Meilisearch `delete-all`、
37    /// Typesense 删集合、Algolia `/clear`),而 README 的生命周期示例在
38    /// update 与 search 之间调用 flush —— 照文档跑一遍就会把索引删空。
39    fn flush<'a>(&'a self, index: &'a str) -> EngineFuture<'a, ()> {
40        Box::pin(async move {
41            crate::validate_index_name(index)?;
42            Ok(())
43        })
44    }
45    fn create_index<'a>(
46        &'a self,
47        index: &'a str,
48        settings: serde_json::Value,
49    ) -> EngineFuture<'a, ()>;
50    fn delete_index<'a>(&'a self, index: &'a str) -> EngineFuture<'a, ()>;
51    /// 批量写入;默认实现逐条调用 [`Self::update`]。
52    fn update_bulk<'a>(&'a self, docs: &'a [SearchDocument]) -> EngineFuture<'a, ()> {
53        Box::pin(async move {
54            for doc in docs {
55                self.update(std::slice::from_ref(doc)).await?;
56            }
57            Ok(())
58        })
59    }
60    /// 批量删除;默认实现逐条调用 [`Self::delete_in`]。
61    fn delete_bulk<'a>(&'a self, index: &'a str, ids: &'a [String]) -> EngineFuture<'a, ()> {
62        Box::pin(async move {
63            for id in ids {
64                self.delete_in(index, std::slice::from_ref(id)).await?;
65            }
66            Ok(())
67        })
68    }
69    /// 软删除:给文档打上 `__soft_deleted: true` 标记,配合
70    /// `SearchBuilder::with_trashed()` / `only_trashed()` 过滤。
71    ///
72    /// **不带索引信息,语义因引擎而异**:`CollectionEngine` / `DatabaseEngine`
73    /// 跨所有索引标记匹配 id 的文档;HTTP 后端做不到跨索引,会返回
74    /// [`crate::ScoutError::Unsupported`](而不是静默什么都不做)——这些后端请用
75    /// [`Self::soft_delete_in`]。
76    fn soft_delete<'a>(&'a self, ids: &'a [String]) -> EngineFuture<'a, ()> {
77        let _ = ids;
78        Box::pin(async move {
79            Err(crate::ScoutError::Unsupported(
80                "soft_delete (index-less) is not supported by this engine; \
81                 use soft_delete_in(index, ids) instead"
82                    .to_string(),
83            ))
84        })
85    }
86    /// 仅对指定索引做软删除;语义与 [`Self::delete_in`] 一致。
87    fn soft_delete_in<'a>(&'a self, index: &'a str, ids: &'a [String]) -> EngineFuture<'a, ()> {
88        let _ = (index, ids);
89        Box::pin(async move {
90            Err(crate::ScoutError::Unsupported(
91                "soft_delete_in not implemented by this engine".to_string(),
92            ))
93        })
94    }
95    /// 重建索引:把 from 索引的内容复制到 to 索引。语义因引擎而异:
96    /// CollectionEngine 直接替换 to 索引的既有内容;ElasticsearchEngine 委托
97    /// 后端 `_reindex`(合并进 to,发生冲突时整批中止)。
98    ///
99    /// **例外:`DatabaseEngine` 是「移动」而不是「复制」** —— from 会被清空。
100    /// 它的表里 `id` 是全局主键(同一 id 不可能同时存在于两个索引),所以复制
101    /// 在这个 schema 下做不到。需要「from 保留」的语义就别走 database 驱动。
102    /// 其余引擎(Collection / ES / Algolia)from 保持不变。
103    fn reindex<'a>(&'a self, from: &'a str, to: &'a str) -> EngineFuture<'a, ()> {
104        let _ = (from, to);
105        Box::pin(async move {
106            Err(crate::ScoutError::Unsupported(
107                "reindex not implemented by this engine".to_string(),
108            ))
109        })
110    }
111}