Skip to content

Commit

Permalink
Merge #577
Browse files Browse the repository at this point in the history
577: Add support to the faceting setting customization at the index level r=alallema a=MaAnCoSa

# Pull Request

## Related issue
Fixes #412 

## What does this PR do?
* Added new methods for faceting customization:
-GET /indexes/:index_uid/settings/faceting
-PATCH /indexes/:index_uid/settings/faceting
-DELETE /indexes/:index_uid/settings/faceting
* Added tests

## 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: “Manuel <“cota.manuel@rocketmail.com”>
Co-authored-by: Manuel Cota <108767897+MaAnCoSa@users.noreply.github.com>
  • Loading branch information
3 people authored Mar 30, 2023
2 parents 41072f7 + 48c93bc commit f40fdd0
Show file tree
Hide file tree
Showing 4 changed files with 124 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/main/java/com/meilisearch/sdk/Index.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ public class Index implements Serializable {
@Getter protected String primaryKey;
@Getter protected String createdAt;
@Getter protected Pagination pagination;
@Getter protected Faceting faceting;
@Getter @ToString.Exclude protected String updatedAt;
@Getter @ToString.Exclude protected Config config;
@ToString.Exclude protected Documents documents;
Expand Down Expand Up @@ -711,6 +712,40 @@ public TaskInfo resetPaginationSettings() throws MeilisearchException {
return this.settingsHandler.resetPaginationSettings(this.uid);
}

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

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

/**
* Resets the faceting field of the index Refer
* https://docs.meilisearch.com/reference/api/settings.html#reset-faceting-settings
*
* @return TaskInfo instance
* @throws MeilisearchException if an error occurs
*/
public TaskInfo resetFacetingSettings() throws MeilisearchException {
return this.settingsHandler.resetFacetingSettings(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
39 changes: 39 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.Faceting;
import com.meilisearch.sdk.model.Pagination;
import com.meilisearch.sdk.model.Settings;
import com.meilisearch.sdk.model.TaskInfo;
Expand Down Expand Up @@ -475,6 +476,44 @@ TaskInfo resetPaginationSettings(String uid) throws MeilisearchException {
settingsPath(uid).addSubroute("pagination").getURL(), TaskInfo.class);
}

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

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

/**
* Reset the faceting settings of the index
*
* @param uid Index identifier
* @return TaskInfo instance
* @throws MeilisearchException if an error occurs
*/
TaskInfo resetFacetingSettings(String uid) throws MeilisearchException {
return httpClient.delete(
settingsPath(uid).addSubroute("faceting").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
1 change: 1 addition & 0 deletions src/main/java/com/meilisearch/sdk/model/Settings.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class Settings {
protected String[] sortableAttributes;
protected TypoTolerance typoTolerance;
protected Pagination pagination;
protected Faceting faceting;

public Settings() {}
}
49 changes: 49 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.Faceting;
import com.meilisearch.sdk.model.Pagination;
import com.meilisearch.sdk.model.Settings;
import com.meilisearch.sdk.model.TaskInfo;
Expand Down Expand Up @@ -950,6 +951,54 @@ public void testResetPaginationSettings() throws Exception {
assertEquals(1000, paginationAfterReset.getMaxTotalHits());
}

/** Tests of the faceting setting methods */
@Test
@DisplayName("Test get faceting settings by uid")
public void testGetFacetingSettings() throws Exception {
Index index = createIndex("testGetFacetingSettings");
Settings initialSettings = index.getSettings();
Faceting initialFaceting = index.getFacetingSettings();

assertEquals(initialSettings.getFaceting().getMaxValuesPerFacet(), 100);
assertNotNull(initialFaceting.getMaxValuesPerFacet());
}

@Test
@DisplayName("Test update faceting settings")
public void testUpdateFacetingSettings() throws Exception {
Index index = createIndex("testUpdateFacetingSettings");
Faceting newFaceting = new Faceting();

Integer MaxValuesPerFacetTypos = 200;

newFaceting.setMaxValuesPerFacet(MaxValuesPerFacetTypos);
index.waitForTask(index.updateFacetingSettings(newFaceting).getTaskUid());
Faceting updatedFaceting = index.getFacetingSettings();

assertEquals(200, updatedFaceting.getMaxValuesPerFacet());
}

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

Faceting initialFaceting = index.getFacetingSettings();
Faceting newFaceting = new Faceting();

Integer MaxValuesPerFacetTypos = 200;
newFaceting.setMaxValuesPerFacet(MaxValuesPerFacetTypos);
index.waitForTask(index.updateFacetingSettings(newFaceting).getTaskUid());
Faceting updatedFaceting = index.getFacetingSettings();

index.waitForTask(index.resetFacetingSettings().getTaskUid());
Faceting facetingAfterReset = index.getFacetingSettings();

assertEquals(100, initialFaceting.getMaxValuesPerFacet());
assertEquals(200, updatedFaceting.getMaxValuesPerFacet());
assertEquals(100, facetingAfterReset.getMaxValuesPerFacet());
}

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

0 comments on commit f40fdd0

Please sign in to comment.