Skip to content

Commit 78c5474

Browse files
Merge #342
342: Add support to the pagination setting customization at the index level r=bidoubiwa a=vishalsodani # Pull Request ## What does this PR do? Fixes #304 <!-- 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]> Co-authored-by: Vishal Sodani <[email protected]>
2 parents cc16278 + 2c267ac commit 78c5474

File tree

3 files changed

+184
-3
lines changed

3 files changed

+184
-3
lines changed

.code-samples.meilisearch.yaml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,26 @@ reset_synonyms_1: |-
220220
.reset_synonyms()
221221
.await
222222
.unwrap();
223+
get_pagination_settings_1: |-
224+
let pagination: PaginationSetting = client
225+
.index("books")
226+
.get_pagination()
227+
.await
228+
.unwrap();
229+
update_pagination_settings_1: |-
230+
let pagination = PaginationSetting {max_total_hits:100};
231+
232+
let task: TaskInfo = client
233+
.index("books")
234+
.set_pagination(pagination)
235+
.await
236+
.unwrap();
237+
reset_pagination_settings_1: |-
238+
let task: TaskInfo = client
239+
.index("books")
240+
.reset_pagination()
241+
.await
242+
.unwrap();
223243
get_stop_words_1: |-
224244
let stop_words: Vec<String> = client
225245
.index("movies")
@@ -657,6 +677,14 @@ settings_guide_searchable_1: |-
657677
.set_settings(&settings)
658678
.await
659679
.unwrap();
680+
settings_guide_pagination_1: |-
681+
let pagination = PaginationSetting {max_total_hits:100};
682+
683+
let task: TaskInfo = client
684+
.index("movies")
685+
.set_pagination(pagination)
686+
.await
687+
.unwrap();
660688
settings_guide_displayed_1: |-
661689
let settings = Settings::new()
662690
.with_displayed_attributes([

src/settings.rs

Lines changed: 155 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@ use crate::{
77
use serde::{Deserialize, Serialize};
88
use std::collections::HashMap;
99

10+
#[derive(Serialize, Deserialize, Default, Debug, Clone, PartialEq, Eq, Copy)]
11+
#[serde(rename_all = "camelCase")]
12+
pub struct PaginationSetting {
13+
pub max_total_hits: usize
14+
}
15+
1016
#[derive(Serialize, Deserialize, Default, Debug, Clone, Eq, PartialEq)]
1117
#[serde(rename_all = "camelCase")]
1218
pub struct FacetingSettings {
@@ -65,6 +71,9 @@ pub struct Settings {
6571
/// Fields displayed in the returned documents
6672
#[serde(skip_serializing_if = "Option::is_none")]
6773
pub displayed_attributes: Option<Vec<String>>,
74+
/// Pagination settings
75+
#[serde(skip_serializing_if = "Option::is_none")]
76+
pub pagination: Option<PaginationSetting>,
6877
/// Faceting settings
6978
#[serde(skip_serializing_if = "Option::is_none")]
7079
pub faceting: Option<FacetingSettings>,
@@ -83,6 +92,7 @@ impl Settings {
8392
distinct_attribute: None,
8493
searchable_attributes: None,
8594
displayed_attributes: None,
95+
pagination: None,
8696
faceting: None,
8797
}
8898
}
@@ -123,6 +133,13 @@ impl Settings {
123133
}
124134
}
125135

136+
pub fn with_pagination(self, pagination_settings: PaginationSetting) -> Settings {
137+
Settings {
138+
pagination: Some(pagination_settings),
139+
..self
140+
}
141+
}
142+
126143
pub fn with_ranking_rules(
127144
self,
128145
ranking_rules: impl IntoIterator<Item = impl AsRef<str>>,
@@ -272,6 +289,35 @@ impl Index {
272289
.await
273290
}
274291

292+
/// Get [pagination](https://docs.meilisearch.com/learn/configuration/settings.html#pagination) of the [Index].
293+
///
294+
/// ```
295+
/// # use meilisearch_sdk::{client::*, indexes::*};
296+
/// #
297+
/// # let MEILISEARCH_HOST = option_env!("MEILISEARCH_HOST").unwrap_or("http://localhost:7700");
298+
/// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey");
299+
/// #
300+
/// # futures::executor::block_on(async move {
301+
/// let client = Client::new(MEILISEARCH_HOST, MEILISEARCH_API_KEY);
302+
/// # client.create_index("get_pagination", None).await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
303+
/// let index = client.index("get_pagination");
304+
/// let pagination = index.get_pagination().await.unwrap();
305+
/// # index.delete().await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
306+
/// # });
307+
/// ```
308+
pub async fn get_pagination(&self) -> Result<PaginationSetting, Error> {
309+
request::<(), PaginationSetting>(
310+
&format!(
311+
"{}/indexes/{}/settings/pagination",
312+
self.client.host, self.uid
313+
),
314+
&self.client.api_key,
315+
Method::Get(()),
316+
200,
317+
)
318+
.await
319+
}
320+
275321
/// Get [stop-words](https://docs.meilisearch.com/reference/features/stop_words.html) of the [Index].
276322
///
277323
/// ```
@@ -510,7 +556,7 @@ impl Index {
510556
/// # Example
511557
///
512558
/// ```
513-
/// # use meilisearch_sdk::{client::*, indexes::*, settings::Settings};
559+
/// # use meilisearch_sdk::{client::*, indexes::*, settings::{Settings, PaginationSetting}};
514560
/// #
515561
/// # let MEILISEARCH_HOST = option_env!("MEILISEARCH_HOST").unwrap_or("http://localhost:7700");
516562
/// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey");
@@ -522,7 +568,9 @@ impl Index {
522568
///
523569
/// let stop_words = vec![String::from("a"), String::from("the"), String::from("of")];
524570
/// let settings = Settings::new()
525-
/// .with_stop_words(stop_words.clone());
571+
/// .with_stop_words(stop_words.clone())
572+
/// .with_pagination(PaginationSetting {max_total_hits: 100}
573+
/// );
526574
///
527575
/// let task = index.set_settings(&settings).await.unwrap();
528576
/// # index.delete().await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
@@ -578,6 +626,38 @@ impl Index {
578626
.await
579627
}
580628

629+
/// Update [pagination](https://docs.meilisearch.com/learn/configuration/settings.html#pagination) of the [Index].
630+
///
631+
/// # Example
632+
///
633+
/// ```
634+
/// # use meilisearch_sdk::{client::*, indexes::*, settings::{Settings, PaginationSetting}};
635+
/// #
636+
/// # let MEILISEARCH_HOST = option_env!("MEILISEARCH_HOST").unwrap_or("http://localhost:7700");
637+
/// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey");
638+
/// #
639+
/// # futures::executor::block_on(async move {
640+
/// let client = Client::new(MEILISEARCH_HOST, MEILISEARCH_API_KEY);
641+
/// # client.create_index("set_pagination", None).await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
642+
/// let mut index = client.index("set_pagination");
643+
/// let pagination = PaginationSetting {max_total_hits:100};
644+
/// let task = index.set_pagination(pagination).await.unwrap();
645+
/// # index.delete().await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
646+
/// # });
647+
/// ```
648+
pub async fn set_pagination(&self, pagination: PaginationSetting,) -> Result<TaskInfo, Error> {
649+
request::<&PaginationSetting, TaskInfo>(
650+
&format!(
651+
"{}/indexes/{}/settings/pagination",
652+
self.client.host, self.uid
653+
),
654+
&self.client.api_key,
655+
Method::Patch(&pagination),
656+
202,
657+
)
658+
.await
659+
}
660+
581661
/// Update [stop-words](https://docs.meilisearch.com/reference/features/stop_words.html) of the [Index].
582662
///
583663
/// # Example
@@ -967,6 +1047,37 @@ impl Index {
9671047
.await
9681048
}
9691049

1050+
/// Reset [pagination](https://docs.meilisearch.com/learn/configuration/settings.html#pagination) of the [Index].
1051+
///
1052+
/// # Example
1053+
///
1054+
/// ```
1055+
/// # use meilisearch_sdk::{client::*, indexes::*, settings::Settings};
1056+
/// #
1057+
/// # let MEILISEARCH_HOST = option_env!("MEILISEARCH_HOST").unwrap_or("http://localhost:7700");
1058+
/// # let MEILISEARCH_API_KEY = option_env!("MEILISEARCH_API_KEY").unwrap_or("masterKey");
1059+
/// #
1060+
/// # futures::executor::block_on(async move {
1061+
/// let client = Client::new(MEILISEARCH_HOST, MEILISEARCH_API_KEY);
1062+
/// # client.create_index("reset_pagination", None).await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
1063+
/// let mut index = client.index("reset_pagination");
1064+
///
1065+
/// let task = index.reset_pagination().await.unwrap();
1066+
/// # index.delete().await.unwrap().wait_for_completion(&client, None, None).await.unwrap();
1067+
/// # });
1068+
/// ```
1069+
pub async fn reset_pagination(&self) -> Result<TaskInfo, Error> {
1070+
request::<(), TaskInfo>(
1071+
&format!(
1072+
"{}/indexes/{}/settings/pagination",
1073+
self.client.host, self.uid
1074+
),
1075+
&self.client.api_key,
1076+
Method::Delete,
1077+
202,
1078+
)
1079+
.await
1080+
}
9701081
/// Reset [stop-words](https://docs.meilisearch.com/reference/features/stop_words.html) of the [Index].
9711082
///
9721083
/// # Example
@@ -1285,4 +1396,46 @@ mod tests {
12851396

12861397
assert_eq!(faceting, res);
12871398
}
1399+
1400+
#[meilisearch_test]
1401+
async fn test_get_pagination(index: Index) {
1402+
let pagination = PaginationSetting {
1403+
max_total_hits: 1000,
1404+
};
1405+
1406+
let res = index.get_pagination().await.unwrap();
1407+
1408+
assert_eq!(pagination, res);
1409+
}
1410+
1411+
#[meilisearch_test]
1412+
async fn test_set_pagination(index: Index) {
1413+
let pagination = PaginationSetting {
1414+
max_total_hits: 11,
1415+
};
1416+
let task = index.set_pagination(pagination).await.unwrap();
1417+
index.wait_for_task(task, None, None).await.unwrap();
1418+
1419+
let res = index.get_pagination().await.unwrap();
1420+
1421+
assert_eq!(pagination, res);
1422+
}
1423+
1424+
#[meilisearch_test]
1425+
async fn test_reset_pagination(index: Index) {
1426+
let pagination = PaginationSetting {
1427+
max_total_hits: 10,
1428+
};
1429+
let default = PaginationSetting { max_total_hits: 1000};
1430+
1431+
let task = index.set_pagination(pagination).await.unwrap();
1432+
index.wait_for_task(task, None, None).await.unwrap();
1433+
1434+
let reset_task = index.reset_pagination().await.unwrap();
1435+
index.wait_for_task(reset_task, None, None).await.unwrap();
1436+
1437+
let res = index.get_pagination().await.unwrap();
1438+
1439+
assert_eq!(default, res);
1440+
}
12881441
}

src/tasks.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ pub enum TaskType {
2626
details: Option<IndexDeletion>,
2727
},
2828
SettingsUpdate {
29-
details: Option<Settings>,
29+
details: Box<Option<Settings>>,
3030
},
3131
DumpCreation {
3232
details: Option<DumpCreation>,

0 commit comments

Comments
 (0)