Skip to content

Commit fa19b6d

Browse files
committed
feat(stopwords): add stopwords support
Implements stopwords management capabilities allowing users to create and manage stopwords sets for search optimization. Adds complete CRUD operations for stopwords with proper error handling and test coverage. Changes include: - New Stopwords and StopwordsSet classes for managing stopwords - Client integration for stopwords management - Full test coverage with realistic scenarios - Helper class updates for testing stopwords functionality - Documentation updates with usage examples feat(stopwords): add Stopwords class for bulk operations feat(stopwords): add StopwordsSet class for individual set management test(stopwords): add StopwordsTest test coverage refactor(client): integrate stopwords support refactor(helper): add stopwords support in test helper docs(stopwords): add documentation and examples
1 parent 4af0a7a commit fa19b6d

File tree

6 files changed

+216
-0
lines changed

6 files changed

+216
-0
lines changed

README.md

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,34 @@ AnalyticsEventCreateSchema analyticsEvent = new AnalyticsEventCreateSchema()
206206
client.analytics().events().create(analyticsEvent);
207207
```
208208

209+
### Upsert a stopwords set
210+
```java
211+
List<String> stopwords = new ArrayList<>();
212+
stopwords.add("the");
213+
stopwords.add("of");
214+
stopwords.add("and");
215+
216+
StopwordsSetUpsertSchema stopwordsSet = new StopwordsSetUpsertSchema();
217+
stopwordsSet.stopwords(stopwords);
218+
219+
client.stopwords().upsert("common-words", stopwordsSet);
220+
```
221+
222+
### Retrieve a stopwords set
223+
```java
224+
StopwordsSetRetrieveSchema set = client.stopwords("common-words").retrieve();
225+
```
226+
227+
### Retrieve all stopwords sets
228+
```java
229+
StopwordsSetsRetrieveAllSchema sets = client.stopwords().retrieve();
230+
```
231+
232+
### Delete a stopwords set
233+
```java
234+
client.stopwords("common-words").delete();
235+
```
236+
209237
### Create an API key
210238
```java
211239
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
@@ -21,6 +21,9 @@ public class Client {
2121

2222
private Analytics analytics;
2323

24+
private Stopwords stopwords;
25+
private Map<String, StopwordsSet> individualStopwordsSets;
26+
2427
public Health health;
2528
public Operations operations;
2629
public Metrics metrics;
@@ -42,6 +45,8 @@ public Client(Configuration configuration){
4245
this.debug = new Debug(this.apiCall);
4346
this.multiSearch = new MultiSearch(this.apiCall);
4447
this.analytics = new Analytics(this.apiCall);
48+
this.stopwords = new Stopwords(this.apiCall);
49+
this.individualStopwordsSets = new HashMap<>();
4550
}
4651

4752
public Collection collections(String name){
@@ -94,4 +99,19 @@ public Key keys(Long id){
9499
public Analytics analytics(){
95100
return this.analytics;
96101
}
102+
103+
public Stopwords stopwords() {
104+
return this.stopwords;
105+
}
106+
107+
public StopwordsSet stopwords(String stopwordsSetId) {
108+
StopwordsSet retVal;
109+
110+
if (!this.individualStopwordsSets.containsKey(stopwordsSetId)) {
111+
this.individualStopwordsSets.put(stopwordsSetId, new StopwordsSet(stopwordsSetId, this.apiCall));
112+
}
113+
114+
retVal = this.individualStopwordsSets.get(stopwordsSetId);
115+
return retVal;
116+
}
97117
}
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.StopwordsSetSchema;
5+
import org.typesense.model.StopwordsSetUpsertSchema;
6+
import org.typesense.model.StopwordsSetsRetrieveAllSchema;
7+
8+
public class Stopwords {
9+
public final static String RESOURCEPATH = "/stopwords";
10+
11+
private final ApiCall apiCall;
12+
13+
public Stopwords(ApiCall apiCall) {
14+
this.apiCall = apiCall;
15+
}
16+
17+
public StopwordsSetSchema upsert(String stopwordSetId, StopwordsSetUpsertSchema stopwordSet) throws Exception {
18+
return this.apiCall.put(getEndpoint(stopwordSetId), stopwordSet, null, StopwordsSetSchema.class);
19+
}
20+
21+
public StopwordsSetsRetrieveAllSchema retrieve() throws Exception {
22+
return this.apiCall.get(Stopwords.RESOURCEPATH, null, StopwordsSetsRetrieveAllSchema.class);
23+
}
24+
25+
private String getEndpoint(String stopwordSetId) {
26+
return RESOURCEPATH + "/" + URLEncoding.encodeURIComponent(stopwordSetId);
27+
}
28+
29+
}
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.StopwordsSetRetrieveSchema;
5+
import org.typesense.model.StopwordsSetSchema;
6+
7+
public class StopwordsSet {
8+
private final ApiCall apiCall;
9+
private final String stopwordsSetId;
10+
11+
public StopwordsSet(String stopwordsSetId, ApiCall apiCall) {
12+
this.stopwordsSetId = stopwordsSetId;
13+
this.apiCall = apiCall;
14+
}
15+
16+
public StopwordsSetRetrieveSchema retrieve() throws Exception {
17+
return this.apiCall.get(this.getEndpoint(), null, StopwordsSetRetrieveSchema.class);
18+
}
19+
20+
public StopwordsSetSchema delete() throws Exception {
21+
return this.apiCall.delete(this.getEndpoint(), null, StopwordsSetSchema.class);
22+
}
23+
24+
private String getEndpoint() {
25+
return Stopwords.RESOURCEPATH + "/" + URLEncoding.encodeURIComponent(this.stopwordsSetId);
26+
}
27+
28+
}

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

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@
2525
import org.typesense.model.SearchOverrideRule;
2626
import org.typesense.model.SearchOverrideSchema;
2727
import org.typesense.model.SearchSynonymSchema;
28+
import org.typesense.model.StopwordsSetSchema;
29+
import org.typesense.model.StopwordsSetUpsertSchema;
30+
import org.typesense.model.StopwordsSetsRetrieveAllSchema;
2831
import org.typesense.resources.Node;
2932

3033
public class Helper {
@@ -125,6 +128,18 @@ public void createTestAnalyticsRule() throws Exception {
125128
client.analytics().rules().upsert("analytics-rule", analyticsRuleSchema);
126129
}
127130

131+
public void createTestStopwordsSet() throws Exception {
132+
List<String> stopwords = new ArrayList<>();
133+
stopwords.add("the");
134+
stopwords.add("of");
135+
stopwords.add("and");
136+
137+
StopwordsSetUpsertSchema stopwordsSetSchema = new StopwordsSetUpsertSchema();
138+
stopwordsSetSchema.stopwords(stopwords);
139+
140+
client.stopwords().upsert("common-words", stopwordsSetSchema);
141+
}
142+
128143
public void teardown() throws Exception {
129144
CollectionResponse[] collectionResponses = client.collections().retrieve();
130145
for (CollectionResponse c : collectionResponses) {
@@ -145,5 +160,10 @@ public void teardown() throws Exception {
145160
for (ApiKey k : apiKeysResponse.getKeys()) {
146161
client.keys(k.getId()).delete();
147162
}
163+
164+
StopwordsSetsRetrieveAllSchema stopwords = client.stopwords().retrieve();
165+
for (StopwordsSetSchema s : stopwords.getStopwords()) {
166+
client.stopwords(s.getId()).delete();
167+
}
148168
}
149169
}
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package org.typesense.api;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
import org.junit.jupiter.api.AfterEach;
7+
import static org.junit.jupiter.api.Assertions.assertEquals;
8+
import static org.junit.jupiter.api.Assertions.assertNotNull;
9+
import org.junit.jupiter.api.BeforeEach;
10+
import org.junit.jupiter.api.Test;
11+
import org.typesense.model.StopwordsSetRetrieveSchema;
12+
import org.typesense.model.StopwordsSetSchema;
13+
import org.typesense.model.StopwordsSetUpsertSchema;
14+
import org.typesense.model.StopwordsSetsRetrieveAllSchema;
15+
16+
public class StopwordsTest {
17+
18+
private Client client;
19+
private Helper helper;
20+
21+
@BeforeEach
22+
void setUp() throws Exception {
23+
helper = new Helper();
24+
client = helper.getClient();
25+
helper.teardown();
26+
helper.createTestCollection();
27+
}
28+
29+
@AfterEach
30+
void tearDown() throws Exception {
31+
helper.teardown();
32+
}
33+
34+
@Test
35+
void testUpsert() throws Exception {
36+
List<String> stopwords = new ArrayList<>();
37+
stopwords.add("the");
38+
stopwords.add("of");
39+
stopwords.add("and");
40+
41+
StopwordsSetUpsertSchema stopwordsSetSchema = new StopwordsSetUpsertSchema();
42+
stopwordsSetSchema.stopwords(stopwords);
43+
44+
client.stopwords().upsert("common-words", stopwordsSetSchema);
45+
}
46+
47+
@Test
48+
void testRetrieve() throws Exception {
49+
helper.createTestStopwordsSet();
50+
51+
StopwordsSetRetrieveSchema result = this.client.stopwords("common-words").retrieve();
52+
53+
assertNotNull(result);
54+
55+
StopwordsSetSchema set = result.getStopwords();
56+
57+
assertEquals("common-words", set.getId());
58+
assertEquals(3, set.getStopwords().size());
59+
assertEquals("and", set.getStopwords().get(0));
60+
assertEquals("the", set.getStopwords().get(1));
61+
assertEquals("of", set.getStopwords().get(2));
62+
}
63+
64+
@Test
65+
void testRetrieveAll() throws Exception {
66+
helper.createTestStopwordsSet();
67+
68+
StopwordsSetsRetrieveAllSchema result = this.client.stopwords().retrieve();
69+
70+
assertNotNull(result);
71+
assertEquals(1, result.getStopwords().size());
72+
73+
StopwordsSetSchema set = result.getStopwords().get(0);
74+
75+
assertEquals("common-words", set.getId());
76+
assertEquals(3, set.getStopwords().size());
77+
assertEquals("and", set.getStopwords().get(0));
78+
assertEquals("the", set.getStopwords().get(1));
79+
assertEquals("of", set.getStopwords().get(2));
80+
}
81+
82+
@Test
83+
void testDelete() throws Exception {
84+
helper.createTestStopwordsSet();
85+
86+
StopwordsSetSchema result = this.client.stopwords("common-words").delete();
87+
88+
assertNotNull(result);
89+
assertEquals("common-words", result.getId());
90+
}
91+
}

0 commit comments

Comments
 (0)