From fa19b6d368eebd31e4879286ac5d5da4b3de18e3 Mon Sep 17 00:00:00 2001 From: Fanis Tharropoulos Date: Fri, 1 Nov 2024 16:00:07 +0200 Subject: [PATCH] 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 --- README.md | 28 ++++++ src/main/java/org/typesense/api/Client.java | 20 ++++ .../java/org/typesense/api/Stopwords.java | 29 ++++++ .../java/org/typesense/api/StopwordsSet.java | 28 ++++++ src/test/java/org/typesense/api/Helper.java | 20 ++++ .../java/org/typesense/api/StopwordsTest.java | 91 +++++++++++++++++++ 6 files changed, 216 insertions(+) create mode 100644 src/main/java/org/typesense/api/Stopwords.java create mode 100644 src/main/java/org/typesense/api/StopwordsSet.java create mode 100644 src/test/java/org/typesense/api/StopwordsTest.java diff --git a/README.md b/README.md index c917dd4..252ddf9 100644 --- a/README.md +++ b/README.md @@ -206,6 +206,34 @@ AnalyticsEventCreateSchema analyticsEvent = new AnalyticsEventCreateSchema() client.analytics().events().create(analyticsEvent); ``` +### Upsert a stopwords set +```java +List stopwords = new ArrayList<>(); +stopwords.add("the"); +stopwords.add("of"); +stopwords.add("and"); + +StopwordsSetUpsertSchema stopwordsSet = new StopwordsSetUpsertSchema(); +stopwordsSet.stopwords(stopwords); + +client.stopwords().upsert("common-words", stopwordsSet); +``` + +### Retrieve a stopwords set +```java +StopwordsSetRetrieveSchema set = client.stopwords("common-words").retrieve(); +``` + +### Retrieve all stopwords sets +```java +StopwordsSetsRetrieveAllSchema sets = client.stopwords().retrieve(); +``` + +### Delete a stopwords set +```java +client.stopwords("common-words").delete(); +``` + ### Create an API key ```java ApiKeySchema apiKeySchema = new ApiKeySchema(); diff --git a/src/main/java/org/typesense/api/Client.java b/src/main/java/org/typesense/api/Client.java index 6177674..84e94f0 100644 --- a/src/main/java/org/typesense/api/Client.java +++ b/src/main/java/org/typesense/api/Client.java @@ -21,6 +21,9 @@ public class Client { private Analytics analytics; + private Stopwords stopwords; + private Map individualStopwordsSets; + public Health health; public Operations operations; public Metrics metrics; @@ -42,6 +45,8 @@ public Client(Configuration configuration){ this.debug = new Debug(this.apiCall); this.multiSearch = new MultiSearch(this.apiCall); this.analytics = new Analytics(this.apiCall); + this.stopwords = new Stopwords(this.apiCall); + this.individualStopwordsSets = new HashMap<>(); } public Collection collections(String name){ @@ -94,4 +99,19 @@ public Key keys(Long id){ public Analytics analytics(){ return this.analytics; } + + public Stopwords stopwords() { + return this.stopwords; + } + + public StopwordsSet stopwords(String stopwordsSetId) { + StopwordsSet retVal; + + if (!this.individualStopwordsSets.containsKey(stopwordsSetId)) { + this.individualStopwordsSets.put(stopwordsSetId, new StopwordsSet(stopwordsSetId, this.apiCall)); + } + + retVal = this.individualStopwordsSets.get(stopwordsSetId); + return retVal; + } } diff --git a/src/main/java/org/typesense/api/Stopwords.java b/src/main/java/org/typesense/api/Stopwords.java new file mode 100644 index 0000000..921e43b --- /dev/null +++ b/src/main/java/org/typesense/api/Stopwords.java @@ -0,0 +1,29 @@ +package org.typesense.api; + +import org.typesense.api.utils.URLEncoding; +import org.typesense.model.StopwordsSetSchema; +import org.typesense.model.StopwordsSetUpsertSchema; +import org.typesense.model.StopwordsSetsRetrieveAllSchema; + +public class Stopwords { + public final static String RESOURCEPATH = "/stopwords"; + + private final ApiCall apiCall; + + public Stopwords(ApiCall apiCall) { + this.apiCall = apiCall; + } + + public StopwordsSetSchema upsert(String stopwordSetId, StopwordsSetUpsertSchema stopwordSet) throws Exception { + return this.apiCall.put(getEndpoint(stopwordSetId), stopwordSet, null, StopwordsSetSchema.class); + } + + public StopwordsSetsRetrieveAllSchema retrieve() throws Exception { + return this.apiCall.get(Stopwords.RESOURCEPATH, null, StopwordsSetsRetrieveAllSchema.class); + } + + private String getEndpoint(String stopwordSetId) { + return RESOURCEPATH + "/" + URLEncoding.encodeURIComponent(stopwordSetId); + } + +} diff --git a/src/main/java/org/typesense/api/StopwordsSet.java b/src/main/java/org/typesense/api/StopwordsSet.java new file mode 100644 index 0000000..9200960 --- /dev/null +++ b/src/main/java/org/typesense/api/StopwordsSet.java @@ -0,0 +1,28 @@ +package org.typesense.api; + +import org.typesense.api.utils.URLEncoding; +import org.typesense.model.StopwordsSetRetrieveSchema; +import org.typesense.model.StopwordsSetSchema; + +public class StopwordsSet { + private final ApiCall apiCall; + private final String stopwordsSetId; + + public StopwordsSet(String stopwordsSetId, ApiCall apiCall) { + this.stopwordsSetId = stopwordsSetId; + this.apiCall = apiCall; + } + + public StopwordsSetRetrieveSchema retrieve() throws Exception { + return this.apiCall.get(this.getEndpoint(), null, StopwordsSetRetrieveSchema.class); + } + + public StopwordsSetSchema delete() throws Exception { + return this.apiCall.delete(this.getEndpoint(), null, StopwordsSetSchema.class); + } + + private String getEndpoint() { + return Stopwords.RESOURCEPATH + "/" + URLEncoding.encodeURIComponent(this.stopwordsSetId); + } + +} diff --git a/src/test/java/org/typesense/api/Helper.java b/src/test/java/org/typesense/api/Helper.java index 33781ea..de910e3 100644 --- a/src/test/java/org/typesense/api/Helper.java +++ b/src/test/java/org/typesense/api/Helper.java @@ -25,6 +25,9 @@ import org.typesense.model.SearchOverrideRule; import org.typesense.model.SearchOverrideSchema; import org.typesense.model.SearchSynonymSchema; +import org.typesense.model.StopwordsSetSchema; +import org.typesense.model.StopwordsSetUpsertSchema; +import org.typesense.model.StopwordsSetsRetrieveAllSchema; import org.typesense.resources.Node; public class Helper { @@ -125,6 +128,18 @@ public void createTestAnalyticsRule() throws Exception { client.analytics().rules().upsert("analytics-rule", analyticsRuleSchema); } + public void createTestStopwordsSet() throws Exception { + List stopwords = new ArrayList<>(); + stopwords.add("the"); + stopwords.add("of"); + stopwords.add("and"); + + StopwordsSetUpsertSchema stopwordsSetSchema = new StopwordsSetUpsertSchema(); + stopwordsSetSchema.stopwords(stopwords); + + client.stopwords().upsert("common-words", stopwordsSetSchema); + } + public void teardown() throws Exception { CollectionResponse[] collectionResponses = client.collections().retrieve(); for (CollectionResponse c : collectionResponses) { @@ -145,5 +160,10 @@ public void teardown() throws Exception { for (ApiKey k : apiKeysResponse.getKeys()) { client.keys(k.getId()).delete(); } + + StopwordsSetsRetrieveAllSchema stopwords = client.stopwords().retrieve(); + for (StopwordsSetSchema s : stopwords.getStopwords()) { + client.stopwords(s.getId()).delete(); + } } } diff --git a/src/test/java/org/typesense/api/StopwordsTest.java b/src/test/java/org/typesense/api/StopwordsTest.java new file mode 100644 index 0000000..91aeb27 --- /dev/null +++ b/src/test/java/org/typesense/api/StopwordsTest.java @@ -0,0 +1,91 @@ +package org.typesense.api; + +import java.util.ArrayList; +import java.util.List; + +import org.junit.jupiter.api.AfterEach; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.typesense.model.StopwordsSetRetrieveSchema; +import org.typesense.model.StopwordsSetSchema; +import org.typesense.model.StopwordsSetUpsertSchema; +import org.typesense.model.StopwordsSetsRetrieveAllSchema; + +public class StopwordsTest { + + private Client client; + private Helper helper; + + @BeforeEach + void setUp() throws Exception { + helper = new Helper(); + client = helper.getClient(); + helper.teardown(); + helper.createTestCollection(); + } + + @AfterEach + void tearDown() throws Exception { + helper.teardown(); + } + + @Test + void testUpsert() throws Exception { + List stopwords = new ArrayList<>(); + stopwords.add("the"); + stopwords.add("of"); + stopwords.add("and"); + + StopwordsSetUpsertSchema stopwordsSetSchema = new StopwordsSetUpsertSchema(); + stopwordsSetSchema.stopwords(stopwords); + + client.stopwords().upsert("common-words", stopwordsSetSchema); + } + + @Test + void testRetrieve() throws Exception { + helper.createTestStopwordsSet(); + + StopwordsSetRetrieveSchema result = this.client.stopwords("common-words").retrieve(); + + assertNotNull(result); + + StopwordsSetSchema set = result.getStopwords(); + + assertEquals("common-words", set.getId()); + assertEquals(3, set.getStopwords().size()); + assertEquals("and", set.getStopwords().get(0)); + assertEquals("the", set.getStopwords().get(1)); + assertEquals("of", set.getStopwords().get(2)); + } + + @Test + void testRetrieveAll() throws Exception { + helper.createTestStopwordsSet(); + + StopwordsSetsRetrieveAllSchema result = this.client.stopwords().retrieve(); + + assertNotNull(result); + assertEquals(1, result.getStopwords().size()); + + StopwordsSetSchema set = result.getStopwords().get(0); + + assertEquals("common-words", set.getId()); + assertEquals(3, set.getStopwords().size()); + assertEquals("and", set.getStopwords().get(0)); + assertEquals("the", set.getStopwords().get(1)); + assertEquals("of", set.getStopwords().get(2)); + } + + @Test + void testDelete() throws Exception { + helper.createTestStopwordsSet(); + + StopwordsSetSchema result = this.client.stopwords("common-words").delete(); + + assertNotNull(result); + assertEquals("common-words", result.getId()); + } +}