Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Changes related to the next MeiliSearch release (v0.22.0) #169

Merged
merged 9 commits into from
Sep 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 63 additions & 6 deletions .code-samples.meilisearch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -82,9 +82,10 @@ update_settings_1: |-
"typo",
"proximity",
"attribute",
"sort",
"exactness",
"desc(release_date)",
"desc(rank)"
"release_date:desc",
"rank:desc"
])
.with_distinct_attribute("movie_id")
.with_searchable_attributes([
Expand All @@ -103,6 +104,10 @@ update_settings_1: |-
"a",
"an"
])
.with_sortable_attributes([
"title",
"release_date"
])
.with_synonyms(synonyms);

let progress: Progress = movies.set_settings(&settings).await.unwrap();
Expand Down Expand Up @@ -134,9 +139,10 @@ update_ranking_rules_1: |-
"typo",
"proximity",
"attribute",
"sort",
"exactness",
"asc(release_date)",
"desc(rank)",
"release_date:asc",
"rank:desc",
];

let progress: Progress = movies.set_ranking_rules(&ranking_rules).await.unwrap();
Expand Down Expand Up @@ -330,9 +336,10 @@ settings_guide_ranking_rules_1: |-
"typo",
"proximity",
"attribute",
"sort",
"exactness",
"asc(release_date)",
"desc(rank)",
"release_date:asc",
"rank:desc",
];

let progress: Progress = movies.set_ranking_rules(&ranking_rules).await.unwrap();
Expand Down Expand Up @@ -576,3 +583,53 @@ phrase_search_1: |-
.execute()
.await
.unwrap();
sorting_guide_update_sortable_attributes_1: |-
let sortable_attributes = [
"author",
"price"
];

let progress: Progress = books.set_sortable_attributes(&sortable_attributes).await.unwrap();
sorting_guide_update_ranking_rules_1: |-
let ranking_rules = [
"words",
"sort",
"typo",
"proximity",
"attribute",
"exactness"
];

let progress: Progress = books.set_ranking_rules(&ranking_rules).await.unwrap();
sorting_guide_sort_parameter_1: |-
let results: SearchResults<Books> = books.search()
.with_query("science fiction")
.with_sort(&["price:asc"])
.execute()
.await
.unwrap();
sorting_guide_sort_parameter_2: |-
let results: SearchResults<Books> = books.search()
.with_query("butler")
.with_sort(&["author:desc"])
.execute()
.await
.unwrap();
get_sortable_attributes_1: |-
let sortable_attributes: Vec<String> = books.get_sortable_attributes().await.unwrap();
update_sortable_attributes_1: |-
let sortable_attributes = [
"price",
"author"
];

let progress: Progress = books.set_sortable_attributes(&sortable_attributes).await.unwrap();
reset_sortable_attributes_1: |-
let progress: Progress = books.reset_sortable_attributes().await.unwrap();
search_parameter_guide_sort_1: |-
let results: SearchResults<Books> = books.search()
.with_query("science fiction")
.with_sort(&["price:asc"])
.execute()
.await
.unwrap();
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ WARNING: `meilisearch-sdk` will panic if no Window is available (ex: Web extensi

## 🤖 Compatibility with MeiliSearch

This package only guarantees the compatibility with the [version v0.21.0 of MeiliSearch](https://github.com/meilisearch/MeiliSearch/releases/tag/v0.21.0).
This package only guarantees the compatibility with the [version v0.22.0 of MeiliSearch](https://github.com/meilisearch/MeiliSearch/releases/tag/v0.22.0).

## ⚙️ Development Workflow and Contributing

Expand Down
2 changes: 1 addition & 1 deletion README.tpl
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ See our [Documentation](https://docs.meilisearch.com/learn/tutorials/getting_sta

## 🤖 Compatibility with MeiliSearch

This package only guarantees the compatibility with the [version v0.21.0 of MeiliSearch](https://github.com/meilisearch/MeiliSearch/releases/tag/v0.21.0).
This package only guarantees the compatibility with the [version v0.22.0 of MeiliSearch](https://github.com/meilisearch/MeiliSearch/releases/tag/v0.22.0).

## ⚙️ Development Workflow and Contributing

Expand Down
2 changes: 2 additions & 0 deletions src/progress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,8 @@ pub struct SettingsUpdate {
pub synonyms: Option<BTreeMap<String, Vec<String>>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub filterable_attributes: Option<Vec<String>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub sortable_attributes: Option<Vec<String>>,
}

#[allow(clippy::large_enum_variant)]
Expand Down
29 changes: 29 additions & 0 deletions src/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,9 @@ pub struct Query<'a> {
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(serialize_with = "serialize_with_wildcard")]
pub facets_distribution: Option<Selectors<&'a [&'a str]>>,
/// Attributes to sort.
#[serde(skip_serializing_if = "Option::is_none")]
pub sort: Option<&'a [&'a str]>,
/// Attributes to display in the returned documents.
///
/// Can be set to a [wildcard value](enum.Selectors.html#variant.All) that will select all existing attributes.
Expand Down Expand Up @@ -197,6 +200,7 @@ impl<'a> Query<'a> {
offset: None,
limit: None,
filter: None,
sort: None,
facets_distribution: None,
attributes_to_retrieve: None,
attributes_to_crop: None,
Expand Down Expand Up @@ -228,6 +232,13 @@ impl<'a> Query<'a> {
self.facets_distribution = Some(facets_distribution);
self
}
pub fn with_sort<'b>(
&'b mut self,
sort: &'a [&'a str],
) -> &'b mut Query<'a> {
self.sort = Some(sort);
self
}
pub fn with_attributes_to_retrieve<'b>(
&'b mut self,
attributes_to_retrieve: Selectors<&'a [&'a str]>,
Expand Down Expand Up @@ -311,6 +322,7 @@ mod tests {
Document { id: 9, kind: "title".into(), value: "Harry Potter and the Deathly Hallows".to_string() },
], None).await.unwrap();
index.set_filterable_attributes(["kind", "value"]).await.unwrap();
index.set_sortable_attributes(["title"]).await.unwrap();
sleep(Duration::from_secs(1));
index
}
Expand Down Expand Up @@ -448,6 +460,23 @@ mod tests {
.unwrap();
}

#[async_test]
async fn test_query_sort() {
let client = Client::new("http://localhost:7700", "masterKey");
let index = setup_test_index(&client, "test_query_sort").await;

let mut query = Query::new(&index);
query.with_query("harry potter");
query.with_sort(&["title:desc"]);
let results: SearchResults<Document> = index.execute_query(&query).await.unwrap();
assert_eq!(results.hits.len(), 7);

client
.delete_index("test_query_sort")
.await
.unwrap();
}

#[async_test]
async fn test_query_attributes_to_crop() {
let client = Client::new("http://localhost:7700", "masterKey");
Expand Down
95 changes: 88 additions & 7 deletions src/settings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,12 @@ pub struct Settings {
/// List of [ranking rules](https://docs.meilisearch.com/learn/core_concepts/relevancy.html#order-of-the-rules) sorted by order of importance
#[serde(skip_serializing_if = "Option::is_none")]
pub ranking_rules: Option<Vec<String>>,
/// Attributes to use as [facets](https://docs.meilisearch.com/reference/features/faceted_search.html)
/// Attributes to use for [filtering and faceted search](https://docs.meilisearch.com/reference/features/filtering_and_faceted_search.html)
#[serde(skip_serializing_if = "Option::is_none")]
pub filterable_attributes: Option<Vec<String>>,
/// Attributes to sort
#[serde(skip_serializing_if = "Option::is_none")]
pub sortable_attributes: Option<Vec<String>>,
/// Search returns documents with distinct (different) values of the given field
#[serde(skip_serializing_if = "Option::is_none")]
pub distinct_attribute: Option<String>,
Expand Down Expand Up @@ -161,6 +164,7 @@ impl Settings {
stop_words: None,
ranking_rules: None,
filterable_attributes: None,
sortable_attributes: None,
distinct_attribute: None,
searchable_attributes: None,
displayed_attributes: None,
Expand Down Expand Up @@ -198,6 +202,12 @@ impl Settings {
..self
}
}
pub fn with_sortable_attributes<T: IntoVecString>(self, sortable_attributes: T) -> Settings {
Settings {
sortable_attributes: Some(sortable_attributes.convert()),
..self
}
}
pub fn with_distinct_attribute<T: Into<String>>(self, distinct_attribute: T) -> Settings {
Settings {
distinct_attribute: Some(distinct_attribute.into()),
Expand Down Expand Up @@ -295,7 +305,7 @@ impl Index {
).await?)
}

/// Get [attributes for faceting](https://docs.meilisearch.com/reference/features/faceted_search.html) of the Index.
/// Get [filterable attributes](https://docs.meilisearch.com/reference/features/filtering_and_faceted_search.html) of the Index.
///
/// ```
/// # use meilisearch_sdk::{client::*, indexes::*, document::*};
Expand All @@ -314,6 +324,25 @@ impl Index {
).await?)
}

/// Get [sortable attributes](https://docs.meilisearch.com/reference/features/sorting.html) of the Index.
///
/// ```
/// # use meilisearch_sdk::{client::*, indexes::*, document::*};
/// # futures::executor::block_on(async move {
/// let client = Client::new("http://localhost:7700", "masterKey");
/// let movie_index = client.get_or_create("movies").await.unwrap();
/// let sortable_attributes = movie_index.get_sortable_attributes().await.unwrap();
/// # });
/// ```
pub async fn get_sortable_attributes(&self) -> Result<Vec<String>, Error> {
Ok(request::<(), Vec<String>>(
&format!("{}/indexes/{}/settings/sortable-attributes", self.host, self.uid),
&self.api_key,
Method::Get,
200,
).await?)
}

/// Get the [distinct attribute](https://docs.meilisearch.com/reference/features/settings.html#distinct-attribute) of the Index.
///
/// ```
Expand Down Expand Up @@ -472,9 +501,10 @@ impl Index {
/// "typo",
/// "proximity",
/// "attribute",
/// "sort",
/// "exactness",
/// "asc(release_date)",
/// "desc(rank)",
/// "release_date:asc",
/// "rank:desc",
/// ];
/// let progress = movie_index.set_ranking_rules(ranking_rules).await.unwrap();
/// # std::thread::sleep(std::time::Duration::from_secs(2));
Expand All @@ -491,7 +521,7 @@ impl Index {
.into_progress(self))
}

/// Update [attributes for faceting](https://docs.meilisearch.com/reference/features/faceted_search.html) of the index.
/// Update [filterable attributes](https://docs.meilisearch.com/reference/features/filtering_and_faceted_search.html) of the index.
///
/// # Example
///
Expand All @@ -517,6 +547,32 @@ impl Index {
.into_progress(self))
}

/// Update [sortable attributes](https://docs.meilisearch.com/reference/features/sorting.html) of the index.
///
/// # Example
///
/// ```
/// # use meilisearch_sdk::{client::*, indexes::*, document::*, settings::Settings};
/// # futures::executor::block_on(async move {
/// let client = Client::new("http://localhost:7700", "masterKey");
/// let mut movie_index = client.get_or_create("movies").await.unwrap();
///
/// let sortable_attributes = ["genre", "director"];
/// let progress = movie_index.set_sortable_attributes(&sortable_attributes).await.unwrap();
/// # std::thread::sleep(std::time::Duration::from_secs(2));
/// # progress.get_status().await.unwrap();
/// # });
/// ```
pub async fn set_sortable_attributes(&self, sortable_attributes: impl IntoVecString) -> Result<Progress, Error> {
Ok(request::<Vec<String>, ProgressJson>(
&format!("{}/indexes/{}/settings/sortable-attributes", self.host, self.uid),
&self.api_key,
Method::Post(sortable_attributes.convert()),
202,
).await?
.into_progress(self))
}

/// Update the [distinct attribute](https://docs.meilisearch.com/reference/features/settings.html#distinct-attribute) of the index.
///
/// # Example
Expand Down Expand Up @@ -669,7 +725,7 @@ impl Index {
}

/// Reset [ranking rules](https://docs.meilisearch.com/learn/core_concepts/relevancy.html#ranking-rules) of the index to default value.
/// Default value: ["words", "typo", "proximity", "attribute", "exactness"].
/// Default value: ["words", "typo", "proximity", "attribute", "sort", "exactness"].
///
/// # Example
///
Expand All @@ -694,7 +750,7 @@ impl Index {
.into_progress(self))
}

/// Reset [attributes for faceting](https://docs.meilisearch.com/reference/features/faceted_search.html) of the index.
/// Reset [filterable attributes]https://docs.meilisearch.com/reference/features/filtering_and_faceted_search.html) of the index.
///
/// # Example
///
Expand All @@ -719,6 +775,31 @@ impl Index {
.into_progress(self))
}

/// Reset [sortable attributes]https://docs.meilisearch.com/reference/features/sorting.html) of the index.
///
/// # Example
///
/// ```
/// # use meilisearch_sdk::{client::*, indexes::*, document::*, settings::Settings};
/// # futures::executor::block_on(async move {
/// let client = Client::new("http://localhost:7700", "masterKey");
/// let mut movie_index = client.get_or_create("movies").await.unwrap();
///
/// let progress = movie_index.reset_sortable_attributes().await.unwrap();
/// # std::thread::sleep(std::time::Duration::from_secs(2));
/// # progress.get_status().await.unwrap();
/// # });
/// ```
pub async fn reset_sortable_attributes(&self) -> Result<Progress, Error> {
Ok(request::<(), ProgressJson>(
&format!("{}/indexes/{}/settings/sortable-attributes", self.host, self.uid),
&self.api_key,
Method::Delete,
202,
).await?
.into_progress(self))
}

/// Reset the [distinct attribute](https://docs.meilisearch.com/reference/features/settings.html#distinct-attribute) of the index.
///
/// # Example
Expand Down