Skip to content

Commit 740d3f6

Browse files
authored
Merge pull request #987 from zshs000/feat/986-dynamic-search-rule-filter
feat: add filter condition to dynamic search rules
2 parents 69def58 + 2c0691c commit 740d3f6

4 files changed

Lines changed: 113 additions & 12 deletions

File tree

‎.code-samples.meilisearch.yaml‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -672,9 +672,9 @@ list_dynamic_search_rules_1: |-
672672
get_dynamic_search_rule_1: |-
673673
DynamicSearchRule rule = client.getDynamicSearchRule("black-friday");
674674
patch_dynamic_search_rule_1: |-
675-
List<Map<String, Object>> conditions = List.of(
676-
Map.of("scope", "query", "isEmpty", true),
677-
Map.of("scope", "time", "start", "2025-11-28T00:00:00Z", "end", "2025-11-28T23:59:59Z"));
675+
Map<String, Object> conditions = Map.of(
676+
"query", Map.of("isEmpty", true),
677+
"time", Map.of("start", "2025-11-28T00:00:00Z", "end", "2025-11-28T23:59:59Z"));
678678
679679
List<Map<String, Object>> actions = List.of(
680680
Map.of(

‎src/main/java/com/meilisearch/sdk/model/DynamicSearchRule.java‎

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,24 @@
1717
public class DynamicSearchRule implements Serializable {
1818
protected String uid;
1919
protected String description;
20-
protected int priority;
20+
protected int precedence;
2121
protected boolean active;
22-
protected List<Map<String, Object>> conditions;
22+
protected Map<String, Object> conditions;
2323
protected List<Map<String, Object>> actions;
24+
protected String lastUpdatedAt;
2425

2526
public DynamicSearchRule() {}
2627

2728
public DynamicSearchRule(
2829
String uid,
2930
String description,
30-
int priority,
31+
int precedence,
3132
boolean active,
32-
List<Map<String, Object>> conditions,
33+
Map<String, Object> conditions,
3334
List<Map<String, Object>> actions) {
3435
this.uid = uid;
3536
this.description = description;
36-
this.priority = priority;
37+
this.precedence = precedence;
3738
this.active = active;
3839
this.conditions = conditions;
3940
this.actions = actions;

‎src/test/java/com/meilisearch/integration/DynamicSearchRulesTest.java‎

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@
66

77
import com.meilisearch.integration.classes.AbstractIT;
88
import com.meilisearch.sdk.exceptions.MeilisearchException;
9+
import com.meilisearch.sdk.json.GsonJsonHandler;
910
import com.meilisearch.sdk.model.DynamicSearchRule;
1011
import com.meilisearch.sdk.model.DynamicSearchRulesQuery;
1112
import com.meilisearch.sdk.model.Results;
1213
import com.meilisearch.sdk.model.TaskInfo;
1314
import java.util.Collections;
1415
import java.util.List;
1516
import java.util.Map;
17+
import org.json.JSONObject;
1618
import org.junit.jupiter.api.AfterAll;
1719
import org.junit.jupiter.api.BeforeEach;
1820
import org.junit.jupiter.api.Tag;
@@ -33,14 +35,17 @@ static void cleanMeilisearch() {
3335
}
3436

3537
private DynamicSearchRule buildTestRule(String uid) {
36-
Map<String, Object> queryCondition = Map.of("scope", "query", "isEmpty", true);
38+
Map<String, Object> queryCondition = Map.of("query", Map.of("isEmpty", true));
3739
Map<String, Object> action =
3840
Map.of(
3941
"selector", Map.of("indexUid", "products"),
4042
"action", Map.of("type", "pin", "position", 1));
4143

42-
return new DynamicSearchRule(
43-
uid, "Test rule", 5, true, List.of(queryCondition), List.of(action));
44+
return new DynamicSearchRule(uid, "Test rule", 5, true, queryCondition, List.of(action));
45+
}
46+
47+
private Map<String, Object> buildFilterCondition(String color) {
48+
return Map.of("filter", Map.of("values", Map.of("color", color, "category", "shirt")));
4449
}
4550

4651
@Test
@@ -53,8 +58,44 @@ public void testUpdateDynamicSearchRule() throws MeilisearchException {
5358
DynamicSearchRule fetched = client.getDynamicSearchRule("test-rule");
5459
assertThat(fetched.getUid(), equalTo("test-rule"));
5560
assertThat(fetched.getDescription(), equalTo("Test rule"));
56-
assertThat(fetched.getPriority(), equalTo(5));
61+
assertThat(fetched.getPrecedence(), equalTo(5));
5762
assertThat(fetched.isActive(), equalTo(true));
63+
assertThat(fetched.getLastUpdatedAt(), notNullValue());
64+
}
65+
66+
@Test
67+
public void testCreateAndUpdateDynamicSearchRuleWithFilterCondition()
68+
throws MeilisearchException {
69+
DynamicSearchRule rule = buildTestRule("test-filter");
70+
rule.setConditions(buildFilterCondition("red"));
71+
72+
TaskInfo createTask = client.updateDynamicSearchRule("test-filter", rule);
73+
client.waitForTask(createTask.getTaskUid());
74+
75+
DynamicSearchRule created = client.getDynamicSearchRule("test-filter");
76+
JSONObject createdConditions =
77+
new JSONObject(new GsonJsonHandler().encode(created.getConditions()));
78+
assertThat(
79+
createdConditions
80+
.getJSONObject("filter")
81+
.getJSONObject("values")
82+
.getString("color"),
83+
equalTo("red"));
84+
85+
rule.setConditions(buildFilterCondition("blue"));
86+
TaskInfo updateTask = client.updateDynamicSearchRule("test-filter", rule);
87+
client.waitForTask(updateTask.getTaskUid());
88+
89+
DynamicSearchRule updated = client.getDynamicSearchRule("test-filter");
90+
JSONObject updatedConditions =
91+
new JSONObject(new GsonJsonHandler().encode(updated.getConditions()));
92+
assertThat(
93+
updatedConditions
94+
.getJSONObject("filter")
95+
.getJSONObject("values")
96+
.getString("color"),
97+
equalTo("blue"));
98+
assertThat(updated.getLastUpdatedAt(), notNullValue());
5899
}
59100

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

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

84126
@Test
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package com.meilisearch.sdk.model;
2+
3+
import static org.hamcrest.MatcherAssert.assertThat;
4+
import static org.hamcrest.Matchers.equalTo;
5+
import static org.hamcrest.Matchers.is;
6+
7+
import com.meilisearch.sdk.json.GsonJsonHandler;
8+
import java.util.List;
9+
import java.util.Map;
10+
import org.json.JSONObject;
11+
import org.junit.jupiter.api.Test;
12+
13+
class DynamicSearchRuleTest {
14+
private final GsonJsonHandler jsonHandler = new GsonJsonHandler();
15+
16+
@Test
17+
void serializesFilterConditionValues() {
18+
Map<String, Object> conditions =
19+
Map.of(
20+
"filter",
21+
Map.of(
22+
"values",
23+
Map.of(
24+
"color", "red",
25+
"category", "shirt")));
26+
Map<String, Object> action =
27+
Map.of(
28+
"selector", Map.of("indexUid", "products", "id", "123"),
29+
"action", Map.of("type", "pin", "position", 1));
30+
31+
DynamicSearchRule rule =
32+
new DynamicSearchRule(
33+
"test-filter", "Filter rule", 1, true, conditions, List.of(action));
34+
35+
JSONObject json = new JSONObject(jsonHandler.encode(rule));
36+
JSONObject values =
37+
json.getJSONObject("conditions").getJSONObject("filter").getJSONObject("values");
38+
39+
assertThat(values.getString("color"), is(equalTo("red")));
40+
assertThat(values.getString("category"), is(equalTo("shirt")));
41+
}
42+
43+
@Test
44+
void deserializesLastUpdatedAt() {
45+
String lastUpdatedAt = "2026-07-28T08:30:00Z";
46+
String response =
47+
"{\"uid\":\"test-filter\",\"description\":\"Filter rule\","
48+
+ "\"precedence\":1,\"active\":true,\"conditions\":{},\"actions\":[],"
49+
+ "\"lastUpdatedAt\":\""
50+
+ lastUpdatedAt
51+
+ "\"}";
52+
53+
DynamicSearchRule rule = jsonHandler.decode(response, DynamicSearchRule.class);
54+
55+
assertThat(rule.getPrecedence(), is(equalTo(1)));
56+
assertThat(rule.getLastUpdatedAt(), is(equalTo(lastUpdatedAt)));
57+
}
58+
}

0 commit comments

Comments
 (0)