qdrant_client/builders/text_query_builder.rs
1use crate::qdrant::{text_query, Bm25Params, TextQuery};
2
3/// Builder for [`TextQuery`], ranking points by BM25 over the text index of a payload field.
4///
5/// The payload field is selected with `using` on the query request, and its text index must
6/// have scoring enabled, see [`TextIndexParamsBuilder::bm25_scoring`](crate::qdrant::TextIndexParamsBuilder::bm25_scoring).
7#[must_use]
8#[derive(Clone)]
9pub struct TextQueryBuilder {
10 /// Text to search for, tokenized by the field's text index.
11 pub(crate) query: String,
12 /// Term frequency saturation. Default is 1.2.
13 pub(crate) k: Option<f32>,
14 /// Document length normalization, from 0 (none) to 1 (full). Default is 0.75.
15 pub(crate) b: Option<f32>,
16}
17
18impl TextQueryBuilder {
19 /// Create a new TextQueryBuilder for the given text.
20 ///
21 /// # Examples
22 ///
23 /// ```
24 /// use qdrant_client::qdrant::TextQueryBuilder;
25 ///
26 /// let text_query = TextQueryBuilder::new("vector database").k(1.5).b(0.5).build();
27 /// ```
28 pub fn new(query: impl Into<String>) -> Self {
29 Self {
30 query: query.into(),
31 k: None,
32 b: None,
33 }
34 }
35
36 /// Term frequency saturation. Default is 1.2.
37 pub fn k(self, value: f32) -> Self {
38 let mut new = self;
39 new.k = Some(value);
40 new
41 }
42
43 /// Document length normalization, from 0 (none) to 1 (full). Default is 0.75.
44 pub fn b(self, value: f32) -> Self {
45 let mut new = self;
46 new.b = Some(value);
47 new
48 }
49
50 /// Builds the query. `k` and `b` go in the BM25 scorer's parameters; with neither set, the
51 /// scorer runs with its defaults.
52 pub fn build(self) -> TextQuery {
53 let scoring = (self.k.is_some() || self.b.is_some()).then_some(text_query::Scoring::Bm25(
54 Bm25Params {
55 k: self.k,
56 b: self.b,
57 },
58 ));
59 TextQuery {
60 query: self.query,
61 scoring,
62 }
63 }
64}
65
66impl From<TextQueryBuilder> for TextQuery {
67 fn from(value: TextQueryBuilder) -> Self {
68 value.build()
69 }
70}