pub struct MultiModalIndex {
pub config: MultiModalIndexConfig,
pub documents: HashMap<String, MultiModalDocument>,
pub projection_matrices: HashMap<(Modality, Modality), Vec<Vec<f64>>>,
/* private fields */
}Expand description
A unified index for multimodal content supporting cross-modal similarity search.
§Overview
MultiModalIndex stores MultiModalDocument entries — each carrying one or
more modality embeddings — and provides:
- Same-modality search via
same_modality_search - Cross-modal search via
search(projects query into each target modality space when a projection matrix is registered) - Configurable score fusion via
FusionStrategy - Metadata filtering on result sets
Fields§
§config: MultiModalIndexConfigIndex configuration (fusion strategy, dims, normalization).
documents: HashMap<String, MultiModalDocument>Indexed documents keyed by their string ID.
projection_matrices: HashMap<(Modality, Modality), Vec<Vec<f64>>>Learned cross-modal projection matrices.
Key (from, to) maps query space from into document space to.
Implementations§
Source§impl MultiModalIndex
impl MultiModalIndex
Sourcepub fn new(config: MultiModalIndexConfig) -> Self
pub fn new(config: MultiModalIndexConfig) -> Self
Create a new empty MultiModalIndex with the given configuration.
Sourcepub fn add_document(&mut self, doc: MultiModalDocument) -> Result<(), MmiError>
pub fn add_document(&mut self, doc: MultiModalDocument) -> Result<(), MmiError>
Index a new document.
Returns Err(MmiError::DocumentAlreadyExists) if a document with the
same ID is already present.
Sourcepub fn remove_document(&mut self, doc_id: &str) -> bool
pub fn remove_document(&mut self, doc_id: &str) -> bool
Remove a document by ID. Returns true if the document existed.
Sourcepub fn modality_coverage(&self) -> HashMap<Modality, usize>
pub fn modality_coverage(&self) -> HashMap<Modality, usize>
Count the number of documents that contain each modality.
Sourcepub fn add_projection(
&mut self,
from: Modality,
to: Modality,
matrix: Vec<Vec<f64>>,
) -> Result<(), MmiError>
pub fn add_projection( &mut self, from: Modality, to: Modality, matrix: Vec<Vec<f64>>, ) -> Result<(), MmiError>
Register a cross-modal projection matrix from modality from to to.
matrix[i][j] is the weight from input dimension j to output dimension i.
So matrix must be cross_modal_dims × from_dims.
Returns Err(MmiError::ProjectionDimsMismatch) if the matrix has zero
rows or inconsistent column counts.
Sourcepub fn cosine_similarity(a: &[f64], b: &[f64]) -> f64
pub fn cosine_similarity(a: &[f64], b: &[f64]) -> f64
Compute cosine similarity between two equal-length vectors. Returns 0.0 for zero-norm vectors.
Sourcepub fn project(embedding: &[f64], matrix: &[Vec<f64>]) -> Vec<f64>
pub fn project(embedding: &[f64], matrix: &[Vec<f64>]) -> Vec<f64>
Project an embedding vector through a matrix.
result[i] = Σ_j matrix[i][j] * embedding[j]
Output length equals the number of rows in matrix.
Sourcepub fn l2_normalize(v: &[f64]) -> Vec<f64>
pub fn l2_normalize(v: &[f64]) -> Vec<f64>
L2-normalize a vector. Returns the zero vector if the norm is zero.
Sourcepub fn search(&self, query: &CrossModalQuery) -> Vec<CrossModalResult>
pub fn search(&self, query: &CrossModalQuery) -> Vec<CrossModalResult>
Search the index using the given cross-modal query.
For each document:
- Compute per-modality cosine similarities (with optional cross-modal projection).
- Fuse scores using
config.fusion_strategy. - Filter by
query.min_similarityand metadata filters. - Return the top-
query.top_kresults sorted by combined score descending, with 1-based ranks assigned.
Sourcepub fn same_modality_search(
&self,
query: &CrossModalQuery,
) -> Vec<CrossModalResult>
pub fn same_modality_search( &self, query: &CrossModalQuery, ) -> Vec<CrossModalResult>
Convenience search restricted to query.query_modality only, with no
cross-modal projection.
This is equivalent to calling search with
target_modalities = [query_modality] and is more efficient because it
skips the projection lookup entirely.
Auto Trait Implementations§
impl Freeze for MultiModalIndex
impl RefUnwindSafe for MultiModalIndex
impl Send for MultiModalIndex
impl Sync for MultiModalIndex
impl Unpin for MultiModalIndex
impl UnsafeUnpin for MultiModalIndex
impl UnwindSafe for MultiModalIndex
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.