Skip to content

Commit d74e2bb

Browse files
Merge #510
510: #499 (attributesToSearchOn) r=irevoire a=brendon-shf # Pull Request ## Related issue Fixes #499 ## What does this PR do? - adds support for the attributesToSearchOn (which was added in meilisearch v1.3) to the rust client as `with_attributes_to_search_on` ## PR checklist Please check if your PR fulfills the following requirements: - [x] Does this PR fix an existing issue, or have you listed the changes applied in the PR description (and why they are needed)? - [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: brendon-shf <[email protected]>
2 parents e93c27b + f91e4dd commit d74e2bb

File tree

2 files changed

+38
-0
lines changed

2 files changed

+38
-0
lines changed

.code-samples.meilisearch.yaml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -692,6 +692,15 @@ search_parameter_guide_query_1: |-
692692
.execute()
693693
.await
694694
.unwrap();
695+
search_parameter_guide_attributes_to_search_on_1: |-
696+
let results: SearchResults<Movie> = client
697+
.index("movies")
698+
.search()
699+
.with_query("adventure")
700+
.with_attributes_to_search_on(&["overview"])
701+
.execute()
702+
.await
703+
.unwrap();
695704
search_parameter_guide_offset_1: |-
696705
let results: SearchResults<Movie> = client
697706
.index("movies")

src/search.rs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,13 @@ pub struct SearchQuery<'a> {
246246
/// Attributes to sort.
247247
#[serde(skip_serializing_if = "Option::is_none")]
248248
pub sort: Option<&'a [&'a str]>,
249+
/// Attributes to perform the search on.
250+
///
251+
/// Specify the subset of searchableAttributes for a search without modifying Meilisearch’s index settings.
252+
///
253+
/// **Default: all searchable attributes found in the documents.**
254+
#[serde(skip_serializing_if = "Option::is_none")]
255+
pub attributes_to_search_on: Option<&'a [&'a str]>,
249256
/// Attributes to display in the returned documents.
250257
///
251258
/// Can be set to a [wildcard value](enum.Selectors.html#variant.All) that will select all existing attributes.
@@ -323,6 +330,7 @@ impl<'a> SearchQuery<'a> {
323330
filter: None,
324331
sort: None,
325332
facets: None,
333+
attributes_to_search_on: None,
326334
attributes_to_retrieve: None,
327335
attributes_to_crop: None,
328336
crop_length: None,
@@ -430,6 +438,13 @@ impl<'a> SearchQuery<'a> {
430438
self.sort = Some(sort);
431439
self
432440
}
441+
pub fn with_attributes_to_search_on<'b>(
442+
&'b mut self,
443+
attributes_to_search_on: &'a [&'a str],
444+
) -> &'b mut SearchQuery<'a> {
445+
self.attributes_to_search_on = Some(attributes_to_search_on);
446+
self
447+
}
433448
pub fn with_attributes_to_retrieve<'b>(
434449
&'b mut self,
435450
attributes_to_retrieve: Selectors<&'a [&'a str]>,
@@ -830,6 +845,20 @@ mod tests {
830845
Ok(())
831846
}
832847

848+
#[meilisearch_test]
849+
async fn test_query_attributes_to_search_on(client: Client, index: Index) -> Result<(), Error> {
850+
setup_test_index(&client, &index).await?;
851+
852+
let results: SearchResults<Document> = index
853+
.search()
854+
.with_query("title")
855+
.with_attributes_to_search_on(&["kind"])
856+
.execute()
857+
.await?;
858+
assert_eq!(results.hits.len(), 8);
859+
Ok(())
860+
}
861+
833862
#[meilisearch_test]
834863
async fn test_query_sort(client: Client, index: Index) -> Result<(), Error> {
835864
setup_test_index(&client, &index).await?;

0 commit comments

Comments
 (0)