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

Add support to the pagination setting customization at the index level #568

Merged
merged 13 commits into from
Mar 6, 2023
Merged
Show file tree
Hide file tree
Changes from 10 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
48 changes: 36 additions & 12 deletions src/main/java/com/meilisearch/sdk/Index.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,7 @@
package com.meilisearch.sdk;

import com.meilisearch.sdk.exceptions.MeilisearchException;
import com.meilisearch.sdk.model.DocumentQuery;
import com.meilisearch.sdk.model.DocumentsQuery;
import com.meilisearch.sdk.model.IndexStats;
import com.meilisearch.sdk.model.Results;
import com.meilisearch.sdk.model.SearchResult;
import com.meilisearch.sdk.model.Searchable;
import com.meilisearch.sdk.model.Settings;
import com.meilisearch.sdk.model.Task;
import com.meilisearch.sdk.model.TaskInfo;
import com.meilisearch.sdk.model.TasksQuery;
import com.meilisearch.sdk.model.TasksResults;
import com.meilisearch.sdk.model.TypoTolerance;
import com.meilisearch.sdk.model.*;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
Expand All @@ -27,6 +16,7 @@ public class Index implements Serializable {
@Getter protected String uid;
@Getter protected String primaryKey;
@Getter protected String createdAt;
@Getter protected Pagination pagination;
@Getter @ToString.Exclude protected String updatedAt;
@Getter @ToString.Exclude protected Config config;
@ToString.Exclude protected Documents documents;
Expand Down Expand Up @@ -674,6 +664,40 @@ public TaskInfo resetTypoToleranceSettings() throws MeilisearchException {
return this.settingsHandler.resetTypoToleranceSettings(this.uid);
}

/**
* Gets the pagination field of the index Refer
* https://docs.meilisearch.com/reference/api/settings.html#get-pagination-settings
*
* @return Pagination instance from Index
* @throws MeilisearchException if an error occurs
*/
public Pagination getPaginationSettings() throws MeilisearchException {
return this.settingsHandler.getPaginationSettings(this.uid);
}

/**
* Updates the pagination field of the index Refer
* https://docs.meilisearch.com/reference/api/settings.html#update-pagination-settings
*
* @param pagination A pagination instance
* @return TaskInfo instance
* @throws MeilisearchException if an error occurs
*/
public TaskInfo updatePaginationSettings(Pagination pagination) throws MeilisearchException {
return this.settingsHandler.updatePaginationSettings(this.uid, pagination);
}

/**
* Resets the pagination field of the index Refer
* https://docs.meilisearch.com/reference/api/settings.html#reset-pagination-settings
*
* @return TaskInfo instance
* @throws MeilisearchException if an error occurs
*/
public TaskInfo resetPaginationSettings() throws MeilisearchException {
return this.settingsHandler.resetPaginationSettings(this.uid);
}

/**
* Gets extended information and metrics about indexes and the Meilisearch database Refer
* https://docs.meilisearch.com/reference/api/stats.html#stats-object
Expand Down
41 changes: 41 additions & 0 deletions src/main/java/com/meilisearch/sdk/SettingsHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.meilisearch.sdk.exceptions.MeilisearchException;
import com.meilisearch.sdk.http.URLBuilder;
import com.meilisearch.sdk.model.Pagination;
import com.meilisearch.sdk.model.Settings;
import com.meilisearch.sdk.model.TaskInfo;
import com.meilisearch.sdk.model.TypoTolerance;
Expand Down Expand Up @@ -392,6 +393,46 @@ TaskInfo resetTypoToleranceSettings(String uid) throws MeilisearchException {
settingsPath(uid).addSubroute("typo-tolerance").getURL(), TaskInfo.class);
}

/**
* Gets the pagination settings of the index
*
* @param uid Index identifier
* @return a Pagination instance that contains all pagination settings
* @throws MeilisearchException if an error occurs
*/
Pagination getPaginationSettings(String uid) throws MeilisearchException {
return httpClient.get(
settingsPath(uid).addSubroute("pagination").getURL(), Pagination.class);
}

/**
* Updates the pagination settings of the index
*
* @param uid Index identifier
* @param pagination a Pagination instance that contains the new pagination settings
* @return TaskInfo instance
* @throws MeilisearchException if an error occurs
*/
TaskInfo updatePaginationSettings(String uid, Pagination pagination)
throws MeilisearchException {
return httpClient.patch(
settingsPath(uid).addSubroute("pagination").getURL(),
pagination == null ? httpClient.jsonHandler.encode(pagination) : pagination,
TaskInfo.class);
}

/**
* Resets the pagination settings of the index
*
* @param uid Index identifier
* @return TaskInfo instance
* @throws MeilisearchException if an error occurs
*/
TaskInfo resetPaginationSettings(String uid) throws MeilisearchException {
return httpClient.delete(
settingsPath(uid).addSubroute("pagination").getURL(), TaskInfo.class);
}

/** Creates an URLBuilder for the constant route settings */
private URLBuilder settingsPath(String uid) {
return new URLBuilder("/indexes").addSubroute(uid).addSubroute("/settings");
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/meilisearch/sdk/model/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
@Setter
@Accessors(chain = true)
public class Settings {

protected HashMap<String, String[]> synonyms;
protected String[] stopWords;
protected String[] rankingRules;
Expand All @@ -23,6 +24,7 @@ public class Settings {
protected String[] displayedAttributes;
protected String[] sortableAttributes;
protected TypoTolerance typoTolerance;
protected Pagination pagination;

public Settings() {}
}
46 changes: 46 additions & 0 deletions src/test/java/com/meilisearch/integration/SettingsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import com.meilisearch.integration.classes.AbstractIT;
import com.meilisearch.integration.classes.TestData;
import com.meilisearch.sdk.Index;
import com.meilisearch.sdk.model.Pagination;
import com.meilisearch.sdk.model.Settings;
import com.meilisearch.sdk.model.TaskInfo;
import com.meilisearch.sdk.model.TypoTolerance;
Expand Down Expand Up @@ -844,6 +845,51 @@ public void testUpdateDistinctAttributeSettingsUsingNull() throws Exception {
assertEquals(initialDistinctAttribute, resetDistinctAttribute);
}

/** Tests of the pagination setting methods */
@Test
@DisplayName("Test get pagination settings by uid")
public void testGetPaginationSettings() throws Exception {
Index index = createIndex("testGetPaginationSettings");
Settings initialSettings = index.getSettings();
Pagination initialPagination = index.getPaginationSettings();

assertEquals(initialSettings.getPagination().getMaxTotalHits(), 1000);
assertNotNull(initialPagination.getMaxTotalHits());
}

@Test
@DisplayName("Test update pagination settings")
public void testUpdatePaginationSettings() throws Exception {
Index index = createIndex("testUpdatePaginationSettings");
Pagination newPagination = new Pagination();

Integer MaxTotalHitsTypos = 100;

newPagination.setMaxTotalHits(MaxTotalHitsTypos);
index.waitForTask(index.updatePaginationSettings(newPagination).getTaskUid());
Pagination updatedPagination = index.getPaginationSettings();

assertTrue(updatedPagination.getMaxTotalHits() == 100);
dennxa marked this conversation as resolved.
Show resolved Hide resolved
}

@Test
@DisplayName("Test reset pagination settings")
public void testResetPaginationSettings() throws Exception {
Index index = createIndex("testResetPaginationSettings");

Pagination initialPagination = index.getPaginationSettings();
Pagination newPagination = new Pagination();
index.waitForTask(index.updatePaginationSettings(newPagination).getTaskUid());
dennxa marked this conversation as resolved.
Show resolved Hide resolved
Pagination updatedPagination = index.getPaginationSettings();

index.waitForTask(index.resetPaginationSettings().getTaskUid());
Pagination paginationAfterReset = index.getPaginationSettings();

assertTrue(initialPagination.getMaxTotalHits() == 1000);
assertTrue(updatedPagination.getMaxTotalHits() == 100);
assertTrue(paginationAfterReset.getMaxTotalHits() == 1000);
dennxa marked this conversation as resolved.
Show resolved Hide resolved
}

private Index createIndex(String indexUid) throws Exception {
Index index = client.index(indexUid);
TaskInfo updateInfo = index.addDocuments(testData.getRaw());
Expand Down