-
Notifications
You must be signed in to change notification settings - Fork 148
Add dynamic search rules API support #982
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
Open
Fayupable
wants to merge
4
commits into
meilisearch:main
Choose a base branch
from
Fayupable:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
ec78f3c
Add dynamic search rules API support
Fayupable ec41e91
Add no-arg constructor and setters to DynamicSearchRule
Fayupable c41b8c9
fix: format code and ensure newline at end of file in DynamicSearchRu…
Fayupable d534011
fix: ensure newline at end of file in .code-samples.meilisearch.yaml
Fayupable File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
src/main/java/com/meilisearch/sdk/DynamicSearchRulesHandler.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| package com.meilisearch.sdk; | ||
|
|
||
| import com.meilisearch.sdk.exceptions.MeilisearchException; | ||
| import com.meilisearch.sdk.http.URLBuilder; | ||
| import com.meilisearch.sdk.model.DynamicSearchRule; | ||
| import com.meilisearch.sdk.model.DynamicSearchRulesQuery; | ||
| import com.meilisearch.sdk.model.Results; | ||
| import com.meilisearch.sdk.model.TaskInfo; | ||
|
|
||
| class DynamicSearchRulesHandler { | ||
| private final HttpClient httpClient; | ||
|
|
||
| protected DynamicSearchRulesHandler(Config config) { | ||
| httpClient = config.httpClient; | ||
| } | ||
|
|
||
| Results<DynamicSearchRule> listDynamicSearchRules(DynamicSearchRulesQuery query) | ||
| throws MeilisearchException { | ||
| return httpClient.post( | ||
| dynamicSearchRulesPath().getURL(), query, Results.class, DynamicSearchRule.class); | ||
| } | ||
|
|
||
| DynamicSearchRule getDynamicSearchRule(String uid) throws MeilisearchException { | ||
| return httpClient.get( | ||
| dynamicSearchRulesPath().addSubroute(uid).getURL(), DynamicSearchRule.class); | ||
| } | ||
|
|
||
| TaskInfo updateDynamicSearchRule(String uid, DynamicSearchRule dynamicSearchRule) | ||
| throws MeilisearchException { | ||
| return httpClient.patch( | ||
| dynamicSearchRulesPath().addSubroute(uid).getURL(), | ||
| dynamicSearchRule, | ||
| TaskInfo.class); | ||
| } | ||
|
|
||
| TaskInfo deleteDynamicSearchRule(String uid) throws MeilisearchException { | ||
| return httpClient.delete( | ||
| dynamicSearchRulesPath().addSubroute(uid).getURL(), TaskInfo.class); | ||
| } | ||
|
|
||
| private URLBuilder dynamicSearchRulesPath() { | ||
| return new URLBuilder("/dynamic-search-rules"); | ||
| } | ||
| } |
41 changes: 41 additions & 0 deletions
41
src/main/java/com/meilisearch/sdk/model/DynamicSearchRule.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| package com.meilisearch.sdk.model; | ||
|
|
||
| import java.io.Serializable; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import lombok.Getter; | ||
| import lombok.Setter; | ||
|
|
||
| /** | ||
| * Meilisearch Dynamic Search Rule. | ||
| * | ||
| * @see <a href="https://www.meilisearch.com/docs/capabilities/search_rules/overview">API | ||
| * specification</a> | ||
| */ | ||
| @Getter | ||
| @Setter | ||
| public class DynamicSearchRule implements Serializable { | ||
| protected String uid; | ||
| protected String description; | ||
| protected int priority; | ||
| protected boolean active; | ||
| protected List<Map<String, Object>> conditions; | ||
| protected List<Map<String, Object>> actions; | ||
|
|
||
| public DynamicSearchRule() {} | ||
|
|
||
| public DynamicSearchRule( | ||
| String uid, | ||
| String description, | ||
| int priority, | ||
| boolean active, | ||
| List<Map<String, Object>> conditions, | ||
| List<Map<String, Object>> actions) { | ||
| this.uid = uid; | ||
| this.description = description; | ||
| this.priority = priority; | ||
| this.active = active; | ||
| this.conditions = conditions; | ||
| this.actions = actions; | ||
| } | ||
| } | ||
27 changes: 27 additions & 0 deletions
27
src/main/java/com/meilisearch/sdk/model/DynamicSearchRulesQuery.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| package com.meilisearch.sdk.model; | ||
|
|
||
| import java.io.Serializable; | ||
| import java.util.Map; | ||
|
|
||
| public class DynamicSearchRulesQuery implements Serializable { | ||
| protected Integer offset; | ||
| protected Integer limit; | ||
| protected Map<String, Object> filter; | ||
|
|
||
| public DynamicSearchRulesQuery() {} | ||
|
|
||
| public DynamicSearchRulesQuery setOffset(int offset) { | ||
| this.offset = offset; | ||
| return this; | ||
| } | ||
|
|
||
| public DynamicSearchRulesQuery setLimit(int limit) { | ||
| this.limit = limit; | ||
| return this; | ||
| } | ||
|
|
||
| public DynamicSearchRulesQuery setFilter(Map<String, Object> filter) { | ||
| this.filter = filter; | ||
| return this; | ||
| } | ||
| } |
99 changes: 99 additions & 0 deletions
99
src/test/java/com/meilisearch/integration/DynamicSearchRulesTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| package com.meilisearch.integration; | ||
|
|
||
| import static org.hamcrest.CoreMatchers.equalTo; | ||
| import static org.hamcrest.CoreMatchers.notNullValue; | ||
| import static org.hamcrest.MatcherAssert.assertThat; | ||
|
|
||
| import com.meilisearch.integration.classes.AbstractIT; | ||
| import com.meilisearch.sdk.exceptions.MeilisearchException; | ||
| import com.meilisearch.sdk.model.DynamicSearchRule; | ||
| import com.meilisearch.sdk.model.DynamicSearchRulesQuery; | ||
| import com.meilisearch.sdk.model.Results; | ||
| import com.meilisearch.sdk.model.TaskInfo; | ||
| import java.util.Collections; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import org.junit.jupiter.api.AfterAll; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Tag; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| @Tag("integration") | ||
| public class DynamicSearchRulesTest extends AbstractIT { | ||
|
|
||
| @BeforeEach | ||
| public void initialize() throws MeilisearchException { | ||
| this.setUp(); | ||
| client.experimentalFeatures(Collections.singletonMap("dynamicSearchRules", true)); | ||
| } | ||
|
|
||
| @AfterAll | ||
| static void cleanMeilisearch() { | ||
| cleanup(); | ||
| } | ||
|
|
||
| private DynamicSearchRule buildTestRule(String uid) { | ||
| Map<String, Object> queryCondition = Map.of("scope", "query", "isEmpty", true); | ||
| Map<String, Object> action = | ||
| Map.of( | ||
| "selector", Map.of("indexUid", "products"), | ||
| "action", Map.of("type", "pin", "position", 1)); | ||
|
|
||
| return new DynamicSearchRule( | ||
| uid, "Test rule", 5, true, List.of(queryCondition), List.of(action)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testUpdateDynamicSearchRule() throws MeilisearchException { | ||
| DynamicSearchRule rule = buildTestRule("test-rule"); | ||
|
|
||
| TaskInfo task = client.updateDynamicSearchRule("test-rule", rule); | ||
| client.waitForTask(task.getTaskUid()); | ||
|
|
||
| DynamicSearchRule fetched = client.getDynamicSearchRule("test-rule"); | ||
| assertThat(fetched.getUid(), equalTo("test-rule")); | ||
| assertThat(fetched.getDescription(), equalTo("Test rule")); | ||
| assertThat(fetched.getPriority(), equalTo(5)); | ||
| assertThat(fetched.isActive(), equalTo(true)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testGetDynamicSearchRule() throws MeilisearchException { | ||
| DynamicSearchRule rule = buildTestRule("test-rule-get"); | ||
| TaskInfo task = client.updateDynamicSearchRule("test-rule-get", rule); | ||
| client.waitForTask(task.getTaskUid()); | ||
|
|
||
| DynamicSearchRule fetched = client.getDynamicSearchRule("test-rule-get"); | ||
| assertThat(fetched, notNullValue()); | ||
| assertThat(fetched.getUid(), equalTo("test-rule-get")); | ||
| } | ||
|
|
||
| @Test | ||
| public void testListDynamicSearchRules() throws MeilisearchException { | ||
| TaskInfo task1 = client.updateDynamicSearchRule("rule-one", buildTestRule("rule-one")); | ||
| client.waitForTask(task1.getTaskUid()); | ||
| TaskInfo task2 = client.updateDynamicSearchRule("rule-two", buildTestRule("rule-two")); | ||
| client.waitForTask(task2.getTaskUid()); | ||
|
|
||
| Results<DynamicSearchRule> results = | ||
| client.listDynamicSearchRules(new DynamicSearchRulesQuery()); | ||
|
|
||
| assertThat(results.getResults().length >= 2, equalTo(true)); | ||
| } | ||
|
|
||
| @Test | ||
| public void testDeleteDynamicSearchRule() throws MeilisearchException { | ||
| DynamicSearchRule rule = buildTestRule("test-rule-delete"); | ||
| TaskInfo createTask = client.updateDynamicSearchRule("test-rule-delete", rule); | ||
| client.waitForTask(createTask.getTaskUid()); | ||
|
|
||
| TaskInfo deleteTask = client.deleteDynamicSearchRule("test-rule-delete"); | ||
| client.waitForTask(deleteTask.getTaskUid()); | ||
|
|
||
| MeilisearchException exception = | ||
| org.junit.jupiter.api.Assertions.assertThrows( | ||
| MeilisearchException.class, | ||
| () -> client.getDynamicSearchRule("test-rule-delete")); | ||
| assertThat(exception, notNullValue()); | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.