searus/
lib.rs

1//! # Searus: A Flexible, Multi-Modal Search Engine Library for Rust
2//!
3//! Searus is a powerful, adaptable search library designed to provide a unified interface for various search strategies. Whether you need full-text search, semantic understanding, tag-based filtering, or fuzzy matching, Searus offers a cohesive solution. It is particularly well-suited for applications that require combining multiple search modalities to deliver nuanced and relevant results.
4//!
5//! The library is built with flexibility in mind, allowing you to compose different searchers, define custom scoring rules, and extend functionality with hooks into the search lifecycle.
6//!
7//! ## Key Features
8//!
9//! - **Multi-Modal Search**: Combine different searchers (e.g., `SemanticSearch`, `TaggedSearch`, `FuzzySearch`) in a single query.
10//! - **Configurable Ranking**: Use `SemanticRules` to define field-specific weights, priorities, and search methods (like BM25 or exact matching).
11//! - **Extensible Architecture**: Implement custom `Searcher` traits or use `SearusExtension` to modify queries and results.
12//! - **Filtering**: Apply complex, field-based filters to refine search results before or after the search process.
13//! - **Tag Relationship Trees (TRT)**: Expand tag-based queries to include related tags, enabling more comprehensive searches.
14//! - **Parallel Execution**: Speed up searches with the optional `parallel` feature flag.
15//!
16//! ## Feature Flags
17//!
18//! - `semantic` (default): Enables semantic search capabilities (BM25).
19//! - `fuzzy` (default): Enables fuzzy search capabilities.
20//! - `tagged` (default): Enables tag-based search capabilities.
21//! - `parallel`: Enables parallel execution using `rayon`.
22//! - `serde`: Enables serialization support (required for most features).
23//!
24//! ## Getting Started
25//!
26//! Here's a quick example of how to set up a semantic search engine for a collection of blog posts.
27//!
28//! First, add Searus to your `Cargo.toml`:
29//!
30//! ```toml
31//! [dependencies]
32//! searus = "0.1.0" # Replace with the latest version
33//! serde = { version = "1.0", features = ["derive"] }
34//! ```
35//!
36//! Now, you can create a search engine and query your data:
37//!
38//! ```rust
39//! use searus::prelude::*;
40//! use searus::searchers::SemanticSearch;
41//! use serde::{Deserialize, Serialize};
42//!
43//! // Define the data structure to be searched.
44//! // It must derive `Serialize`, `Deserialize`, and `Clone`.
45//! #[derive(Debug, Clone, Serialize, Deserialize)]
46//! struct Post {
47//!     id: u32,
48//!     title: String,
49//!     content: String,
50//!     author: String,
51//! }
52//!
53//! fn main() {
54//!     // 1. Create a collection of documents to search.
55//!     let posts = vec![
56//!         Post {
57//!             id: 1,
58//!             title: "Getting Started with Rust".to_string(),
59//!             content: "Rust is a systems programming language.".to_string(),
60//!             author: "Alice".to_string(),
61//!         },
62//!         Post {
63//!             id: 2,
64//!             title: "Building a Search Engine in Rust".to_string(),
65//!             content: "Learn how to build a search engine.".to_string(),
66//!             author: "Bob".to_string(),
67//!         },
68//!     ];
69//!
70//!     // 2. Define semantic rules for searching fields.
71//!     // Here, we prioritize matches in 'title' over 'content'.
72//!     let rules = SemanticRules::builder()
73//!         .field("title", FieldRule::bm25().priority(2))
74//!         .field("content", FieldRule::bm25().priority(1))
75//!         .build();
76//!
77//!     // 3. Create a searcher. `SemanticSearch` is great for text-based queries.
78//!     let semantic_searcher = SemanticSearch::new(rules);
79//!
80//!     // 4. Build the search engine and register the searcher.
81//!     let engine = SearusEngine::builder()
82//!         .with(Box::new(semantic_searcher))
83//!         .build();
84//!
85//!     // 5. Construct a query.
86//!     let query = Query::builder()
87//!         .text("rust programming")
88//!         .options(SearchOptions::default().limit(1))
89//!         .build();
90//!
91//!     // 6. Execute the search.
92//!     let results = engine.search(&posts, &query);
93//!
94//!     // 7. Print the results.
95//!     println!("Query: \"rust programming\"");
96//!     for result in results {
97//!         println!(
98//!             "Found post: \"{}\" by {} (Score: {:.3})",
99//!             result.item.title, result.item.author, result.score
100//!         );
101//!     }
102//! }
103//! ```
104//!
105//! This example demonstrates the basic workflow: defining data, configuring rules, building an engine, and executing a query. For more advanced use cases, such as combining multiple searchers or using filters, see the documentation for `SearusEngine`, `Query`, and the specific `Searcher` implementations.
106
107/// Provides the `SearchContext`, which holds the state of the items being searched.
108pub mod context;
109/// Contains components for generating embeddings, used in vector or semantic search.
110/// (Currently experimental).
111pub mod embeddings;
112/// The core `SearusEngine`, which orchestrates the search process across multiple searchers.
113pub mod engine;
114/// Defines the `SearusExtension` trait for hooking into the search lifecycle to modify queries or results.
115pub mod extension;
116/// Provides powerful filtering capabilities with `FilterExpr` to refine search results.
117pub mod filter;
118/// Defines indexing structures for optimizing search performance.
119/// (Currently includes in-memory adapters).
120pub mod index;
121/// Implements the `SemanticRules` and `FieldRule` for fine-grained control over text-based searching.
122pub mod rules;
123/// Contains the fundamental `Searcher` trait and the multi-searcher implementation.
124pub mod searcher;
125/// A collection of built-in `Searcher` implementations, including `SemanticSearch`, `TaggedSearch`, and `FuzzySearch`.
126pub mod searchers;
127/// Defines the core data structures used throughout the library, such as `Query`, `SearusMatch`, and `SearchOptions`.
128pub mod types;
129
130pub mod prelude {
131  //! Convenient re-exports for common types and traits.
132
133  pub use crate::context::*;
134  pub use crate::embeddings::*;
135  pub use crate::engine::*;
136  pub use crate::extension::*;
137  pub use crate::filter::*;
138  pub use crate::index::*;
139  pub use crate::rules::*;
140  pub use crate::searcher::*;
141  pub use crate::searchers::*;
142  pub use crate::types::*;
143}