pub struct PgSparseVectorIndex { /* private fields */ }Expand description
PostgreSQL sparse vector index — the lexical half of hybrid retrieval.
Stores sparsevec(N) rows (N = vocabulary size, e.g. 250 002 for BGE-M3’s
XLM-R vocabulary) and ranks by inner product, which is how learned-lexical
weights (BGE-M3 sparse, SPLADE) are scored. Deliberately not an
AsyncVectorIndex — that trait speaks
dense Vec<f32>. Pair it with a PgVectorIndex and combine the two
result lists with Fusion.
Requires the pgvector extension ≥ 0.7 (sparsevec + sparsevec_ip_ops).
pgvector caps the number of non-zero elements it will index, so prune long
lexical vectors with SparseVector::prune_top_k before inserting.
Implementations§
Source§impl PgSparseVectorIndex
impl PgSparseVectorIndex
Sourcepub async fn new(url: &str, table: &str, dim: usize) -> Result<Self>
pub async fn new(url: &str, table: &str, dim: usize) -> Result<Self>
Connect and create the sparsevec table + HNSW inner-product index.
dim is the vocabulary size and is enforced by the sparsevec(dim)
column; vectors carrying an index ≥ dim are rejected on insert.
Sourcepub fn pool(&self) -> &PgPool
pub fn pool(&self) -> &PgPool
Underlying connection pool — see PgVectorIndex::pool.
Sourcepub async fn add(&self, vectors: &[SparseVector], ids: &[u64]) -> Result<()>
pub async fn add(&self, vectors: &[SparseVector], ids: &[u64]) -> Result<()>
Upsert sparse vectors by external ID.
Sourcepub async fn search(
&self,
query: &SparseVector,
k: usize,
) -> Result<Vec<SearchHit>>
pub async fn search( &self, query: &SparseVector, k: usize, ) -> Result<Vec<SearchHit>>
Top-k by inner product. An empty query matches nothing by definition
(its inner product with every row is zero), so it short-circuits.
Sourcepub async fn search_filtered(
&self,
query: &SparseVector,
k: usize,
allowlist: &[u64],
) -> Result<Vec<SearchHit>>
pub async fn search_filtered( &self, query: &SparseVector, k: usize, allowlist: &[u64], ) -> Result<Vec<SearchHit>>
Top-k by inner product, restricted to allowlist.
Sourcepub async fn remove_in_tx(
&self,
tx: &mut PgConnection,
ids: &[u64],
) -> Result<()>
pub async fn remove_in_tx( &self, tx: &mut PgConnection, ids: &[u64], ) -> Result<()>
Remove within a caller-provided transaction — see
PgVectorIndex::remove_in_tx. Lets a dense index, a sparse index and
the caller’s own tables be pruned atomically together.
Auto Trait Implementations§
impl !RefUnwindSafe for PgSparseVectorIndex
impl !UnwindSafe for PgSparseVectorIndex
impl Freeze for PgSparseVectorIndex
impl Send for PgSparseVectorIndex
impl Sync for PgSparseVectorIndex
impl Unpin for PgSparseVectorIndex
impl UnsafeUnpin for PgSparseVectorIndex
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
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<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.