Skip to main content

qdrant_edge/edge/builders/
search_request.rs

1//! Fluent builder for [`SearchRequest`].
2//!
3//! Builder fields mirror [`SearchRequest`] 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::query_enum::QueryEnum;
9
10use crate::edge::requests::search::SearchRequest;
11
12/// Fluent builder for [`SearchRequest`].
13///
14/// `query` and `limit` are required and passed through [`Self::new`]; every
15/// other field is optional and falls back to the [`SearchRequest::new`] defaults.
16#[derive(Clone, Debug)]
17pub struct SearchRequestBuilder {
18    query: QueryEnum,
19    filter: Option<Filter>,
20    params: Option<SearchParams>,
21    limit: usize,
22    offset: usize,
23    with_payload: Option<WithPayloadInterface>,
24    with_vector: Option<WithVector>,
25    score_threshold: Option<ScoreType>,
26}
27
28impl SearchRequestBuilder {
29    pub fn new(query: QueryEnum, limit: usize) -> Self {
30        let SearchRequest {
31            query,
32            filter,
33            params,
34            limit,
35            offset,
36            with_payload,
37            with_vector,
38            score_threshold,
39        } = SearchRequest::new(query, limit);
40        Self {
41            query,
42            filter,
43            params,
44            limit,
45            offset,
46            with_payload,
47            with_vector,
48            score_threshold,
49        }
50    }
51
52    pub fn filter(mut self, filter: Filter) -> Self {
53        self.filter = Some(filter);
54        self
55    }
56
57    pub fn params(mut self, params: SearchParams) -> Self {
58        self.params = Some(params);
59        self
60    }
61
62    pub fn offset(mut self, offset: usize) -> Self {
63        self.offset = offset;
64        self
65    }
66
67    pub fn with_payload(mut self, with_payload: WithPayloadInterface) -> Self {
68        self.with_payload = Some(with_payload);
69        self
70    }
71
72    pub fn with_vector(mut self, with_vector: WithVector) -> Self {
73        self.with_vector = Some(with_vector);
74        self
75    }
76
77    pub fn score_threshold(mut self, score_threshold: ScoreType) -> Self {
78        self.score_threshold = Some(score_threshold);
79        self
80    }
81
82    pub fn build(self) -> SearchRequest {
83        // Exhaustively destructure Self and construct SearchRequest:
84        // adding a field to either type forces a compile error here.
85        let Self {
86            query,
87            filter,
88            params,
89            limit,
90            offset,
91            with_payload,
92            with_vector,
93            score_threshold,
94        } = self;
95        SearchRequest {
96            query,
97            filter,
98            params,
99            limit,
100            offset,
101            with_payload,
102            with_vector,
103            score_threshold,
104        }
105    }
106}