Skip to content

Commit 87c3563

Browse files
committed
feat(presets): add search presets support
Implements search presets management to allow storing and reusing common search configurations. Adds complete CRUD operations for search presets with proper error handling and test coverage. Changes include: - New Presets and Preset classes for managing search presets - Full test coverage with realistic search preset scenarios - Helper class updates for testing preset functionality - Documentation updates with usage examples for common scenarios feat(presets): add Presets class for bulk operations feat(presets): add Preset class for individual preset management test(presets): add PresetsTest test coverage refactor(helper): add presets support in test helper docs(presets): add documentation and examples
1 parent 1eee81d commit 87c3563

File tree

6 files changed

+213
-0
lines changed

6 files changed

+213
-0
lines changed

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,34 @@ StopwordsSetsRetrieveAllSchema sets = client.stopwords().retrieve();
234234
client.stopwords("common-words").delete();
235235
```
236236

237+
### Create or update a search preset
238+
```java
239+
SearchParameters params = new SearchParameters()
240+
.q("bestseller")
241+
.queryBy("title,author")
242+
.sortBy("ratings_count:desc");
243+
244+
PresetUpsertSchema preset = new PresetUpsertSchema()
245+
.value(params);
246+
247+
client.presets().upsert("bestsellers_view", preset);
248+
```
249+
250+
### Retrieve a search preset
251+
```java
252+
PresetSchema preset = client.presets("bestsellers_view").retrieve();
253+
```
254+
255+
### Retrieve all search presets
256+
```java
257+
PresetsRetrieveSchema presets = client.presets().retrieve();
258+
```
259+
260+
### Delete a search preset
261+
```java
262+
client.presets("bestsellers_view").delete();
263+
```
264+
237265
### Create an API key
238266
```java
239267
ApiKeySchema apiKeySchema = new ApiKeySchema();

src/main/java/org/typesense/api/Client.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ public class Client {
2424
private Stopwords stopwords;
2525
private Map<String, StopwordsSet> individualStopwordsSets;
2626

27+
private Presets presets;
28+
private Map<String, Preset> individualPresets;
29+
2730
public Health health;
2831
public Operations operations;
2932
public Metrics metrics;
@@ -47,6 +50,8 @@ public Client(Configuration configuration){
4750
this.analytics = new Analytics(this.apiCall);
4851
this.stopwords = new Stopwords(this.apiCall);
4952
this.individualStopwordsSets = new HashMap<>();
53+
this.presets = new Presets(this.apiCall);
54+
this.individualPresets = new HashMap<>();
5055
}
5156

5257
public Collection collections(String name){
@@ -114,4 +119,19 @@ public StopwordsSet stopwords(String stopwordsSetId) {
114119
retVal = this.individualStopwordsSets.get(stopwordsSetId);
115120
return retVal;
116121
}
122+
123+
public Presets presets() {
124+
return this.presets;
125+
}
126+
127+
public Preset presets(String presetId) {
128+
Preset retVal;
129+
130+
if (!this.individualPresets.containsKey(presetId)) {
131+
this.individualPresets.put(presetId, new Preset(presetId, this.apiCall));
132+
}
133+
134+
retVal = this.individualPresets.get(presetId);
135+
return retVal;
136+
}
117137
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package org.typesense.api;
2+
3+
import org.typesense.api.utils.URLEncoding;
4+
import org.typesense.model.PresetDeleteSchema;
5+
import org.typesense.model.PresetSchema;
6+
7+
public class Preset {
8+
private final ApiCall apiCall;
9+
private final String name;
10+
11+
public Preset(String name, ApiCall apiCall) {
12+
this.name = name;
13+
this.apiCall = apiCall;
14+
}
15+
16+
public PresetSchema retrieve() throws Exception {
17+
return this.apiCall.get(this.getEndpoint(), null, PresetSchema.class);
18+
}
19+
20+
public PresetDeleteSchema delete() throws Exception {
21+
return this.apiCall.delete(this.getEndpoint(), null, PresetDeleteSchema.class);
22+
}
23+
24+
private String getEndpoint() {
25+
return Presets.RESOURCEPATH + "/" + URLEncoding.encodeURIComponent(this.name);
26+
}
27+
28+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package org.typesense.api;
2+
3+
import org.typesense.api.utils.URLEncoding;
4+
import org.typesense.model.PresetSchema;
5+
import org.typesense.model.PresetUpsertSchema;
6+
import org.typesense.model.PresetsRetrieveSchema;
7+
8+
public class Presets {
9+
public final static String RESOURCEPATH = "/presets";
10+
11+
private final ApiCall apiCall;
12+
13+
public Presets(ApiCall apiCall) {
14+
this.apiCall = apiCall;
15+
}
16+
17+
public PresetSchema upsert(String name, PresetUpsertSchema preset) throws Exception {
18+
return this.apiCall.put(getEndpoint(name), preset, null, PresetSchema.class);
19+
}
20+
21+
public PresetsRetrieveSchema retrieve() throws Exception {
22+
return this.apiCall.get(Presets.RESOURCEPATH, null, PresetsRetrieveSchema.class);
23+
}
24+
25+
private String getEndpoint(String name) {
26+
return RESOURCEPATH + "/" + URLEncoding.encodeURIComponent(name);
27+
}
28+
29+
}

src/test/java/org/typesense/api/Helper.java

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,13 @@
2121
import org.typesense.model.CollectionResponse;
2222
import org.typesense.model.CollectionSchema;
2323
import org.typesense.model.Field;
24+
import org.typesense.model.PresetSchema;
25+
import org.typesense.model.PresetUpsertSchema;
26+
import org.typesense.model.PresetsRetrieveSchema;
2427
import org.typesense.model.SearchOverrideInclude;
2528
import org.typesense.model.SearchOverrideRule;
2629
import org.typesense.model.SearchOverrideSchema;
30+
import org.typesense.model.SearchParameters;
2731
import org.typesense.model.SearchSynonymSchema;
2832
import org.typesense.model.StopwordsSetSchema;
2933
import org.typesense.model.StopwordsSetUpsertSchema;
@@ -140,6 +144,17 @@ public void createTestStopwordsSet() throws Exception {
140144
client.stopwords().upsert("common-words", stopwordsSetSchema);
141145
}
142146

147+
public void createTestPreset() throws Exception {
148+
SearchParameters params = new SearchParameters()
149+
.q("Romeo")
150+
.queryBy("title");
151+
152+
PresetUpsertSchema preset = new PresetUpsertSchema()
153+
.value(params);
154+
155+
client.presets().upsert("listing_view", preset);
156+
}
157+
143158
public void teardown() throws Exception {
144159
CollectionResponse[] collectionResponses = client.collections().retrieve();
145160
for (CollectionResponse c : collectionResponses) {
@@ -165,5 +180,10 @@ public void teardown() throws Exception {
165180
for (StopwordsSetSchema s : stopwords.getStopwords()) {
166181
client.stopwords(s.getId()).delete();
167182
}
183+
184+
PresetsRetrieveSchema presets = client.presets().retrieve();
185+
for (PresetSchema p : presets.getPresets()) {
186+
client.presets(p.getName()).delete();
187+
}
168188
}
169189
}
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package org.typesense.api;
2+
3+
import org.junit.jupiter.api.AfterEach;
4+
import static org.junit.jupiter.api.Assertions.assertEquals;
5+
import static org.junit.jupiter.api.Assertions.assertNotNull;
6+
import org.junit.jupiter.api.BeforeEach;
7+
import org.junit.jupiter.api.Test;
8+
import org.typesense.model.OneOfPresetUpsertSchemaValue;
9+
import org.typesense.model.PresetDeleteSchema;
10+
import org.typesense.model.PresetSchema;
11+
import org.typesense.model.PresetUpsertSchema;
12+
import org.typesense.model.PresetsRetrieveSchema;
13+
import org.typesense.model.SearchParameters;
14+
15+
public class PresetsTest {
16+
17+
private Client client;
18+
private Helper helper;
19+
20+
@BeforeEach
21+
void setUp() throws Exception {
22+
helper = new Helper();
23+
client = helper.getClient();
24+
helper.teardown();
25+
helper.createTestCollection();
26+
}
27+
28+
@AfterEach
29+
void tearDown() throws Exception {
30+
helper.teardown();
31+
}
32+
33+
@Test
34+
void testUpsert() throws Exception {
35+
SearchParameters params = new SearchParameters()
36+
.q("Romeo")
37+
.queryBy("title");
38+
39+
PresetUpsertSchema preset = new PresetUpsertSchema()
40+
.value(params);
41+
42+
PresetSchema result = client.presets().upsert("listing_view", preset);
43+
44+
assertNotNull(result);
45+
assertEquals("listing_view", result.getName());
46+
47+
OneOfPresetUpsertSchemaValue value = result.getValue();
48+
assertNotNull(value);
49+
}
50+
51+
@Test
52+
void testRetrieve() throws Exception {
53+
helper.createTestPreset();
54+
55+
PresetSchema result = this.client.presets("listing_view").retrieve();
56+
57+
assertNotNull(result);
58+
assertEquals("listing_view", result.getName());
59+
assertNotNull(result.getValue());
60+
}
61+
62+
@Test
63+
void testRetrieveAll() throws Exception {
64+
helper.createTestPreset();
65+
66+
PresetsRetrieveSchema result = this.client.presets().retrieve();
67+
68+
assertNotNull(result);
69+
70+
assertEquals(1, result.getPresets().size());
71+
72+
PresetSchema preset = result.getPresets().get(0);
73+
assertEquals("listing_view", preset.getName());
74+
assertNotNull(preset.getValue());
75+
}
76+
77+
@Test
78+
void testDelete() throws Exception {
79+
helper.createTestPreset();
80+
81+
PresetDeleteSchema result = this.client.presets("listing_view").delete();
82+
83+
assertNotNull(result);
84+
85+
assertEquals("listing_view", result.getName());
86+
87+
}
88+
}

0 commit comments

Comments
 (0)