pub struct VectorQueryExpander { /* private fields */ }Expand description
Vector-based semantic query expander.
Maintains a synonym registry where each term maps to a list of
(synonym_string, embedding_vector) pairs. Given a query term and its
embedding, the expander finds synonyms whose cosine similarity exceeds
VectorExpanderConfig::similarity_threshold, weights them by
similarity × decay^rank, and returns a combined (L2-normalised) vector.
Implementations§
Source§impl VectorQueryExpander
impl VectorQueryExpander
Sourcepub fn new(config: VectorExpanderConfig) -> Self
pub fn new(config: VectorExpanderConfig) -> Self
Create a new expander with the given configuration and an empty synonym registry.
Sourcepub fn add_synonym(&mut self, term: &str, synonym: &str, vector: Vec<f64>)
pub fn add_synonym(&mut self, term: &str, synonym: &str, vector: Vec<f64>)
Register a synonym for term together with its embedding vector.
Sourcepub fn expand_query(
&mut self,
query_term: &str,
query_vector: &[f64],
) -> VectorExpandedQuery
pub fn expand_query( &mut self, query_term: &str, query_vector: &[f64], ) -> VectorExpandedQuery
Expand a query by finding matching synonyms, weighting them, and producing a combined vector.
Steps:
- Look up synonyms for
query_term. - Compute cosine similarity of each synonym vector to
query_vector. - Filter by
similarity_threshold. - Sort descending by similarity.
- Take at most
max_expansions. - Weight each expansion by
similarity × decay^rank(rank starts at 0). - Combine vectors via weighted average and L2-normalise.
Sourcepub fn combine_vectors(
original: &[f64],
expansions: &[(Vec<f64>, f64)],
) -> Vec<f64>
pub fn combine_vectors( original: &[f64], expansions: &[(Vec<f64>, f64)], ) -> Vec<f64>
Weighted average of original and expansions, then L2-normalise.
Formula: (original + Σ(weight_i × expansion_i)) / (1 + Σ weight_i)
Sourcepub fn cosine_similarity(a: &[f64], b: &[f64]) -> f64
pub fn cosine_similarity(a: &[f64], b: &[f64]) -> f64
Cosine similarity between two vectors.
Returns 0.0 when either vector has zero magnitude.
Sourcepub fn remove_synonym(&mut self, term: &str, synonym: &str) -> bool
pub fn remove_synonym(&mut self, term: &str, synonym: &str) -> bool
Remove a specific synonym for term. Returns true if found and removed.
Sourcepub fn synonym_count(&self, term: &str) -> usize
pub fn synonym_count(&self, term: &str) -> usize
Number of synonyms registered for term (0 if unknown).
Sourcepub fn clear_synonyms(&mut self)
pub fn clear_synonyms(&mut self)
Remove all synonym entries.
Sourcepub fn stats(&self) -> VectorExpanderStats
pub fn stats(&self) -> VectorExpanderStats
Return a snapshot of the current runtime statistics.
Auto Trait Implementations§
impl Freeze for VectorQueryExpander
impl RefUnwindSafe for VectorQueryExpander
impl Send for VectorQueryExpander
impl Sync for VectorQueryExpander
impl Unpin for VectorQueryExpander
impl UnsafeUnpin for VectorQueryExpander
impl UnwindSafe for VectorQueryExpander
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.