74 lines
2.0 KiB
Rust
74 lines
2.0 KiB
Rust
// 负责暴露全文搜索抽象并隐藏 Tantivy 的具体实现细节
|
|
|
|
mod document;
|
|
mod filter;
|
|
mod indexer;
|
|
mod query;
|
|
mod schema;
|
|
|
|
pub use indexer::SearchEngine;
|
|
pub use query::{AvailabilitySummary, SearchHit, SearchOptions, SearchPage, SearchSort};
|
|
|
|
#[derive(Debug, thiserror::Error)]
|
|
pub enum SearchError {
|
|
#[error("搜索索引操作失败: {0}")]
|
|
Tantivy(#[from] tantivy::TantivyError),
|
|
#[error("无法打开搜索索引目录: {0}")]
|
|
Directory(String),
|
|
#[error("搜索文档缺少字段 {0}")]
|
|
MissingField(&'static str),
|
|
}
|
|
|
|
impl SearchError {
|
|
pub fn is_retryable_io(&self) -> bool {
|
|
matches!(
|
|
self,
|
|
Self::Tantivy(tantivy::TantivyError::IoError(error))
|
|
if matches!(
|
|
error.kind(),
|
|
std::io::ErrorKind::PermissionDenied
|
|
| std::io::ErrorKind::WouldBlock
|
|
| std::io::ErrorKind::Interrupted
|
|
| std::io::ErrorKind::TimedOut
|
|
)
|
|
)
|
|
}
|
|
}
|
|
|
|
#[derive(Debug, thiserror::Error)]
|
|
pub enum IndexingError {
|
|
#[error(transparent)]
|
|
Search(#[from] SearchError),
|
|
#[error(transparent)]
|
|
Storage(#[from] crate::storage::StorageError),
|
|
}
|
|
|
|
impl IndexingError {
|
|
pub fn is_retryable_io(&self) -> bool {
|
|
matches!(self, Self::Search(error) if error.is_retryable_io())
|
|
}
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use std::{io, sync::Arc};
|
|
|
|
use super::{IndexingError, SearchError};
|
|
|
|
#[test]
|
|
fn permission_denied_index_error_is_retryable() {
|
|
let error = IndexingError::Search(SearchError::Tantivy(tantivy::TantivyError::IoError(
|
|
Arc::new(io::Error::from(io::ErrorKind::PermissionDenied)),
|
|
)));
|
|
assert!(error.is_retryable_io());
|
|
}
|
|
|
|
#[test]
|
|
fn invalid_query_is_not_retryable() {
|
|
let error = IndexingError::Search(SearchError::Tantivy(
|
|
tantivy::TantivyError::InvalidArgument("invalid".into()),
|
|
));
|
|
assert!(!error.is_retryable_io());
|
|
}
|
|
}
|