Skip to content

Commit

Permalink
Merge #710
Browse files Browse the repository at this point in the history
710: Add proximity precision settings support to the Meilisearch java client r=curquiza a=iMezemz

# Pull Request

## Related issue
Fixes #702

## What does this PR do?
- This PR adds support for the newly introduced proximity precision level feature introduced in v1.6, adds integration tests for the new methods and updates code samples to show how to use the newly added feature. 

## 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?



Co-authored-by: Mohamed Emad <mez_emz@yahoo.com>
  • Loading branch information
meili-bors[bot] and iMezemz authored Feb 26, 2024
2 parents 9a64a3a + 3ac9a0e commit f7f5337
Show file tree
Hide file tree
Showing 5 changed files with 143 additions and 0 deletions.
6 changes: 6 additions & 0 deletions .code-samples.meilisearch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -286,6 +286,12 @@ update_dictionary_1: |-
client.index("books").updateDictionarySettings(new String[] {"J. R. R.", "W. E. B."});
reset_dictionary_1: |-
client.index("books").resetDictionarySettings();
get_proximity_precision_settings_1: |-
client.index("books").getProximityPrecisionSettings();
update_proximity_precision_settings_1: |-
client.index("books").updateProximityPrecisionSettings("byAttribute");
reset_proximity_precision_settings_1: |-
client.index("books").resetProximityPrecisionSettings();
get_index_stats_1: |-
client.index("movies").getStats();
get_indexes_stats_1: |-
Expand Down
41 changes: 41 additions & 0 deletions src/main/java/com/meilisearch/sdk/Index.java
Original file line number Diff line number Diff line change
Expand Up @@ -869,6 +869,47 @@ public TaskInfo resetDictionarySettings() throws MeilisearchException {
return this.settingsHandler.resetDictionarySettings(this.uid);
}

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

/**
* Updates the proximity precision level of the index
*
* @param proximityPrecision A String: the proximity precision level.
* @return TaskInfo instance
* @throws MeilisearchException if an error occurs
* @see <a
* href="https://www.meilisearch.com/docs/reference/api/settings#update-proximity-precision-settings">API
* specification</a>
*/
public TaskInfo updateProximityPrecisionSettings(String proximityPrecision)
throws MeilisearchException {
return this.settingsHandler.updateProximityPrecisionSettings(this.uid, proximityPrecision);
}

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

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

/**
* Gets the proximity precision level of the index
*
* @param uid Index identifier
* @return a string of the proximity precision level
* @throws MeilisearchException if an error occurs
*/
String getProximityPrecisionSettings(String uid) throws MeilisearchException {
String response =
httpClient.get(
settingsPath(uid).addSubroute("proximity-precision").getURL(),
String.class);
return response.substring(1, response.length() - 1);
}

/**
* Updates the proximity precision level of the index
*
* @param uid Index identifier
* @param proximityPrecision a String that contains the new proximity precision level settings
* @return TaskInfo instance
* @throws MeilisearchException if an error occurs
*/
TaskInfo updateProximityPrecisionSettings(String uid, String proximityPrecision)
throws MeilisearchException {
return httpClient.put(
settingsPath(uid).addSubroute("proximity-precision").getURL(),
proximityPrecision == null
? httpClient.jsonHandler.encode(proximityPrecision)
: "\"" + proximityPrecision + "\"",
TaskInfo.class);
}

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

public Settings() {}
}
50 changes: 50 additions & 0 deletions src/test/java/com/meilisearch/integration/SettingsTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1089,6 +1089,56 @@ public void testResetFacetingSettings() throws Exception {
assertThat(facetingAfterReset.getMaxValuesPerFacet(), is(equalTo(100)));
}

/** Tests of the proximity precision setting methods */
@Test
@DisplayName("Test get proximity precision settings by uid")
public void testGetProximityPrecisionSettings() throws Exception {
Index index = createIndex("testGetProximityPrecisionSettings");
Settings initialSettings = index.getSettings();
String initialProximityPrecision = index.getProximityPrecisionSettings();

assertThat(initialProximityPrecision, is(equalTo(initialSettings.getProximityPrecision())));
}

@Test
@DisplayName("Test update proximity precision settings")
public void testUpdateProximityPrecisionSettings() throws Exception {
Index index = createIndex("testUpdateProximityPrecisionSettings");
String initialProximityPrecision = index.getProximityPrecisionSettings();
String newProximityPrecision = "byAttribute";

index.waitForTask(
index.updateProximityPrecisionSettings(newProximityPrecision).getTaskUid());
String updatedProximityPrecision = index.getProximityPrecisionSettings();

assertThat(updatedProximityPrecision, is(equalTo(newProximityPrecision)));
assertThat(updatedProximityPrecision, is(not(equalTo(initialProximityPrecision))));
}

@Test
@DisplayName("Test reset proximity precision settings")
public void testResetProximityPrecisionSettings() throws Exception {
Index index = createIndex("testResetProximityPrecisionSettings");
String initialProximityPrecision = index.getProximityPrecisionSettings();
String newProximityPrecision = "byAttribute";

index.waitForTask(
index.updateProximityPrecisionSettings(newProximityPrecision).getTaskUid());
String updatedProximityPrecision = index.getProximityPrecisionSettings();

index.waitForTask(index.resetProximityPrecisionSettings().getTaskUid());
String proximityPrecisionAfterReset = index.getProximityPrecisionSettings();

assertThat(updatedProximityPrecision, is(equalTo(newProximityPrecision)));
assertThat(updatedProximityPrecision, is(not(equalTo(initialProximityPrecision))));
assertThat(proximityPrecisionAfterReset, is(not(equalTo(updatedProximityPrecision))));
assertThat(
proximityPrecisionAfterReset,
is(equalTo(initialProximityPrecision))); // Resetting proximity precision
// changes it back to the default
// "byWord"
}

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

0 comments on commit f7f5337

Please sign in to comment.