pub struct SparseVector { /* private fields */ }Expand description
A sparse vector — indices[i] carries weight values[i].
Indices are 0-based vocabulary positions, as emitted by the model. Backends
convert to their own convention on write (pgvector’s text form is 1-based).
Elements are kept sorted by index with zero weights removed, which is what
sparsevec expects and what makes intersection cheap.
Implementations§
Source§impl SparseVector
impl SparseVector
Sourcepub fn new(indices: Vec<u32>, values: Vec<f32>) -> Option<Self>
pub fn new(indices: Vec<u32>, values: Vec<f32>) -> Option<Self>
Build from parallel indices / values.
Zero weights are dropped and the remaining elements are sorted by index.
Returns None when the two slices disagree in length — a mismatch means
the caller lost the pairing and silently truncating would corrupt scores.
Both +0.0 and -0.0 are dropped: v != 0.0 is false for both
(-0.0 == 0.0 per IEEE-754), so a signed zero cannot sneak through and
later tie-break against a genuinely nonzero weight in
prune_top_k.
Sourcepub fn prune_top_k(&self, k: usize) -> SparseVector
pub fn prune_top_k(&self, k: usize) -> SparseVector
Keep only the k highest-magnitude weights, still sorted by index.
Lexical vectors from models like BGE-M3 carry a long tail of near-zero
weights that costs storage and search time while contributing almost
nothing to the score. pgvector also refuses to build an HNSW index over
a sparsevec beyond a bounded number of non-zero elements (1,000 as of
pgvector 0.8), so long vectors must be pruned before indexing.
Trait Implementations§
Source§impl Clone for SparseVector
impl Clone for SparseVector
Source§fn clone(&self) -> SparseVector
fn clone(&self) -> SparseVector
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for SparseVector
impl Debug for SparseVector
Source§impl Default for SparseVector
impl Default for SparseVector
Source§fn default() -> SparseVector
fn default() -> SparseVector
Source§impl PartialEq for SparseVector
impl PartialEq for SparseVector
impl StructuralPartialEq for SparseVector
Auto Trait Implementations§
impl Freeze for SparseVector
impl RefUnwindSafe for SparseVector
impl Send for SparseVector
impl Sync for SparseVector
impl Unpin for SparseVector
impl UnsafeUnpin for SparseVector
impl UnwindSafe for SparseVector
Blanket Implementations§
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> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> ErasedDestructor for Twhere
T: 'static,
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> IntoRequest<T> for T
impl<T> IntoRequest<T> for T
Source§fn into_request(self) -> Request<T>
fn into_request(self) -> Request<T>
T in a tonic::RequestSource§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> PolicyExt for Twhere
T: ?Sized,
impl<T> Read<Exclusive, BecauseExclusive> for Twhere
T: ?Sized,
Source§impl<R, P> ReadPrimitive<R> for P
impl<R, P> ReadPrimitive<R> for P
Source§fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
fn read_from_little_endian(read: &mut R) -> Result<Self, Error>
ReadEndian::read_from_little_endian().impl<T> Scalar for T
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.