pub enum RankExpr {
Absolute(Box<RankExpr>),
Division {
left: Box<RankExpr>,
right: Box<RankExpr>,
},
Exponentiation(Box<RankExpr>),
Knn {
query: QueryVector,
key: Key,
limit: u32,
default: Option<f32>,
return_rank: bool,
},
Logarithm(Box<RankExpr>),
Maximum(Vec<RankExpr>),
Minimum(Vec<RankExpr>),
Multiplication(Vec<RankExpr>),
Subtraction {
left: Box<RankExpr>,
right: Box<RankExpr>,
},
Summation(Vec<RankExpr>),
Value(f32),
}Expand description
A ranking expression for scoring and ordering search results.
Ranking expressions determine which documents appear in results and their order. Lower scores indicate better matches (distance-based scoring).
§Variants
§Knn - K-Nearest Neighbor Search
The primary ranking method for vector similarity search.
use chroma_types::operator::{RankExpr, QueryVector, Key};
let rank = RankExpr::Knn {
query: QueryVector::Dense(vec![0.1, 0.2, 0.3]),
key: Key::Embedding,
limit: 100, // Consider top 100 candidates
default: None, // No default score for missing documents
return_rank: false, // Return distances, not rank positions
};§Value - Constant
Represents a constant score.
use chroma_types::operator::RankExpr;
let rank = RankExpr::Value(0.5);§Arithmetic Operations
Combine ranking expressions using standard operators (+, -, *, /).
use chroma_types::operator::{RankExpr, QueryVector, Key};
let knn1 = RankExpr::Knn {
query: QueryVector::Dense(vec![0.1, 0.2, 0.3]),
key: Key::Embedding,
limit: 100,
default: None,
return_rank: false,
};
let knn2 = RankExpr::Knn {
query: QueryVector::Dense(vec![0.2, 0.3, 0.4]),
key: Key::field("other_embedding"),
limit: 100,
default: None,
return_rank: false,
};
// Weighted combination: 70% knn1 + 30% knn2
let combined = knn1 * 0.7 + knn2 * 0.3;
// Normalized
let normalized = combined / 2.0;§Mathematical Functions
Apply mathematical transformations to scores.
use chroma_types::operator::{RankExpr, QueryVector, Key};
let knn = RankExpr::Knn {
query: QueryVector::Dense(vec![0.1, 0.2, 0.3]),
key: Key::Embedding,
limit: 100,
default: None,
return_rank: false,
};
// Exponential - amplifies differences
let amplified = knn.clone().exp();
// Logarithm - compresses range (add constant to avoid log(0))
let compressed = (knn.clone() + 1.0).log();
// Absolute value
let absolute = knn.clone().abs();
// Min/Max - clamping
let clamped = knn.min(1.0).max(0.0);§Examples
§Basic vector search
use chroma_types::operator::{RankExpr, QueryVector, Key};
let rank = RankExpr::Knn {
query: QueryVector::Dense(vec![0.1, 0.2, 0.3]),
key: Key::Embedding,
limit: 100,
default: None,
return_rank: false,
};§Hybrid search with weighted combination
use chroma_types::operator::{RankExpr, QueryVector, Key};
let dense = RankExpr::Knn {
query: QueryVector::Dense(vec![0.1, 0.2, 0.3]),
key: Key::Embedding,
limit: 200,
default: None,
return_rank: false,
};
let sparse = RankExpr::Knn {
query: QueryVector::Dense(vec![0.1, 0.2, 0.3]), // Use sparse in practice
key: Key::field("sparse_embedding"),
limit: 200,
default: None,
return_rank: false,
};
// 70% semantic + 30% keyword
let hybrid = dense * 0.7 + sparse * 0.3;§Reciprocal Rank Fusion (RRF)
Use the rrf() function for combining rankings with different score scales.
use chroma_types::operator::{RankExpr, QueryVector, Key, rrf};
let dense = RankExpr::Knn {
query: QueryVector::Dense(vec![0.1, 0.2, 0.3]),
key: Key::Embedding,
limit: 200,
default: None,
return_rank: true, // RRF requires rank positions
};
let sparse = RankExpr::Knn {
query: QueryVector::Dense(vec![0.1, 0.2, 0.3]),
key: Key::field("sparse_embedding"),
limit: 200,
default: None,
return_rank: true, // RRF requires rank positions
};
let rrf_rank = rrf(
vec![dense, sparse],
Some(60), // k parameter (smoothing)
Some(vec![0.7, 0.3]), // weights
false, // normalize weights
).unwrap();Variants§
Absolute(Box<RankExpr>)
Division
Exponentiation(Box<RankExpr>)
Knn
Logarithm(Box<RankExpr>)
Maximum(Vec<RankExpr>)
Minimum(Vec<RankExpr>)
Multiplication(Vec<RankExpr>)
Subtraction
Summation(Vec<RankExpr>)
Value(f32)
Implementations§
Source§impl RankExpr
impl RankExpr
pub fn default_knn_key() -> Key
pub fn default_knn_limit() -> u32
pub fn knn_queries(&self) -> Vec<KnnQuery>
Sourcepub fn exp(self) -> RankExpr
pub fn exp(self) -> RankExpr
Applies exponential transformation: e^rank.
Amplifies differences between scores.
§Examples
use chroma_types::operator::{RankExpr, QueryVector, Key};
let knn = RankExpr::Knn {
query: QueryVector::Dense(vec![0.1, 0.2, 0.3]),
key: Key::Embedding,
limit: 100,
default: None,
return_rank: false,
};
let amplified = knn.exp();Sourcepub fn log(self) -> RankExpr
pub fn log(self) -> RankExpr
Applies natural logarithm transformation: ln(rank).
Compresses the score range. Add a constant to avoid log(0).
§Examples
use chroma_types::operator::{RankExpr, QueryVector, Key};
let knn = RankExpr::Knn {
query: QueryVector::Dense(vec![0.1, 0.2, 0.3]),
key: Key::Embedding,
limit: 100,
default: None,
return_rank: false,
};
// Add constant to avoid log(0)
let compressed = (knn + 1.0).log();Sourcepub fn abs(self) -> RankExpr
pub fn abs(self) -> RankExpr
Takes absolute value of the ranking expression.
§Examples
use chroma_types::operator::{RankExpr, QueryVector, Key};
let knn1 = RankExpr::Knn {
query: QueryVector::Dense(vec![0.1, 0.2, 0.3]),
key: Key::Embedding,
limit: 100,
default: None,
return_rank: false,
};
let knn2 = RankExpr::Knn {
query: QueryVector::Dense(vec![0.2, 0.3, 0.4]),
key: Key::field("other"),
limit: 100,
default: None,
return_rank: false,
};
// Absolute difference
let diff = (knn1 - knn2).abs();Sourcepub fn max(self, other: impl Into<RankExpr>) -> RankExpr
pub fn max(self, other: impl Into<RankExpr>) -> RankExpr
Returns maximum of this expression and another.
Can be chained to clamp scores to a maximum value.
§Examples
use chroma_types::operator::{RankExpr, QueryVector, Key};
let knn = RankExpr::Knn {
query: QueryVector::Dense(vec![0.1, 0.2, 0.3]),
key: Key::Embedding,
limit: 100,
default: None,
return_rank: false,
};
// Clamp to maximum of 1.0
let clamped = knn.clone().max(1.0);
// Clamp to range [0.0, 1.0]
let range_clamped = knn.min(0.0).max(1.0);Sourcepub fn min(self, other: impl Into<RankExpr>) -> RankExpr
pub fn min(self, other: impl Into<RankExpr>) -> RankExpr
Returns minimum of this expression and another.
Can be chained to clamp scores to a minimum value.
§Examples
use chroma_types::operator::{RankExpr, QueryVector, Key};
let knn = RankExpr::Knn {
query: QueryVector::Dense(vec![0.1, 0.2, 0.3]),
key: Key::Embedding,
limit: 100,
default: None,
return_rank: false,
};
// Clamp to minimum of 0.0 (ensure non-negative)
let clamped = knn.clone().min(0.0);
// Clamp to range [0.0, 1.0]
let range_clamped = knn.min(0.0).max(1.0);Trait Implementations§
Source§impl<'de> Deserialize<'de> for RankExpr
impl<'de> Deserialize<'de> for RankExpr
Source§fn deserialize<__D>(
__deserializer: __D,
) -> Result<RankExpr, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(
__deserializer: __D,
) -> Result<RankExpr, <__D as Deserializer<'de>>::Error>where
__D: Deserializer<'de>,
Source§impl Serialize for RankExpr
impl Serialize for RankExpr
Source§fn serialize<__S>(
&self,
__serializer: __S,
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
fn serialize<__S>(
&self,
__serializer: __S,
) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>where
__S: Serializer,
Auto Trait Implementations§
impl Freeze for RankExpr
impl RefUnwindSafe for RankExpr
impl Send for RankExpr
impl Sync for RankExpr
impl Unpin for RankExpr
impl UnwindSafe for RankExpr
Blanket Implementations§
§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§unsafe fn clone_to_uninit(&self, dest: *mut u8)
unsafe fn clone_to_uninit(&self, dest: *mut u8)
clone_to_uninit)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,
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§unsafe fn to_subset_unchecked(&self) -> SS
unsafe 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.