1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
use crate::{errors::Error, indexes::Index};
use serde::{de::DeserializeOwned, Deserialize};
use std::collections::HashMap;
use serde_json::to_string;
#[derive(Deserialize, Debug)]
#[serde(rename_all = "camelCase")]
pub struct SearchResults<T> {
pub hits: Vec<T>,
pub offset: usize,
pub limit: usize,
pub nb_hits: usize,
pub exhaustive_nb_hits: bool,
pub facets_distribution: Option<HashMap<String, HashMap<String, usize>>>,
pub exhaustive_facets_count: Option<bool>,
pub processing_time_ms: usize,
pub query: String,
}
pub struct Query<'a> {
pub query: &'a str,
pub offset: Option<usize>,
pub limit: Option<usize>,
pub filters: Option<&'a str>,
pub facet_filters: Option<Vec<Vec<&'a str>>>,
pub facets_distribution: Option<Option<Vec<&'a str>>>,
pub attributes_to_retrieve: Option<&'a str>,
pub attributes_to_crop: Option<&'a str>,
pub crop_length: Option<usize>,
pub attributes_to_highlight: Option<&'a str>,
}
#[allow(missing_docs)]
impl<'a> Query<'a> {
pub fn new(query: &'a str) -> Query<'a> {
Query {
query,
offset: None,
limit: None,
filters: None,
facet_filters: None,
facets_distribution: None,
attributes_to_retrieve: None,
attributes_to_crop: None,
attributes_to_highlight: None,
crop_length: None,
}
}
pub fn with_offset(self, offset: usize) -> Query<'a> {
Query {
offset: Some(offset),
..self
}
}
pub fn with_limit(self, limit: usize) -> Query<'a> {
Query {
limit: Some(limit),
..self
}
}
pub fn with_filters(self, filters: &'a str) -> Query<'a> {
Query {
filters: Some(filters),
..self
}
}
pub fn with_facet_filters(self, facet_filters: Vec<Vec<&'a str>>) -> Query<'a> {
Query {
facet_filters: Some(facet_filters),
..self
}
}
pub fn with_facets_distribution(self, facets_distribution: Option<Vec<&'a str>>) -> Query<'a> {
Query {
facets_distribution: Some(facets_distribution),
..self
}
}
pub fn with_attributes_to_retrieve(self, attributes_to_retrieve: &'a str) -> Query<'a> {
Query {
attributes_to_retrieve: Some(attributes_to_retrieve),
..self
}
}
pub fn with_attributes_to_crop(self, attributes_to_crop: &'a str) -> Query<'a> {
Query {
attributes_to_crop: Some(attributes_to_crop),
..self
}
}
pub fn with_attributes_to_highlight(self, attributes_to_highlight: &'a str) -> Query<'a> {
Query {
attributes_to_highlight: Some(attributes_to_highlight),
..self
}
}
pub fn with_crop_length(self, crop_length: usize) -> Query<'a> {
Query {
crop_length: Some(crop_length),
..self
}
}
}
impl<'a> Query<'a> {
pub(crate) fn to_url(&self) -> String {
use urlencoding::encode;
let mut url = format!("?q={}", encode(self.query));
if let Some(offset) = self.offset {
url.push_str("&offset=");
url.push_str(offset.to_string().as_str());
}
if let Some(limit) = self.limit {
url.push_str("&limit=");
url.push_str(limit.to_string().as_str());
}
if let Some(filters) = self.filters {
url.push_str("&filters=");
url.push_str(encode(filters).as_str());
}
if let Some(facet_filters) = &self.facet_filters {
url.push_str("&facetFilters=");
url.push_str(encode(&to_string(&facet_filters).unwrap()).as_str());
}
if let Some(facets_distribution) = &self.facets_distribution {
url.push_str("&facetsDistribution=");
match facets_distribution {
Some(facets_distribution) => url.push_str(encode(&to_string(&facets_distribution).unwrap()).as_str()),
None => url.push_str("*")
}
}
if let Some(attributes_to_retrieve) = self.attributes_to_retrieve {
url.push_str("&attributesToRetrieve=");
url.push_str(encode(attributes_to_retrieve).as_str());
}
if let Some(attributes_to_crop) = self.attributes_to_crop {
url.push_str("&attributesToCrop=");
url.push_str(encode(attributes_to_crop).as_str());
}
if let Some(crop_length) = self.crop_length {
url.push_str("&cropLength=");
url.push_str(crop_length.to_string().as_str());
}
if let Some(attributes_to_highlight) = self.attributes_to_highlight {
url.push_str("&attributesToHighlight=");
url.push_str(encode(attributes_to_highlight).as_str());
}
url
}
pub async fn execute<T: 'static + DeserializeOwned>(
&'a self,
index: &Index<'a>,
) -> Result<SearchResults<T>, Error> {
index.search::<T>(&self).await
}
}