Skip to content

Support user-dictionary loading #660 #689

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

Merged
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
6 changes: 6 additions & 0 deletions .code-samples.meilisearch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,12 @@ update_faceting_settings_1: |-
client.index("books").updateFacetingSettings(newFaceting);
reset_faceting_settings_1: |-
client.index("books").resetFacetingSettings();
get_dictionary_1: |-
client.index("books").getDictionarySettings();
update_dictionary_1: |-
client.index("books").updateDictionarySettings(new String[] {"J. R. R.", "W. E. B."});
reset_dictionary_1: |-
client.index("books").resetDictionarySettings();
get_index_stats_1: |-
client.index("movies").getStats();
get_indexes_stats_1: |-
Expand Down
37 changes: 37 additions & 0 deletions src/main/java/com/meilisearch/sdk/Index.java
Original file line number Diff line number Diff line change
Expand Up @@ -832,6 +832,43 @@ public TaskInfo resetFacetingSettings() throws MeilisearchException {
return this.settingsHandler.resetFacetingSettings(this.uid);
}

/**
* Gets the dictionary settings of the index
*
* @return dictionary of a given uid as String
* @throws MeilisearchException if an error occurs
* @see <a href="https://www.meilisearch.com/docs/reference/api/settings#get-dictionary">API
* specification</a>
*/
public String[] getDictionarySettings() throws MeilisearchException {
return this.settingsHandler.getDictionarySettings(this.uid);
}

/**
* Updates the dictionary settings of the index
*
* @param dictionary An array of strings that contains the dictionary.
* @return TaskInfo instance
* @throws MeilisearchException if an error occurs
* @see <a href="https://www.meilisearch.com/docs/reference/api/settings#update-dictionary">API
* specification</a>
*/
public TaskInfo updateDictionarySettings(String[] dictionary) throws MeilisearchException {
return this.settingsHandler.updateDictionarySettings(this.uid, dictionary);
}

/**
* Resets the dictionary settings of the index
*
* @return TaskInfo instance
* @throws MeilisearchException if an error occurs
* @see <a href="https://www.meilisearch.com/docs/reference/api/settings#reset-dictionary">API
* specification</a>
*/
public TaskInfo resetDictionarySettings() throws MeilisearchException {
return this.settingsHandler.resetDictionarySettings(this.uid);
}

/**
* Gets extended information and metrics about indexes and the Meilisearch database
*
Expand Down
38 changes: 38 additions & 0 deletions src/main/java/com/meilisearch/sdk/SettingsHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,44 @@ TaskInfo resetFacetingSettings(String uid) throws MeilisearchException {
settingsPath(uid).addSubroute("faceting").getURL(), TaskInfo.class);
}

/**
* Gets the dictionary settings of the index
*
* @param uid Index identifier
* @return an array of strings that contains the dictionary
* @throws MeilisearchException if an error occurs
*/
String[] getDictionarySettings(String uid) throws MeilisearchException {
return httpClient.get(settingsPath(uid).addSubroute("dictionary").getURL(), String[].class);
}

/**
* Updates the dictionary settings of the index
*
* @param uid Index identifier
* @param dictionary an array of strings that contains the new dictionary settings
* @return TaskInfo instance
* @throws MeilisearchException if an error occurs
*/
TaskInfo updateDictionarySettings(String uid, String[] dictionary) throws MeilisearchException {
return httpClient.put(
settingsPath(uid).addSubroute("dictionary").getURL(),
dictionary == null ? httpClient.jsonHandler.encode(dictionary) : dictionary,
TaskInfo.class);
}

/**
* Resets the dictionary settings of the index
*
* @param uid Index identifier
* @return TaskInfo instance
* @throws MeilisearchException if an error occurs
*/
TaskInfo resetDictionarySettings(String uid) throws MeilisearchException {
return httpClient.delete(
settingsPath(uid).addSubroute("dictionary").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 @@ -26,6 +26,7 @@ public class Settings {
protected TypoTolerance typoTolerance;
protected Pagination pagination;
protected Faceting faceting;
protected String[] dictionary;

public Settings() {}
}
89 changes: 89 additions & 0 deletions src/test/java/com/meilisearch/sdk/SettingsHandlerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
package com.meilisearch.sdk;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.*;
import static org.hamcrest.Matchers.equalTo;

import com.meilisearch.integration.classes.AbstractIT;
import com.meilisearch.integration.classes.TestData;
import com.meilisearch.sdk.model.Settings;
import com.meilisearch.sdk.model.TaskInfo;
import com.meilisearch.sdk.utils.Movie;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

class SettingsHandlerTest extends AbstractIT {

private TestData<Movie> testData;

@BeforeEach
public void initialize() {
setUp();
setUpJacksonClient();
if (testData == null) testData = this.getTestData(MOVIES_INDEX, Movie.class);
cleanup();
}

@AfterAll
static void cleanMeilisearch() {
cleanup();
}

/** Tests of the dictionary setting methods */
@Test
@DisplayName("Test get dictionary settings by uid")
public void testGetDictionarySettings() throws Exception {
Index index = createIndex("testGetDictionarySettings");
Settings initialSettings = index.getSettings();
String[] initialDictionary = index.getDictionarySettings();

assertThat(initialDictionary, is(arrayWithSize(initialSettings.getDictionary().length)));
assertThat(initialDictionary, is(equalTo(initialSettings.getDictionary())));
}

@Test
@DisplayName("Test update dictionary settings")
public void testUpdateDictionarySettings() throws Exception {
Index index = createIndex("testUpdateDictionarySettings");
String[] initialDictionary = index.getDictionarySettings();
String[] newDictionary = {"J. R. R.", "W. E. B."};

index.waitForTask(index.updateDictionarySettings(newDictionary).getTaskUid());
String[] updatedDictionarySettings = index.getDictionarySettings();

assertThat(updatedDictionarySettings, is(arrayWithSize(newDictionary.length)));
assertThat(updatedDictionarySettings, is(equalTo(newDictionary)));
assertThat(updatedDictionarySettings, is(not(arrayWithSize(initialDictionary.length))));
}

@Test
@DisplayName("Test reset dictionary settings")
public void testResetDictionarySettings() throws Exception {
Index index = createIndex("testResetDictionarySettings");
String[] initialDictionary = index.getDictionarySettings();
String[] newDictionary = {"J. R. R.", "W. E. B."};

index.waitForTask(index.updateDictionarySettings(newDictionary).getTaskUid());
String[] updatedDictionarySettings = index.getDictionarySettings();

index.waitForTask(index.resetDictionarySettings().getTaskUid());
String[] dictionaryAfterReset = index.getDictionarySettings();

assertThat(updatedDictionarySettings, is(arrayWithSize(newDictionary.length)));
assertThat(updatedDictionarySettings, is(equalTo(newDictionary)));
assertThat(updatedDictionarySettings, is(not(arrayWithSize(initialDictionary.length))));
assertThat(dictionaryAfterReset, is(not(arrayWithSize(updatedDictionarySettings.length))));
assertThat(dictionaryAfterReset, is(arrayWithSize(initialDictionary.length)));
assertThat(dictionaryAfterReset, is(equalTo(initialDictionary)));
}

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

return index;
}
}