Skip to main content

storage_kit/core/query/
text.rs

1use serde::{Deserialize, Serialize};
2
3use crate::StorageLocator;
4use crate::core::ApplyDefaultLocator;
5
6use super::{ScopedQuery, TextQueryOptions};
7/// Request to run a full-text search.
8///
9/// # Examples
10///
11/// ```
12/// let request = storage_kit::database::query::TextSearchRequest {
13///     query: storage_kit::database::query::ScopedQuery::text(storage_kit::database::StorageLocator::new("documents"))
14///         .text_field("body")
15///         .limit(10),
16///     text: "rust storage".into(),
17/// };
18/// assert_eq!(request.text, "rust storage");
19/// ```
20#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
21pub struct TextSearchRequest {
22    /// Shared query scope and text options.
23    pub query: ScopedQuery<TextQueryOptions>,
24    /// Full-text query string sent to the backend.
25    pub text: String,
26}
27
28impl ApplyDefaultLocator for TextSearchRequest {
29    fn apply_default_locator(mut self, default: &StorageLocator) -> Self {
30        self.query = self.query.apply_default_locator(default);
31        self
32    }
33}
34
35impl TextSearchRequest {
36    /// Creates a text search request with all fields projected.
37    ///
38    /// # Examples
39    ///
40    /// ```
41    /// let request = storage_kit::database::query::TextSearchRequest::new(
42    ///     storage_kit::database::StorageLocator::new("documents"),
43    ///     "rust storage",
44    /// );
45    /// assert_eq!(request.text, "rust storage");
46    /// ```
47    #[must_use]
48    pub fn new(locator: StorageLocator, query: impl Into<String>) -> Self {
49        Self {
50            query: ScopedQuery::text(locator),
51            text: query.into(),
52        }
53    }
54}