RankExpr

Enum RankExpr 

Source
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

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

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

Fields

§right: Box<RankExpr>
§

Exponentiation(Box<RankExpr>)

§

Knn

Fields

§key: Key
§limit: u32
§default: Option<f32>
§return_rank: bool
§

Logarithm(Box<RankExpr>)

§

Maximum(Vec<RankExpr>)

§

Minimum(Vec<RankExpr>)

§

Multiplication(Vec<RankExpr>)

§

Subtraction

Fields

§right: Box<RankExpr>
§

Summation(Vec<RankExpr>)

§

Value(f32)

Implementations§

Source§

impl RankExpr

Source

pub fn default_knn_key() -> Key

Source

pub fn default_knn_limit() -> u32

Source

pub fn knn_queries(&self) -> Vec<KnnQuery>

Source

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();
Source

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();
Source

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();
Source

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);
Source

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 Add<f32> for RankExpr

Source§

type Output = RankExpr

The resulting type after applying the + operator.
Source§

fn add(self, rhs: f32) -> <RankExpr as Add<f32>>::Output

Performs the + operation. Read more
Source§

impl Add for RankExpr

Source§

type Output = RankExpr

The resulting type after applying the + operator.
Source§

fn add(self, rhs: RankExpr) -> <RankExpr as Add>::Output

Performs the + operation. Read more
Source§

impl Clone for RankExpr

Source§

fn clone(&self) -> RankExpr

Returns a duplicate of the value. Read more
1.0.0§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for RankExpr

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for RankExpr

Source§

fn deserialize<__D>( __deserializer: __D, ) -> Result<RankExpr, <__D as Deserializer<'de>>::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Div<f32> for RankExpr

Source§

type Output = RankExpr

The resulting type after applying the / operator.
Source§

fn div(self, rhs: f32) -> <RankExpr as Div<f32>>::Output

Performs the / operation. Read more
Source§

impl Div for RankExpr

Source§

type Output = RankExpr

The resulting type after applying the / operator.
Source§

fn div(self, rhs: RankExpr) -> <RankExpr as Div>::Output

Performs the / operation. Read more
Source§

impl From<f32> for RankExpr

Source§

fn from(v: f32) -> RankExpr

Converts to this type from the input type.
Source§

impl Mul<f32> for RankExpr

Source§

type Output = RankExpr

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: f32) -> <RankExpr as Mul<f32>>::Output

Performs the * operation. Read more
Source§

impl Mul for RankExpr

Source§

type Output = RankExpr

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: RankExpr) -> <RankExpr as Mul>::Output

Performs the * operation. Read more
Source§

impl Neg for RankExpr

Source§

type Output = RankExpr

The resulting type after applying the - operator.
Source§

fn neg(self) -> <RankExpr as Neg>::Output

Performs the unary - operation. Read more
Source§

impl Serialize for RankExpr

Source§

fn serialize<__S>( &self, __serializer: __S, ) -> Result<<__S as Serializer>::Ok, <__S as Serializer>::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl Sub<f32> for RankExpr

Source§

type Output = RankExpr

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: f32) -> <RankExpr as Sub<f32>>::Output

Performs the - operation. Read more
Source§

impl Sub for RankExpr

Source§

type Output = RankExpr

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: RankExpr) -> <RankExpr as Sub>::Output

Performs the - operation. Read more
Source§

impl TryFrom<RankExpr> for RankExpr

Source§

type Error = QueryConversionError

The type returned in the event of a conversion error.
Source§

fn try_from( proto_expr: RankExpr, ) -> Result<RankExpr, <RankExpr as TryFrom<RankExpr>>::Error>

Performs the conversion.

Auto Trait Implementations§

Blanket Implementations§

§

impl<T> Any for T
where T: 'static + ?Sized,

§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
§

impl<T> Borrow<T> for T
where T: ?Sized,

§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
§

impl<T> BorrowMut<T> for T
where T: ?Sized,

§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
§

impl<T> CloneToUninit for T
where T: Clone,

§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
§

impl<T> From<T> for T

§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> FromRef<T> for T
where T: Clone,

Source§

fn from_ref(input: &T) -> T

Converts to this type from a reference to the input type.
Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
§

impl<T, U> Into<U> for T
where U: From<T>,

§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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 more
Source§

impl<T> IntoRequest<T> for T

Source§

fn into_request(self) -> Request<T>

Wrap the input message T in a tonic::Request
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<SS, SP> SupersetOf<SS> for SP
where SS: SubsetOf<SP>,

Source§

fn to_subset(&self) -> Option<SS>

The inverse inclusion map: attempts to construct self from the equivalent element of its superset. Read more
Source§

fn is_in_subset(&self) -> bool

Checks if self is actually part of its subset T (and can be converted to it).
Source§

unsafe fn to_subset_unchecked(&self) -> SS

Use with care! Same as self.to_subset but without any property checks. Always succeeds.
Source§

fn from_subset(element: &SS) -> SP

The inclusion map: converts self to the equivalent element of its superset.
§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> ClosedNeg for T
where T: Neg<Output = T>,

Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<T> ErasedDestructor for T
where T: 'static,

Source§

impl<T> MaybeSendSync for T