Skip to main content

qdrant_edge/edge/builders/
query_request.rs

1//! Fluent builder for [`QueryRequest`].
2//!
3//! Builder fields mirror [`QueryRequest`] explicitly so adding a field
4//! to the target struct forces a compile error here.
5
6use crate::common::types::ScoreType;
7use crate::segment::types::{Filter, SearchParams, WithPayloadInterface, WithVector};
8use crate::shard::query::ScoringQuery;
9
10use crate::edge::requests::query::{Prefetch, QueryRequest};
11
12/// Fluent builder for [`QueryRequest`].
13///
14/// `limit` is required and passed through [`Self::new`]; every other field is
15/// optional and falls back to the [`QueryRequest::new`] defaults.
16#[derive(Clone, Debug)]
17pub struct QueryRequestBuilder {
18    prefetches: Vec<Prefetch>,
19    query: Option<ScoringQuery>,
20    filter: Option<Filter>,
21    score_threshold: Option<ScoreType>,
22    limit: usize,
23    offset: usize,
24    params: Option<SearchParams>,
25    with_vector: WithVector,
26    with_payload: WithPayloadInterface,
27}
28
29impl QueryRequestBuilder {
30    pub fn new(limit: usize) -> Self {
31        let QueryRequest {
32            prefetches,
33            query,
34            filter,
35            score_threshold,
36            limit,
37            offset,
38            params,
39            with_vector,
40            with_payload,
41        } = QueryRequest::new(limit);
42        Self {
43            prefetches,
44            query,
45            filter,
46            score_threshold,
47            limit,
48            offset,
49            params,
50            with_vector,
51            with_payload,
52        }
53    }
54
55    /// Replaces the whole prefetch list; see [`Self::add_prefetch`] to append one stage.
56    pub fn prefetches(mut self, prefetches: Vec<Prefetch>) -> Self {
57        self.prefetches = prefetches;
58        self
59    }
60
61    pub fn add_prefetch(mut self, prefetch: Prefetch) -> Self {
62        self.prefetches.push(prefetch);
63        self
64    }
65
66    pub fn query(mut self, query: ScoringQuery) -> Self {
67        self.query = Some(query);
68        self
69    }
70
71    pub fn filter(mut self, filter: Filter) -> Self {
72        self.filter = Some(filter);
73        self
74    }
75
76    pub fn score_threshold(mut self, score_threshold: ScoreType) -> Self {
77        self.score_threshold = Some(score_threshold);
78        self
79    }
80
81    pub fn offset(mut self, offset: usize) -> Self {
82        self.offset = offset;
83        self
84    }
85
86    pub fn params(mut self, params: SearchParams) -> Self {
87        self.params = Some(params);
88        self
89    }
90
91    pub fn with_vector(mut self, with_vector: WithVector) -> Self {
92        self.with_vector = with_vector;
93        self
94    }
95
96    pub fn with_payload(mut self, with_payload: WithPayloadInterface) -> Self {
97        self.with_payload = with_payload;
98        self
99    }
100
101    pub fn build(self) -> QueryRequest {
102        // Exhaustively destructure Self and construct QueryRequest:
103        // adding a field to either type forces a compile error here.
104        let Self {
105            prefetches,
106            query,
107            filter,
108            score_threshold,
109            limit,
110            offset,
111            params,
112            with_vector,
113            with_payload,
114        } = self;
115        QueryRequest {
116            prefetches,
117            query,
118            filter,
119            score_threshold,
120            limit,
121            offset,
122            params,
123            with_vector,
124            with_payload,
125        }
126    }
127}