Skip to main content

qdrant_client/builders/
rrf_builder.rs

1use crate::qdrant::Rrf;
2
3#[must_use]
4#[derive(Clone)]
5pub struct RrfBuilder {
6    /// K parameter for reciprocal rank fusion.
7    ///
8    /// Controls how quickly the weights decrease as rank increases.
9    /// Higher values make the weighting more uniform across ranks.
10    ///
11    /// Default value is 60.
12    pub(crate) k: Option<Option<u32>>,
13    /// Weights for each prefetch source.
14    /// Higher weight gives more influence on the final ranking.
15    /// If not specified, all prefetches are weighted equally.
16    pub(crate) weights: Option<Vec<f32>>,
17}
18
19impl RrfBuilder {
20    /// Create a new RrfBuilder with default values.
21    ///
22    /// # Examples
23    ///
24    /// ```
25    /// use qdrant_client::qdrant::RrfBuilder;
26    ///
27    /// let rrf = RrfBuilder::new().build();
28    /// ```
29    pub fn new() -> Self {
30        Self::create_empty()
31    }
32
33    /// Create a new RrfBuilder with a specific k parameter.
34    ///
35    /// # Arguments
36    ///
37    /// * `k` - K parameter for reciprocal rank fusion. Default is 60.
38    ///
39    /// # Examples
40    ///
41    /// ```
42    /// use qdrant_client::qdrant::RrfBuilder;
43    ///
44    /// let rrf = RrfBuilder::with_k(100).build();
45    /// ```
46    pub fn with_k(k: u32) -> Self {
47        Self::new().k(k)
48    }
49
50    /// Set the K parameter for reciprocal rank fusion.
51    ///
52    /// Controls how quickly the weights decrease as rank increases.
53    /// Higher values make the weighting more uniform across ranks.
54    ///
55    /// Default value is 60.
56    pub fn k(self, value: u32) -> Self {
57        let mut new = self;
58        new.k = Option::Some(Option::Some(value));
59        new
60    }
61
62    /// Weights for each prefetch source.
63    /// Higher weight gives more influence on the final ranking.
64    /// If not specified, all prefetches are weighted equally.
65    pub fn weights(self, value: Vec<f32>) -> Self {
66        let mut new = self;
67        new.weights = Option::Some(value);
68        new
69    }
70
71    pub fn build(self) -> Rrf {
72        Rrf {
73            k: self.k.unwrap_or_default(),
74            weights: self.weights.unwrap_or_default(),
75        }
76    }
77
78    /// Create an empty builder, with all fields set to `None`.
79    fn create_empty() -> Self {
80        Self {
81            k: core::default::Default::default(),
82            weights: core::default::Default::default(),
83        }
84    }
85}
86
87impl From<RrfBuilder> for Rrf {
88    fn from(value: RrfBuilder) -> Self {
89        value.build()
90    }
91}
92
93impl Default for RrfBuilder {
94    fn default() -> Self {
95        Self::create_empty()
96    }
97}