laravel-rust 0.1.0

Laravel for Rust: a full-stack Rust web framework, inspired by PHP Laravel
Documentation
//! Full-text search over models, with the index kept in step with model writes.
//!
//! Declare a model with `#[model(searchable)]`, mark the fields a search reads with `#[search]`,
//! and implement [`Searchable`]. `Post::search(terms)` then returns a [`Query`], which takes the
//! same `filter`, `order_by`, `latest`, and `oldest` as a model query and returns models and
//! paginators:
//!
//! ```no_run
//! use laravel::model::model;
//! use laravel::search::Searchable;
//! use laravel::time::Timestamp;
//!
//! #[model(searchable)]
//! pub struct Post {
//!     pub id: i64,
//!     #[search(weight = 3)]
//!     pub title: String,
//!     #[search(full_text)]
//!     pub body: String,
//!     #[search(filterable)]
//!     pub published: bool,
//!     #[search(filterable, sortable)]
//!     pub created_at: Timestamp,
//!     pub updated_at: Timestamp,
//! }
//!
//! impl Searchable for Post {}
//!
//! # async fn example() -> laravel::Result<()> {
//! let posts = Post::search("rust").filter(Post::published.eq(true)).paginate(15).await?;
//! # Ok(()) }
//! ```
//!
//! | Mark | The field is |
//! | --- | --- |
//! | `#[search]` | Text that contains the terms |
//! | `#[search(prefix)]` | Text that starts with the terms, such as an email |
//! | `#[search(full_text)]` | Long text with a full-text index, ranked by relevance on PostgreSQL, MySQL, and MariaDB |
//! | `#[search(weight = 3)]` | Text whose matches rank higher than other text's |
//! | `#[search(filterable)]` | A value that a search may filter on |
//! | `#[search(sortable)]` | A value that a search may order by |
//!
//! `SEARCH_ENGINE` chooses the engine, and `database`, the default, searches the model's table.
//! Laravel syncs a searchable model's index after each `create!`, `update!`, `delete`, and
//! `destroy` commits, inline or from a queued job with `SEARCH_QUEUE=true`. [`sync`] syncs the rows
//! of writes through a query, and [`without_syncing`] skips syncing. The `search:import` and
//! `search:flush` commands fill and empty a model's index.
//!
//! The [search guide](https://github.com/laravel/rust-framework/blob/0.x/docs/search.md) covers
//! each part.
//!
//! A bare `#[search]` matches text, so a field of another type is a compile error that suggests
//! `filter` or `sort`:
//!
//! ```compile_fail,E0277
//! # use laravel::model::model;
//! # use laravel::search::Searchable;
//! #[model(searchable)]
//! pub struct Post {
//!     pub id: i64,
//!     #[search]
//!     pub published: bool,
//! }
//! # impl Searchable for Post {}
//! ```
//!
//! A search doesn't match the model's key, so a text mark on the key is a compile error too:
//!
//! ```compile_fail
//! # use laravel::model::model;
//! # use laravel::search::Searchable;
//! #[model(searchable)]
//! pub struct Post {
//!     #[search]
//!     pub id: i64,
//!     #[search]
//!     pub title: String,
//! }
//! # impl Searchable for Post {}
//! ```
//!
//! So is a text mark on a search document's field named after the key:
//!
//! ```compile_fail,E0080
//! # use laravel::model::model;
//! # use laravel::search::{self, Searchable};
//! #[model(searchable(document = PostDocument))]
//! pub struct Post {
//!     pub id: i64,
//!     pub title: String,
//! }
//!
//! #[derive(search::Document)]
//! pub struct PostDocument {
//!     #[search]
//!     id: String,
//!     #[search]
//!     title: String,
//! }
//!
//! impl From<&Post> for PostDocument {
//!     fn from(post: &Post) -> Self {
//!         Self {
//!             id: post.id.to_string(),
//!             title: post.title.clone(),
//!         }
//!     }
//! }
//! # impl Searchable for Post {}
//! ```
//!
//! So is a mark on a model that isn't `searchable`, or a mark the macro doesn't know:
//!
//! ```compile_fail
//! # use laravel::model::model;
//! #[model]
//! pub struct Post {
//!     pub id: i64,
//!     #[search(filterable)]
//!     pub published: bool,
//! }
//! ```
//!
//! ```compile_fail
//! # use laravel::model::model;
//! # use laravel::search::Searchable;
//! #[model(searchable)]
//! pub struct Post {
//!     pub id: i64,
//!     #[search(rank)]
//!     pub title: String,
//! }
//! # impl Searchable for Post {}
//! ```
//!
//! A model declares its embeddings in `#[model(..)]` and keeps them in the table's `embedding`
//! column, which it doesn't map, so an `embedding` mark is a compile error, and so is
//! `embedding` without its dimensions:
//!
//! ```compile_fail
//! # use laravel::model::model;
//! # use laravel::search::Searchable;
//! #[model(searchable)]
//! pub struct Post {
//!     pub id: i64,
//!     #[search]
//!     pub title: String,
//!     #[search(embedding, dimensions = 3)]
//!     pub embedding: Option<laravel::db::Vector>,
//! }
//! # impl Searchable for Post {}
//! ```
//!
//! ```compile_fail
//! # use laravel::model::model;
//! # use laravel::search::Searchable;
//! #[model(searchable(embedding))]
//! pub struct Post {
//!     pub id: i64,
//!     #[search]
//!     pub title: String,
//! }
//! # impl Searchable for Post {}
//! ```

use crate::container::{Container, Registrar};
use crate::foundation::ServiceProvider;

/// Derive a search document's fields from its `#[search(..)]` marks, for
/// `#[model(searchable(document = PostDocument))]`.
pub use laravel_rust_model_macros::Document;
#[doc(hidden)]
pub use laravel_search::search::{
    __Checked, __Declared, __Document, __FieldType, __Registration, __SearchText, __Values, __is_key, __is_text, __text,
};
#[doc(inline)]
pub use laravel_search::search::{
    Document, Drivers, EMBEDDING_COLUMN, Engine, EngineDefinition, Field, Filter, Hits, Index, IntoSync, Kind,
    Operator, Query, Ranking, Search, Searchable, Sort, Text, sync, without_syncing,
};

pub(crate) struct SearchServiceProvider;

impl ServiceProvider for SearchServiceProvider {
    fn config(&self, config: &mut crate::config::Config, env: &crate::config::Env) {
        config.set(crate::config::search::Search::from_env(env));
    }

    fn register(&self, app: &mut Registrar) {
        laravel_search::search::register_commands(app.commands());
        laravel_search::search::listen(app.events());
    }

    async fn boot(&self, _app: &Container) -> crate::Result<()> {
        laravel_search::search::check_registrations()
    }
}