Skip to content

Commit 9e689d5

Browse files
Merge #341
341: Replace struct Query with SearchQuery r=bidoubiwa a=vishalsodani # Pull Request ## What does this PR do? Fixes #332 <!-- Please link the issue you're trying to fix with this PR, if none then please create an issue first. --> ## PR checklist Please check if your PR fulfills the following requirements: - [x] Does this PR fix an existing issue? - [x] Have you read the contributing guidelines? - [x] Have you made sure that the title is accurate and descriptive of the changes? Thank you so much for contributing to Meilisearch! Co-authored-by: vishalsodani <[email protected]>
2 parents dbd0c17 + 61427a4 commit 9e689d5

File tree

4 files changed

+49
-47
lines changed

4 files changed

+49
-47
lines changed

.code-samples.meilisearch.yaml

+4-4
Original file line numberDiff line numberDiff line change
@@ -820,9 +820,9 @@ getting_started_add_documents_md: |-
820820
821821
[About this SDK](https://github.com/meilisearch/meilisearch-rust/)
822822
getting_started_search_md: |-
823-
You can build a `Query` and execute it later:
823+
You can build a `SearchQuery` and execute it later:
824824
```rust
825-
let query: Query = Query::new(&movies)
825+
let query: SearchQuery = SearchQuery::new(&movies)
826826
.with_query("botman")
827827
.build();
828828
@@ -833,9 +833,9 @@ getting_started_search_md: |-
833833
.unwrap();
834834
```
835835
836-
You can build a `Query` and execute it directly:
836+
You can build a `SearchQuery` and execute it directly:
837837
```rust
838-
let results: SearchResults<Movie> = Query::new(&movies)
838+
let results: SearchResults<Movie> = SearchQuery::new(&movies)
839839
.with_query("botman")
840840
.execute()
841841
.await

src/indexes.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -211,17 +211,17 @@ impl Index {
211211
/// // add some documents
212212
/// # movies.add_or_replace(&[Movie{name:String::from("Interstellar"), description:String::from("Interstellar chronicles the adventures of a group of explorers who make use of a newly discovered wormhole to surpass the limitations on human space travel and conquer the vast distances involved in an interstellar voyage.")},Movie{name:String::from("Unknown"), description:String::from("Unknown")}], Some("name")).await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
213213
///
214-
/// let query = Query::new(&movies).with_query("Interstellar").with_limit(5).build();
214+
/// let query = SearchQuery::new(&movies).with_query("Interstellar").with_limit(5).build();
215215
/// let results = movies.execute_query::<Movie>(&query).await.unwrap();
216216
/// assert!(results.hits.len()>0);
217217
/// # movies.delete().await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
218218
/// # });
219219
/// ```
220220
pub async fn execute_query<T: 'static + DeserializeOwned>(
221221
&self,
222-
query: &Query<'_>,
222+
query: &SearchQuery<'_>,
223223
) -> Result<SearchResults<T>, Error> {
224-
request::<&Query, SearchResults<T>>(
224+
request::<&SearchQuery, SearchResults<T>>(
225225
&format!("{}/indexes/{}/search", self.client.host, self.uid),
226226
&self.client.api_key,
227227
Method::Post(query),
@@ -267,8 +267,8 @@ impl Index {
267267
/// # movies.delete().await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
268268
/// # });
269269
/// ```
270-
pub fn search(&self) -> Query {
271-
Query::new(self)
270+
pub fn search(&self) -> SearchQuery {
271+
SearchQuery::new(self)
272272
}
273273

274274
/// Get one [Document] using its unique id.

src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -248,3 +248,5 @@ pub mod tasks;
248248
mod tenant_tokens;
249249
/// Module containing utilies functions.
250250
mod utils;
251+
252+
pub use client::*;

src/search.rs

+38-38
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ fn serialize_attributes_to_crop_with_wildcard<S: Serializer>(
7878
}
7979
}
8080

81-
/// Some list fields in a `Query` can be set to a wildcard value.
81+
/// Some list fields in a `SearchQuery` can be set to a wildcard value.
8282
/// This structure allows you to choose between the wildcard value and an exhaustive list of selectors.
8383
#[derive(Debug, Clone)]
8484
pub enum Selectors<T> {
@@ -98,7 +98,7 @@ type AttributeToCrop<'a> = (&'a str, Option<usize>);
9898
///
9999
/// ```
100100
/// use serde::{Serialize, Deserialize};
101-
/// # use meilisearch_sdk::{client::Client, search::Query, indexes::Index};
101+
/// # use meilisearch_sdk::{client::Client, search::SearchQuery, indexes::Index};
102102
/// #
103103
/// # let MEILISEARCH_HOST = option_env!("MEILISEARCH_HOST").unwrap_or("http://localhost:7700");
104104
/// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey");
@@ -120,7 +120,7 @@ type AttributeToCrop<'a> = (&'a str, Option<usize>);
120120
/// # .try_make_index(&client)
121121
/// # .unwrap();
122122
///
123-
/// let mut res = Query::new(&index)
123+
/// let mut res = SearchQuery::new(&index)
124124
/// .with_query("space")
125125
/// .with_offset(42)
126126
/// .with_limit(21)
@@ -134,7 +134,7 @@ type AttributeToCrop<'a> = (&'a str, Option<usize>);
134134
/// ```
135135
///
136136
/// ```
137-
/// # use meilisearch_sdk::{client::Client, search::Query, indexes::Index};
137+
/// # use meilisearch_sdk::{client::Client, search::SearchQuery, indexes::Index};
138138
/// #
139139
/// # let MEILISEARCH_HOST = option_env!("MEILISEARCH_HOST").unwrap_or("http://localhost:7700");
140140
/// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey");
@@ -149,7 +149,7 @@ type AttributeToCrop<'a> = (&'a str, Option<usize>);
149149
/// ```
150150
#[derive(Debug, Serialize, Clone)]
151151
#[serde(rename_all = "camelCase")]
152-
pub struct Query<'a> {
152+
pub struct SearchQuery<'a> {
153153
#[serde(skip_serializing)]
154154
index: &'a Index,
155155
/// The text that will be searched for among the documents.
@@ -237,9 +237,9 @@ pub struct Query<'a> {
237237
}
238238

239239
#[allow(missing_docs)]
240-
impl<'a> Query<'a> {
241-
pub fn new(index: &'a Index) -> Query<'a> {
242-
Query {
240+
impl<'a> SearchQuery<'a> {
241+
pub fn new(index: &'a Index) -> SearchQuery<'a> {
242+
SearchQuery {
243243
index,
244244
query: None,
245245
offset: None,
@@ -257,82 +257,82 @@ impl<'a> Query<'a> {
257257
show_matches_position: None,
258258
}
259259
}
260-
pub fn with_query<'b>(&'b mut self, query: &'a str) -> &'b mut Query<'a> {
260+
pub fn with_query<'b>(&'b mut self, query: &'a str) -> &'b mut SearchQuery<'a> {
261261
self.query = Some(query);
262262
self
263263
}
264264

265-
pub fn with_offset<'b>(&'b mut self, offset: usize) -> &'b mut Query<'a> {
265+
pub fn with_offset<'b>(&'b mut self, offset: usize) -> &'b mut SearchQuery<'a> {
266266
self.offset = Some(offset);
267267
self
268268
}
269-
pub fn with_limit<'b>(&'b mut self, limit: usize) -> &'b mut Query<'a> {
269+
pub fn with_limit<'b>(&'b mut self, limit: usize) -> &'b mut SearchQuery<'a> {
270270
self.limit = Some(limit);
271271
self
272272
}
273-
pub fn with_filter<'b>(&'b mut self, filter: &'a str) -> &'b mut Query<'a> {
273+
pub fn with_filter<'b>(&'b mut self, filter: &'a str) -> &'b mut SearchQuery<'a> {
274274
self.filter = Some(filter);
275275
self
276276
}
277-
pub fn with_facets<'b>(&'b mut self, facets: Selectors<&'a [&'a str]>) -> &'b mut Query<'a> {
277+
pub fn with_facets<'b>(&'b mut self, facets: Selectors<&'a [&'a str]>) -> &'b mut SearchQuery<'a> {
278278
self.facets = Some(facets);
279279
self
280280
}
281-
pub fn with_sort<'b>(&'b mut self, sort: &'a [&'a str]) -> &'b mut Query<'a> {
281+
pub fn with_sort<'b>(&'b mut self, sort: &'a [&'a str]) -> &'b mut SearchQuery<'a> {
282282
self.sort = Some(sort);
283283
self
284284
}
285285
pub fn with_attributes_to_retrieve<'b>(
286286
&'b mut self,
287287
attributes_to_retrieve: Selectors<&'a [&'a str]>,
288-
) -> &'b mut Query<'a> {
288+
) -> &'b mut SearchQuery<'a> {
289289
self.attributes_to_retrieve = Some(attributes_to_retrieve);
290290
self
291291
}
292292
pub fn with_attributes_to_crop<'b>(
293293
&'b mut self,
294294
attributes_to_crop: Selectors<&'a [(&'a str, Option<usize>)]>,
295-
) -> &'b mut Query<'a> {
295+
) -> &'b mut SearchQuery<'a> {
296296
self.attributes_to_crop = Some(attributes_to_crop);
297297
self
298298
}
299-
pub fn with_crop_length<'b>(&'b mut self, crop_length: usize) -> &'b mut Query<'a> {
299+
pub fn with_crop_length<'b>(&'b mut self, crop_length: usize) -> &'b mut SearchQuery<'a> {
300300
self.crop_length = Some(crop_length);
301301
self
302302
}
303-
pub fn with_crop_marker<'b>(&'b mut self, crop_marker: &'a str) -> &'b mut Query<'a> {
303+
pub fn with_crop_marker<'b>(&'b mut self, crop_marker: &'a str) -> &'b mut SearchQuery<'a> {
304304
self.crop_marker = Some(crop_marker);
305305
self
306306
}
307307
pub fn with_attributes_to_highlight<'b>(
308308
&'b mut self,
309309
attributes_to_highlight: Selectors<&'a [&'a str]>,
310-
) -> &'b mut Query<'a> {
310+
) -> &'b mut SearchQuery<'a> {
311311
self.attributes_to_highlight = Some(attributes_to_highlight);
312312
self
313313
}
314314
pub fn with_highlight_pre_tag<'b>(
315315
&'b mut self,
316316
highlight_pre_tag: &'a str,
317-
) -> &'b mut Query<'a> {
317+
) -> &'b mut SearchQuery<'a> {
318318
self.highlight_pre_tag = Some(highlight_pre_tag);
319319
self
320320
}
321321
pub fn with_highlight_post_tag<'b>(
322322
&'b mut self,
323323
highlight_post_tag: &'a str,
324-
) -> &'b mut Query<'a> {
324+
) -> &'b mut SearchQuery<'a> {
325325
self.highlight_post_tag = Some(highlight_post_tag);
326326
self
327327
}
328328
pub fn with_show_matches_position<'b>(
329329
&'b mut self,
330330
show_matches_position: bool,
331-
) -> &'b mut Query<'a> {
331+
) -> &'b mut SearchQuery<'a> {
332332
self.show_matches_position = Some(show_matches_position);
333333
self
334334
}
335-
pub fn build(&mut self) -> Query<'a> {
335+
pub fn build(&mut self) -> SearchQuery<'a> {
336336
self.clone()
337337
}
338338

@@ -397,7 +397,7 @@ mod tests {
397397

398398
#[meilisearch_test]
399399
async fn test_query_builder(_client: Client, index: Index) -> Result<(), Error> {
400-
let mut query = Query::new(&index);
400+
let mut query = SearchQuery::new(&index);
401401
query.with_query("space").with_offset(42).with_limit(21);
402402

403403
let res = query.execute::<Document>().await.unwrap();
@@ -481,7 +481,7 @@ mod tests {
481481
async fn test_query_facet_distribution(client: Client, index: Index) -> Result<(), Error> {
482482
setup_test_index(&client, &index).await?;
483483

484-
let mut query = Query::new(&index);
484+
let mut query = SearchQuery::new(&index);
485485
query.with_facets(Selectors::All);
486486
let results: SearchResults<Document> = index.execute_query(&query).await?;
487487
assert_eq!(
@@ -495,7 +495,7 @@ mod tests {
495495
&8
496496
);
497497

498-
let mut query = Query::new(&index);
498+
let mut query = SearchQuery::new(&index);
499499
query.with_facets(Selectors::Some(&["kind"]));
500500
let results: SearchResults<Document> = index.execute_query(&query).await?;
501501
assert_eq!(
@@ -533,7 +533,7 @@ mod tests {
533533
.await?;
534534
assert_eq!(results.hits.len(), 10);
535535

536-
let mut query = Query::new(&index);
536+
let mut query = SearchQuery::new(&index);
537537
query.with_attributes_to_retrieve(Selectors::Some(&["kind", "id"])); // omit the "value" field
538538
assert!(index.execute_query::<Document>(&query).await.is_err()); // error: missing "value" field
539539
Ok(())
@@ -543,7 +543,7 @@ mod tests {
543543
async fn test_query_sort(client: Client, index: Index) -> Result<(), Error> {
544544
setup_test_index(&client, &index).await?;
545545

546-
let mut query = Query::new(&index);
546+
let mut query = SearchQuery::new(&index);
547547
query.with_query("harry potter");
548548
query.with_sort(&["title:desc"]);
549549
let results: SearchResults<Document> = index.execute_query(&query).await?;
@@ -555,7 +555,7 @@ mod tests {
555555
async fn test_query_attributes_to_crop(client: Client, index: Index) -> Result<(), Error> {
556556
setup_test_index(&client, &index).await?;
557557

558-
let mut query = Query::new(&index);
558+
let mut query = SearchQuery::new(&index);
559559
query.with_query("lorem ipsum");
560560
query.with_attributes_to_crop(Selectors::All);
561561
let results: SearchResults<Document> = index.execute_query(&query).await?;
@@ -572,7 +572,7 @@ mod tests {
572572
results.hits[0].formatted_result.as_ref().unwrap()
573573
);
574574

575-
let mut query = Query::new(&index);
575+
let mut query = SearchQuery::new(&index);
576576
query.with_query("lorem ipsum");
577577
query.with_attributes_to_crop(Selectors::Some(&[("value", Some(5)), ("kind", None)]));
578578
let results: SearchResults<Document> = index.execute_query(&query).await?;
@@ -594,7 +594,7 @@ mod tests {
594594
async fn test_query_crop_length(client: Client, index: Index) -> Result<(), Error> {
595595
setup_test_index(&client, &index).await?;
596596

597-
let mut query = Query::new(&index);
597+
let mut query = SearchQuery::new(&index);
598598
query.with_query("lorem ipsum");
599599
query.with_attributes_to_crop(Selectors::All);
600600
query.with_crop_length(200);
@@ -607,7 +607,7 @@ mod tests {
607607
},
608608
results.hits[0].formatted_result.as_ref().unwrap());
609609

610-
let mut query = Query::new(&index);
610+
let mut query = SearchQuery::new(&index);
611611
query.with_query("lorem ipsum");
612612
query.with_attributes_to_crop(Selectors::All);
613613
query.with_crop_length(5);
@@ -630,7 +630,7 @@ mod tests {
630630
async fn test_query_customized_crop_marker(client: Client, index: Index) -> Result<(), Error> {
631631
setup_test_index(&client, &index).await?;
632632

633-
let mut query = Query::new(&index);
633+
let mut query = SearchQuery::new(&index);
634634
query.with_query("sed do eiusmod");
635635
query.with_attributes_to_crop(Selectors::All);
636636
query.with_crop_length(6);
@@ -659,7 +659,7 @@ mod tests {
659659
) -> Result<(), Error> {
660660
setup_test_index(&client, &index).await?;
661661

662-
let mut query = Query::new(&index);
662+
let mut query = SearchQuery::new(&index);
663663
query.with_query("Social");
664664
query.with_attributes_to_highlight(Selectors::All);
665665
query.with_highlight_pre_tag("(⊃。•́‿•̀。)⊃ ");
@@ -685,7 +685,7 @@ mod tests {
685685
async fn test_query_attributes_to_highlight(client: Client, index: Index) -> Result<(), Error> {
686686
setup_test_index(&client, &index).await?;
687687

688-
let mut query = Query::new(&index);
688+
let mut query = SearchQuery::new(&index);
689689
query.with_query("dolor text");
690690
query.with_attributes_to_highlight(Selectors::All);
691691
let results: SearchResults<Document> = index.execute_query(&query).await?;
@@ -701,7 +701,7 @@ mod tests {
701701
results.hits[0].formatted_result.as_ref().unwrap(),
702702
);
703703

704-
let mut query = Query::new(&index);
704+
let mut query = SearchQuery::new(&index);
705705
query.with_query("dolor text");
706706
query.with_attributes_to_highlight(Selectors::Some(&["value"]));
707707
let results: SearchResults<Document> = index.execute_query(&query).await?;
@@ -723,7 +723,7 @@ mod tests {
723723
async fn test_query_show_matches_position(client: Client, index: Index) -> Result<(), Error> {
724724
setup_test_index(&client, &index).await?;
725725

726-
let mut query = Query::new(&index);
726+
let mut query = SearchQuery::new(&index);
727727
query.with_query("dolor text");
728728
query.with_show_matches_position(true);
729729
let results: SearchResults<Document> = index.execute_query(&query).await?;
@@ -747,7 +747,7 @@ mod tests {
747747
async fn test_phrase_search(client: Client, index: Index) -> Result<(), Error> {
748748
setup_test_index(&client, &index).await?;
749749

750-
let mut query = Query::new(&index);
750+
let mut query = SearchQuery::new(&index);
751751
query.with_query("harry \"of Fire\"");
752752
let results: SearchResults<Document> = index.execute_query(&query).await?;
753753

0 commit comments

Comments
 (0)