pub struct QueryExpansionEngine {
pub config: ExpansionConfig,
pub synonym_db: HashMap<String, SynonymEntry>,
pub context_terms: HashMap<String, Vec<String>>,
pub expansions_performed: u64,
pub total_terms_added: u64,
}Expand description
Production-grade query expansion engine.
§Thread Safety
The engine holds no shared state — callers that need concurrent access should
wrap it in a Mutex / RwLock (write lock only for expand_query because
it mutates the counters).
Fields§
§config: ExpansionConfigUser-supplied configuration.
synonym_db: HashMap<String, SynonymEntry>Synonym / hypernym / hyponym registry, keyed by lowercased term.
context_terms: HashMap<String, Vec<String>>Context-term map: trigger word → list of related terms.
expansions_performed: u64Total number of expand_query calls.
total_terms_added: u64Cumulative count of expansion terms added across all calls.
Implementations§
Source§impl QueryExpansionEngine
impl QueryExpansionEngine
Sourcepub fn new(config: ExpansionConfig) -> Self
pub fn new(config: ExpansionConfig) -> Self
Create a new engine with the given configuration.
Sourcepub fn add_synonym_entry(&mut self, entry: SynonymEntry)
pub fn add_synonym_entry(&mut self, entry: SynonymEntry)
Insert or replace a SynonymEntry in the synonym database.
The entry.term is lowercased before use as the map key so that
lookups are case-insensitive.
Sourcepub fn add_context_terms(&mut self, trigger: String, related: Vec<String>)
pub fn add_context_terms(&mut self, trigger: String, related: Vec<String>)
Register a list of related terms that fire when trigger appears in a
query.
If an entry for trigger already exists the new terms are appended
(deduplication is left to the caller).
Sourcepub fn expand_term(&self, term: &str) -> Vec<QeExpansionTerm>
pub fn expand_term(&self, term: &str) -> Vec<QeExpansionTerm>
Return expansion terms for a single term, respecting the current
ExpansionConfig.
The returned list is sorted by weight descending and capped at
config.max_expansions_per_term. Terms below config.min_weight are
excluded.
Sourcepub fn expand_query(
&mut self,
query: &str,
embedding: Option<Vec<f64>>,
) -> QeExpandedQuery
pub fn expand_query( &mut self, query: &str, embedding: Option<Vec<f64>>, ) -> QeExpandedQuery
Tokenise query, expand every token, and return a QeExpandedQuery.
Tokenisation splits on ASCII whitespace and the punctuation characters
., ,, !, ?, ;, :. Empty tokens are discarded.
The embedding argument, if provided, is stored verbatim in the result
(e.g. a pre-computed dense vector for the full query string).
Sourcepub fn build_search_string(&self, expanded: &QeExpandedQuery) -> String
pub fn build_search_string(&self, expanded: &QeExpandedQuery) -> String
Build a space-separated search string from all terms whose weight is at
or above config.min_weight.
The original tokens (source = ContextualExpansion) are placed first,
followed by expansion terms, both groups sorted by weight descending.
Sourcepub fn weighted_terms<'a>(
&self,
expanded: &'a QeExpandedQuery,
) -> Vec<(&'a str, f64)>
pub fn weighted_terms<'a>( &self, expanded: &'a QeExpandedQuery, ) -> Vec<(&'a str, f64)>
Return (term, weight) pairs sorted by weight descending.
Sourcepub fn coverage_stats(&self, expanded: &QeExpandedQuery) -> ExpansionStats
pub fn coverage_stats(&self, expanded: &QeExpandedQuery) -> ExpansionStats
Compute expansion statistics for a QeExpandedQuery.
Sourcepub fn engine_stats(&self) -> (u64, u64)
pub fn engine_stats(&self) -> (u64, u64)
Return (expansions_performed, total_terms_added).
Auto Trait Implementations§
impl Freeze for QueryExpansionEngine
impl RefUnwindSafe for QueryExpansionEngine
impl Send for QueryExpansionEngine
impl Sync for QueryExpansionEngine
impl Unpin for QueryExpansionEngine
impl UnsafeUnpin for QueryExpansionEngine
impl UnwindSafe for QueryExpansionEngine
Blanket Implementations§
impl<T> Allocation for T
Source§impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedExplicit<'a, E> for Twhere
T: 'a,
Source§impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
impl<'a, T, E> AsTaggedImplicit<'a, E> for Twhere
T: 'a,
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pointable for T
impl<T> Pointable for T
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.