Skip to content
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: 3 additions & 3 deletions .code-samples.meilisearch.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -672,9 +672,9 @@ list_dynamic_search_rules_1: |-
get_dynamic_search_rule_1: |-
DynamicSearchRule rule = client.getDynamicSearchRule("black-friday");
patch_dynamic_search_rule_1: |-
List<Map<String, Object>> conditions = List.of(
Map.of("scope", "query", "isEmpty", true),
Map.of("scope", "time", "start", "2025-11-28T00:00:00Z", "end", "2025-11-28T23:59:59Z"));
Map<String, Object> conditions = Map.of(
"query", Map.of("isEmpty", true),
"time", Map.of("start", "2025-11-28T00:00:00Z", "end", "2025-11-28T23:59:59Z"));

List<Map<String, Object>> actions = List.of(
Map.of(
Expand Down
11 changes: 6 additions & 5 deletions src/main/java/com/meilisearch/sdk/model/DynamicSearchRule.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,24 @@
public class DynamicSearchRule implements Serializable {
protected String uid;
protected String description;
protected int priority;
protected int precedence;
protected boolean active;
protected List<Map<String, Object>> conditions;
protected Map<String, Object> conditions;
protected List<Map<String, Object>> actions;
protected String lastUpdatedAt;

public DynamicSearchRule() {}

public DynamicSearchRule(
String uid,
String description,
int priority,
int precedence,
boolean active,
List<Map<String, Object>> conditions,
Map<String, Object> conditions,
List<Map<String, Object>> actions) {
this.uid = uid;
this.description = description;
this.priority = priority;
this.precedence = precedence;
this.active = active;
this.conditions = conditions;
this.actions = actions;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@

import com.meilisearch.integration.classes.AbstractIT;
import com.meilisearch.sdk.exceptions.MeilisearchException;
import com.meilisearch.sdk.json.GsonJsonHandler;
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.json.JSONObject;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Tag;
Expand All @@ -33,14 +35,17 @@ static void cleanMeilisearch() {
}

private DynamicSearchRule buildTestRule(String uid) {
Map<String, Object> queryCondition = Map.of("scope", "query", "isEmpty", true);
Map<String, Object> queryCondition = Map.of("query", Map.of("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));
return new DynamicSearchRule(uid, "Test rule", 5, true, queryCondition, List.of(action));
}

private Map<String, Object> buildFilterCondition(String color) {
return Map.of("filter", Map.of("values", Map.of("color", color, "category", "shirt")));
}

@Test
Expand All @@ -53,8 +58,44 @@ public void testUpdateDynamicSearchRule() throws MeilisearchException {
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.getPrecedence(), equalTo(5));
assertThat(fetched.isActive(), equalTo(true));
assertThat(fetched.getLastUpdatedAt(), notNullValue());
}

@Test
public void testCreateAndUpdateDynamicSearchRuleWithFilterCondition()
throws MeilisearchException {
DynamicSearchRule rule = buildTestRule("test-filter");
rule.setConditions(buildFilterCondition("red"));

TaskInfo createTask = client.updateDynamicSearchRule("test-filter", rule);
client.waitForTask(createTask.getTaskUid());

DynamicSearchRule created = client.getDynamicSearchRule("test-filter");
JSONObject createdConditions =
new JSONObject(new GsonJsonHandler().encode(created.getConditions()));
assertThat(
createdConditions
.getJSONObject("filter")
.getJSONObject("values")
.getString("color"),
equalTo("red"));

rule.setConditions(buildFilterCondition("blue"));
TaskInfo updateTask = client.updateDynamicSearchRule("test-filter", rule);
client.waitForTask(updateTask.getTaskUid());

DynamicSearchRule updated = client.getDynamicSearchRule("test-filter");
JSONObject updatedConditions =
new JSONObject(new GsonJsonHandler().encode(updated.getConditions()));
assertThat(
updatedConditions
.getJSONObject("filter")
.getJSONObject("values")
.getString("color"),
equalTo("blue"));
assertThat(updated.getLastUpdatedAt(), notNullValue());
}

@Test
Expand All @@ -79,6 +120,7 @@ public void testListDynamicSearchRules() throws MeilisearchException {
client.listDynamicSearchRules(new DynamicSearchRulesQuery());

assertThat(results.getResults().length >= 2, equalTo(true));
assertThat(results.getResults()[0].getLastUpdatedAt(), notNullValue());
}

@Test
Expand Down
58 changes: 58 additions & 0 deletions src/test/java/com/meilisearch/sdk/model/DynamicSearchRuleTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.meilisearch.sdk.model;

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

import com.meilisearch.sdk.json.GsonJsonHandler;
import java.util.List;
import java.util.Map;
import org.json.JSONObject;
import org.junit.jupiter.api.Test;

class DynamicSearchRuleTest {
private final GsonJsonHandler jsonHandler = new GsonJsonHandler();

@Test
void serializesFilterConditionValues() {
Map<String, Object> conditions =
Map.of(
"filter",
Map.of(
"values",
Map.of(
"color", "red",
"category", "shirt")));
Map<String, Object> action =
Map.of(
"selector", Map.of("indexUid", "products", "id", "123"),
"action", Map.of("type", "pin", "position", 1));

DynamicSearchRule rule =
new DynamicSearchRule(
"test-filter", "Filter rule", 1, true, conditions, List.of(action));

JSONObject json = new JSONObject(jsonHandler.encode(rule));
JSONObject values =
json.getJSONObject("conditions").getJSONObject("filter").getJSONObject("values");

assertThat(values.getString("color"), is(equalTo("red")));
assertThat(values.getString("category"), is(equalTo("shirt")));
}

@Test
void deserializesLastUpdatedAt() {
String lastUpdatedAt = "2026-07-28T08:30:00Z";
String response =
"{\"uid\":\"test-filter\",\"description\":\"Filter rule\","
+ "\"precedence\":1,\"active\":true,\"conditions\":{},\"actions\":[],"
+ "\"lastUpdatedAt\":\""
+ lastUpdatedAt
+ "\"}";

DynamicSearchRule rule = jsonHandler.decode(response, DynamicSearchRule.class);

assertThat(rule.getPrecedence(), is(equalTo(1)));
assertThat(rule.getLastUpdatedAt(), is(equalTo(lastUpdatedAt)));
}
}
Loading