Skip to main content

couchbase_core/options/
search.rs

1/*
2 *
3 *  * Copyright (c) 2025 Couchbase, Inc.
4 *  *
5 *  * Licensed under the Apache License, Version 2.0 (the "License");
6 *  * you may not use this file except in compliance with the License.
7 *  * You may obtain a copy of the License at
8 *  *
9 *  *    http://www.apache.org/licenses/LICENSE-2.0
10 *  *
11 *  * Unless required by applicable law or agreed to in writing, software
12 *  * distributed under the License is distributed on an "AS IS" BASIS,
13 *  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  * See the License for the specific language governing permissions and
15 *  * limitations under the License.
16 *
17 */
18
19use crate::httpx::request::OnBehalfOfInfo;
20use crate::retry::{RetryStrategy, DEFAULT_RETRY_STRATEGY};
21use crate::searchx;
22use crate::searchx::facets::Facet;
23use crate::searchx::queries::Query;
24use crate::searchx::query_options::{Control, Highlight, KnnOperator, KnnQuery};
25use crate::searchx::sort::Sort;
26use std::collections::HashMap;
27use std::sync::Arc;
28
29#[derive(Debug, Clone)]
30#[non_exhaustive]
31pub struct SearchOptions {
32    pub collections: Option<Vec<String>>,
33    pub control: Option<Control>,
34    pub explain: Option<bool>,
35    pub facets: Option<HashMap<String, Facet>>,
36    pub fields: Option<Vec<String>>,
37    pub from: Option<u32>,
38    pub highlight: Option<Highlight>,
39    pub include_locations: Option<bool>,
40    pub query: Option<Query>,
41    pub score: Option<String>,
42    pub search_after: Option<Vec<String>>,
43    pub search_before: Option<Vec<String>>,
44    pub show_request: Option<bool>,
45    pub size: Option<u32>,
46    pub sort: Option<Vec<Sort>>,
47    pub knn: Option<Vec<KnnQuery>>,
48    pub knn_operator: Option<KnnOperator>,
49
50    pub raw: Option<HashMap<String, serde_json::Value>>,
51
52    pub index_name: String,
53    pub scope_name: Option<String>,
54    pub bucket_name: Option<String>,
55
56    pub on_behalf_of: Option<OnBehalfOfInfo>,
57
58    pub endpoint: Option<String>,
59    pub retry_strategy: Arc<dyn RetryStrategy>,
60}
61
62impl SearchOptions {
63    pub fn new(index_name: impl Into<String>) -> Self {
64        Self {
65            collections: None,
66            control: None,
67            explain: None,
68            facets: None,
69            fields: None,
70            from: None,
71            highlight: None,
72            include_locations: None,
73            query: None,
74            score: None,
75            search_after: None,
76            search_before: None,
77            show_request: None,
78            size: None,
79            sort: None,
80            knn: None,
81            knn_operator: None,
82            raw: None,
83            index_name: index_name.into(),
84            scope_name: None,
85            bucket_name: None,
86            on_behalf_of: None,
87            endpoint: None,
88            retry_strategy: DEFAULT_RETRY_STRATEGY.clone(),
89        }
90    }
91
92    pub fn collections(mut self, collections: impl Into<Option<Vec<String>>>) -> Self {
93        self.collections = collections.into();
94        self
95    }
96
97    pub fn control(mut self, control: impl Into<Option<Control>>) -> Self {
98        self.control = control.into();
99        self
100    }
101
102    pub fn explain(mut self, explain: impl Into<Option<bool>>) -> Self {
103        self.explain = explain.into();
104        self
105    }
106
107    pub fn facets(mut self, facets: impl Into<Option<HashMap<String, Facet>>>) -> Self {
108        self.facets = facets.into();
109        self
110    }
111
112    pub fn fields(mut self, fields: impl Into<Option<Vec<String>>>) -> Self {
113        self.fields = fields.into();
114        self
115    }
116
117    pub fn from(mut self, from: impl Into<Option<u32>>) -> Self {
118        self.from = from.into();
119        self
120    }
121
122    pub fn highlight(mut self, highlight: impl Into<Option<Highlight>>) -> Self {
123        self.highlight = highlight.into();
124        self
125    }
126
127    pub fn include_locations(mut self, include_locations: impl Into<Option<bool>>) -> Self {
128        self.include_locations = include_locations.into();
129        self
130    }
131
132    pub fn query(mut self, query: impl Into<Option<Query>>) -> Self {
133        self.query = query.into();
134        self
135    }
136
137    pub fn score(mut self, score: impl Into<Option<String>>) -> Self {
138        self.score = score.into();
139        self
140    }
141
142    pub fn search_after(mut self, search_after: impl Into<Option<Vec<String>>>) -> Self {
143        self.search_after = search_after.into();
144        self
145    }
146
147    pub fn search_before(mut self, search_before: impl Into<Option<Vec<String>>>) -> Self {
148        self.search_before = search_before.into();
149        self
150    }
151
152    pub fn show_request(mut self, show_request: impl Into<Option<bool>>) -> Self {
153        self.show_request = show_request.into();
154        self
155    }
156
157    pub fn size(mut self, size: impl Into<Option<u32>>) -> Self {
158        self.size = size.into();
159        self
160    }
161
162    pub fn sort(mut self, sort: impl Into<Option<Vec<Sort>>>) -> Self {
163        self.sort = sort.into();
164        self
165    }
166
167    pub fn knn(mut self, knn: impl Into<Option<Vec<KnnQuery>>>) -> Self {
168        self.knn = knn.into();
169        self
170    }
171
172    pub fn knn_operator(mut self, knn_operator: impl Into<Option<KnnOperator>>) -> Self {
173        self.knn_operator = knn_operator.into();
174        self
175    }
176
177    pub fn raw(mut self, raw: impl Into<Option<HashMap<String, serde_json::Value>>>) -> Self {
178        self.raw = raw.into();
179        self
180    }
181
182    pub fn scope_name(mut self, scope_name: impl Into<Option<String>>) -> Self {
183        self.scope_name = scope_name.into();
184        self
185    }
186
187    pub fn bucket_name(mut self, bucket_name: impl Into<Option<String>>) -> Self {
188        self.bucket_name = bucket_name.into();
189        self
190    }
191
192    pub fn on_behalf_of(mut self, on_behalf_of: impl Into<Option<OnBehalfOfInfo>>) -> Self {
193        self.on_behalf_of = on_behalf_of.into();
194        self
195    }
196
197    pub fn retry_strategy(mut self, retry_strategy: Arc<dyn RetryStrategy>) -> Self {
198        self.retry_strategy = retry_strategy;
199        self
200    }
201
202    pub fn endpoint(mut self, endpoint: impl Into<Option<String>>) -> Self {
203        self.endpoint = endpoint.into();
204        self
205    }
206}
207
208impl From<SearchOptions> for searchx::query_options::QueryOptions {
209    fn from(opts: SearchOptions) -> Self {
210        searchx::query_options::QueryOptions {
211            collections: opts.collections,
212            control: opts.control,
213            explain: opts.explain,
214            facets: opts.facets,
215            fields: opts.fields,
216            from: opts.from,
217            highlight: opts.highlight,
218            include_locations: opts.include_locations,
219            query: opts.query,
220            score: opts.score,
221            search_after: opts.search_after,
222            search_before: opts.search_before,
223            show_request: opts.show_request,
224            size: opts.size,
225            sort: opts.sort,
226            knn: opts.knn,
227            knn_operator: opts.knn_operator,
228            raw: opts.raw,
229            index_name: opts.index_name,
230            scope_name: opts.scope_name,
231            bucket_name: opts.bucket_name,
232            on_behalf_of: opts.on_behalf_of,
233        }
234    }
235}