Skip to main content

reddb_server/application/
query.rs

1use crate::application::ports::RuntimeQueryPort;
2use crate::runtime::{
3    ContextSearchResult, RuntimeFilter, RuntimeGraphPattern, RuntimeIvfSearchResult,
4    RuntimeQueryExplain, RuntimeQueryResult, RuntimeQueryWeights, ScanCursor, ScanPage,
5};
6use crate::storage::unified::devx::SimilarResult;
7use crate::storage::unified::dsl::QueryResult as DslQueryResult;
8use crate::RedDBResult;
9
10#[derive(Debug, Clone)]
11pub struct ExecuteQueryInput {
12    pub query: String,
13}
14
15#[derive(Debug, Clone)]
16pub struct ExplainQueryInput {
17    pub query: String,
18}
19
20#[derive(Debug, Clone)]
21pub struct ScanCollectionInput {
22    pub collection: String,
23    pub offset: usize,
24    pub limit: usize,
25}
26
27#[derive(Debug, Clone)]
28pub struct SearchSimilarInput {
29    pub collection: String,
30    pub vector: Vec<f32>,
31    pub k: usize,
32    pub min_score: f32,
33    /// Optional text for semantic search (generates embedding on-the-fly)
34    pub text: Option<String>,
35    /// AI provider for semantic search (default: "openai")
36    pub provider: Option<String>,
37}
38
39#[derive(Debug, Clone)]
40pub struct SearchIvfInput {
41    pub collection: String,
42    pub vector: Vec<f32>,
43    pub k: usize,
44    pub n_lists: usize,
45    pub n_probes: Option<usize>,
46}
47
48#[derive(Debug, Clone)]
49pub struct SearchTextInput {
50    pub query: String,
51    pub collections: Option<Vec<String>>,
52    pub entity_types: Option<Vec<String>>,
53    pub capabilities: Option<Vec<String>>,
54    pub fields: Option<Vec<String>>,
55    pub limit: Option<usize>,
56    pub fuzzy: bool,
57}
58
59#[derive(Debug, Clone)]
60pub struct SearchMultimodalInput {
61    pub query: String,
62    pub collections: Option<Vec<String>>,
63    pub entity_types: Option<Vec<String>>,
64    pub capabilities: Option<Vec<String>>,
65    pub limit: Option<usize>,
66}
67
68#[derive(Debug, Clone)]
69pub struct SearchIndexInput {
70    pub index: String,
71    pub value: String,
72    pub exact: bool,
73    pub collections: Option<Vec<String>>,
74    pub entity_types: Option<Vec<String>>,
75    pub capabilities: Option<Vec<String>>,
76    pub limit: Option<usize>,
77}
78
79#[derive(Debug, Clone)]
80pub struct SearchHybridInput {
81    pub vector: Option<Vec<f32>>,
82    pub query: Option<String>,
83    pub k: Option<usize>,
84    pub collections: Option<Vec<String>>,
85    pub entity_types: Option<Vec<String>>,
86    pub capabilities: Option<Vec<String>>,
87    pub graph_pattern: Option<RuntimeGraphPattern>,
88    pub filters: Vec<RuntimeFilter>,
89    pub weights: Option<RuntimeQueryWeights>,
90    pub min_score: Option<f32>,
91    pub limit: Option<usize>,
92}
93
94#[derive(Debug, Clone)]
95pub struct SearchContextInput {
96    pub query: String,
97    pub field: Option<String>,
98    pub vector: Option<Vec<f32>>,
99    pub collections: Option<Vec<String>>,
100    pub graph_depth: Option<usize>,
101    pub graph_max_edges: Option<usize>,
102    pub max_cross_refs: Option<usize>,
103    pub follow_cross_refs: Option<bool>,
104    pub expand_graph: Option<bool>,
105    pub global_scan: Option<bool>,
106    pub reindex: Option<bool>,
107    pub limit: Option<usize>,
108    pub min_score: Option<f32>,
109}
110
111pub struct QueryUseCases<'a, P: ?Sized> {
112    runtime: &'a P,
113}
114
115impl<'a, P: RuntimeQueryPort + crate::application::ports::RuntimeEntityPort + ?Sized>
116    QueryUseCases<'a, P>
117{
118    pub fn new(runtime: &'a P) -> Self {
119        Self { runtime }
120    }
121
122    pub fn execute(&self, input: ExecuteQueryInput) -> RedDBResult<RuntimeQueryResult> {
123        self.runtime.execute_query(&input.query)
124    }
125
126    pub fn explain(&self, input: ExplainQueryInput) -> RedDBResult<RuntimeQueryExplain> {
127        self.runtime.explain_query(&input.query)
128    }
129
130    pub fn scan(&self, input: ScanCollectionInput) -> RedDBResult<ScanPage> {
131        self.runtime.scan_collection(
132            &input.collection,
133            Some(ScanCursor {
134                offset: input.offset,
135            }),
136            input.limit,
137        )
138    }
139
140    pub fn search_similar(&self, mut input: SearchSimilarInput) -> RedDBResult<Vec<SimilarResult>> {
141        // Semantic search: if text provided, generate embedding on-the-fly
142        if let Some(text) = input.text.take() {
143            if input.vector.is_empty() {
144                let provider = match input.provider.as_deref() {
145                    Some(p) => crate::ai::parse_provider(p)?,
146                    None => {
147                        let name = std::env::var("REDDB_AI_PROVIDER")
148                            .ok()
149                            .unwrap_or_else(|| "openai".to_string());
150                        crate::ai::parse_provider(&name)?
151                    }
152                };
153                // Gate non-OpenAI-compatible providers before we spend
154                // cycles resolving a key — Anthropic has no embeddings
155                // endpoint, HuggingFace uses a different wire shape,
156                // Local needs the `local-models` feature flag.
157                if matches!(provider, crate::ai::AiProvider::Local) {
158                    return Err(crate::ai::local_embeddings_unavailable_error());
159                }
160                if !provider.is_openai_compatible() {
161                    return Err(crate::RedDBError::Query(format!(
162                        "SEARCH SIMILAR: embeddings are not yet available for provider '{}'. \
163                         Use an OpenAI-compatible provider (openai, groq, ollama, openrouter, \
164                         together, venice, deepseek, or a custom base URL).",
165                        provider.token()
166                    )));
167                }
168                let api_key = self.runtime.resolve_semantic_api_key(&provider)?;
169                let model = std::env::var(format!(
170                    "REDDB_{}_EMBEDDING_MODEL",
171                    provider.token().to_ascii_uppercase()
172                ))
173                .ok()
174                .or_else(|| std::env::var("REDDB_OPENAI_EMBEDDING_MODEL").ok())
175                .filter(|v| !v.trim().is_empty())
176                .unwrap_or_else(|| provider.default_embedding_model().to_string());
177                let transport = crate::runtime::ai::transport::AiTransport::new(
178                    crate::runtime::ai::transport::AiTransportConfig::default(),
179                );
180                let request = crate::ai::OpenAiEmbeddingRequest {
181                    api_key,
182                    model,
183                    inputs: vec![text],
184                    dimensions: None,
185                    api_base: provider.resolve_api_base(),
186                };
187                let response = crate::runtime::ai::block_on_ai(async move {
188                    crate::ai::openai_embeddings_async(&transport, request).await
189                })
190                .and_then(|result| result)?;
191                input.vector = response.embeddings.into_iter().next().ok_or_else(|| {
192                    crate::RedDBError::Query("embedding API returned no vectors".to_string())
193                })?;
194            }
195        }
196        self.runtime
197            .search_similar(&input.collection, &input.vector, input.k, input.min_score)
198    }
199
200    pub fn search_ivf(&self, input: SearchIvfInput) -> RedDBResult<RuntimeIvfSearchResult> {
201        self.runtime.search_ivf(
202            &input.collection,
203            &input.vector,
204            input.k,
205            input.n_lists,
206            input.n_probes,
207        )
208    }
209
210    pub fn search_text(&self, input: SearchTextInput) -> RedDBResult<DslQueryResult> {
211        self.runtime.search_text(
212            input.query,
213            input.collections,
214            input.entity_types,
215            input.capabilities,
216            input.fields,
217            input.limit,
218            input.fuzzy,
219        )
220    }
221
222    pub fn search_multimodal(&self, input: SearchMultimodalInput) -> RedDBResult<DslQueryResult> {
223        self.runtime.search_multimodal(
224            input.query,
225            input.collections,
226            input.entity_types,
227            input.capabilities,
228            input.limit,
229        )
230    }
231
232    pub fn search_index(&self, input: SearchIndexInput) -> RedDBResult<DslQueryResult> {
233        self.runtime.search_index(
234            input.index,
235            input.value,
236            input.exact,
237            input.collections,
238            input.entity_types,
239            input.capabilities,
240            input.limit,
241        )
242    }
243
244    pub fn search_hybrid(&self, input: SearchHybridInput) -> RedDBResult<DslQueryResult> {
245        self.runtime.search_hybrid(
246            input.vector,
247            input.query,
248            input.k,
249            input.collections,
250            input.entity_types,
251            input.capabilities,
252            input.graph_pattern,
253            input.filters,
254            input.weights,
255            input.min_score,
256            input.limit,
257        )
258    }
259
260    pub fn search_context(&self, input: SearchContextInput) -> RedDBResult<ContextSearchResult> {
261        self.runtime.search_context(input)
262    }
263}