1use async_trait::async_trait;
4use uuid::Uuid;
5
6use crate::capability::StorageCapability;
7use crate::error::StorageError;
8use crate::types::{
9 BatchWriteSummary, IndexRebuildScope, StorageResult, TextDocument, TextFilter, TextIndexStats,
10 TextSearchHit, TextSearchOptions, TextSearchRequest, TextTermStats, TextTermStatsRequest,
11};
12
13#[async_trait]
15pub trait TextSearch: Send + Sync + 'static {
16 async fn upsert_document(&self, document: TextDocument) -> StorageResult<()>;
18 async fn upsert_documents(
20 &self,
21 documents: Vec<TextDocument>,
22 ) -> StorageResult<BatchWriteSummary>;
23 async fn delete_document(&self, namespace: &str, subject_id: Uuid) -> StorageResult<bool>;
25 async fn get_document(
27 &self,
28 namespace: &str,
29 subject_id: Uuid,
30 ) -> StorageResult<Option<TextDocument>>;
31 async fn search(&self, request: TextSearchRequest) -> StorageResult<Vec<TextSearchHit>>;
33 async fn count(&self, filter: TextFilter) -> StorageResult<u64>;
35 async fn stats(&self) -> StorageResult<TextIndexStats>;
37 async fn rebuild(&self, scope: IndexRebuildScope) -> StorageResult<TextIndexStats>;
39
40 async fn search_with_options(
47 &self,
48 request: TextSearchRequest,
49 options: TextSearchOptions,
50 ) -> StorageResult<Vec<TextSearchHit>> {
51 if options == TextSearchOptions::default() {
52 self.search(request).await
53 } else {
54 Err(StorageError::Unsupported {
55 capability: StorageCapability::Text,
56 operation: "search_with_options".into(),
57 message: "this backend does not implement non-default gather options".into(),
58 })
59 }
60 }
61
62 async fn term_stats(
67 &self,
68 _request: TextTermStatsRequest,
69 ) -> StorageResult<Vec<TextTermStats>> {
70 Err(StorageError::Unsupported {
71 capability: StorageCapability::Text,
72 operation: "term_stats".into(),
73 message: "this backend does not implement term_stats".into(),
74 })
75 }
76}