Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add search presets support #75

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,34 @@ StopwordsSetsRetrieveAllSchema sets = client.stopwords().retrieve();
client.stopwords("common-words").delete();
```

### Create or update a search preset
```java
SearchParameters params = new SearchParameters()
.q("bestseller")
.queryBy("title,author")
.sortBy("ratings_count:desc");

PresetUpsertSchema preset = new PresetUpsertSchema()
.value(params);

client.presets().upsert("bestsellers_view", preset);
```

### Retrieve a search preset
```java
PresetSchema preset = client.presets("bestsellers_view").retrieve();
```

### Retrieve all search presets
```java
PresetsRetrieveSchema presets = client.presets().retrieve();
```

### Delete a search preset
```java
client.presets("bestsellers_view").delete();
```

### Create an API key
```java
ApiKeySchema apiKeySchema = new ApiKeySchema();
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/org/typesense/api/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ public class Client {
private Stopwords stopwords;
private Map<String, StopwordsSet> individualStopwordsSets;

private Presets presets;
private Map<String, Preset> individualPresets;

public Health health;
public Operations operations;
public Metrics metrics;
Expand All @@ -47,6 +50,8 @@ public Client(Configuration configuration){
this.analytics = new Analytics(this.apiCall);
this.stopwords = new Stopwords(this.apiCall);
this.individualStopwordsSets = new HashMap<>();
this.presets = new Presets(this.apiCall);
this.individualPresets = new HashMap<>();
}

public Collection collections(String name){
Expand Down Expand Up @@ -114,4 +119,19 @@ public StopwordsSet stopwords(String stopwordsSetId) {
retVal = this.individualStopwordsSets.get(stopwordsSetId);
return retVal;
}

public Presets presets() {
return this.presets;
}

public Preset presets(String presetId) {
Preset retVal;

if (!this.individualPresets.containsKey(presetId)) {
this.individualPresets.put(presetId, new Preset(presetId, this.apiCall));
}

retVal = this.individualPresets.get(presetId);
return retVal;
}
}
28 changes: 28 additions & 0 deletions src/main/java/org/typesense/api/Preset.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package org.typesense.api;

import org.typesense.api.utils.URLEncoding;
import org.typesense.model.PresetDeleteSchema;
import org.typesense.model.PresetSchema;

public class Preset {
private final ApiCall apiCall;
private final String name;

public Preset(String name, ApiCall apiCall) {
this.name = name;
this.apiCall = apiCall;
}

public PresetSchema retrieve() throws Exception {
return this.apiCall.get(this.getEndpoint(), null, PresetSchema.class);
}

public PresetDeleteSchema delete() throws Exception {
return this.apiCall.delete(this.getEndpoint(), null, PresetDeleteSchema.class);
}

private String getEndpoint() {
return Presets.RESOURCEPATH + "/" + URLEncoding.encodeURIComponent(this.name);
}

}
29 changes: 29 additions & 0 deletions src/main/java/org/typesense/api/Presets.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package org.typesense.api;

import org.typesense.api.utils.URLEncoding;
import org.typesense.model.PresetSchema;
import org.typesense.model.PresetUpsertSchema;
import org.typesense.model.PresetsRetrieveSchema;

public class Presets {
public final static String RESOURCEPATH = "/presets";

private final ApiCall apiCall;

public Presets(ApiCall apiCall) {
this.apiCall = apiCall;
}

public PresetSchema upsert(String name, PresetUpsertSchema preset) throws Exception {
return this.apiCall.put(getEndpoint(name), preset, null, PresetSchema.class);
}

public PresetsRetrieveSchema retrieve() throws Exception {
return this.apiCall.get(Presets.RESOURCEPATH, null, PresetsRetrieveSchema.class);
}

private String getEndpoint(String name) {
return RESOURCEPATH + "/" + URLEncoding.encodeURIComponent(name);
}

}
20 changes: 20 additions & 0 deletions src/test/java/org/typesense/api/Helper.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,13 @@
import org.typesense.model.CollectionResponse;
import org.typesense.model.CollectionSchema;
import org.typesense.model.Field;
import org.typesense.model.PresetSchema;
import org.typesense.model.PresetUpsertSchema;
import org.typesense.model.PresetsRetrieveSchema;
import org.typesense.model.SearchOverrideInclude;
import org.typesense.model.SearchOverrideRule;
import org.typesense.model.SearchOverrideSchema;
import org.typesense.model.SearchParameters;
import org.typesense.model.SearchSynonymSchema;
import org.typesense.model.StopwordsSetSchema;
import org.typesense.model.StopwordsSetUpsertSchema;
Expand Down Expand Up @@ -140,6 +144,17 @@ public void createTestStopwordsSet() throws Exception {
client.stopwords().upsert("common-words", stopwordsSetSchema);
}

public void createTestPreset() throws Exception {
SearchParameters params = new SearchParameters()
.q("Romeo")
.queryBy("title");

PresetUpsertSchema preset = new PresetUpsertSchema()
.value(params);

client.presets().upsert("listing_view", preset);
}

public void teardown() throws Exception {
CollectionResponse[] collectionResponses = client.collections().retrieve();
for (CollectionResponse c : collectionResponses) {
Expand All @@ -165,5 +180,10 @@ public void teardown() throws Exception {
for (StopwordsSetSchema s : stopwords.getStopwords()) {
client.stopwords(s.getId()).delete();
}

PresetsRetrieveSchema presets = client.presets().retrieve();
for (PresetSchema p : presets.getPresets()) {
client.presets(p.getName()).delete();
}
}
}
88 changes: 88 additions & 0 deletions src/test/java/org/typesense/api/PresetsTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package org.typesense.api;

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.OneOfPresetUpsertSchemaValue;
import org.typesense.model.PresetDeleteSchema;
import org.typesense.model.PresetSchema;
import org.typesense.model.PresetUpsertSchema;
import org.typesense.model.PresetsRetrieveSchema;
import org.typesense.model.SearchParameters;

public class PresetsTest {

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 {
SearchParameters params = new SearchParameters()
.q("Romeo")
.queryBy("title");

PresetUpsertSchema preset = new PresetUpsertSchema()
.value(params);

PresetSchema result = client.presets().upsert("listing_view", preset);

assertNotNull(result);
assertEquals("listing_view", result.getName());

OneOfPresetUpsertSchemaValue value = result.getValue();
assertNotNull(value);
}

@Test
void testRetrieve() throws Exception {
helper.createTestPreset();

PresetSchema result = this.client.presets("listing_view").retrieve();

assertNotNull(result);
assertEquals("listing_view", result.getName());
assertNotNull(result.getValue());
}

@Test
void testRetrieveAll() throws Exception {
helper.createTestPreset();

PresetsRetrieveSchema result = this.client.presets().retrieve();

assertNotNull(result);

assertEquals(1, result.getPresets().size());

PresetSchema preset = result.getPresets().get(0);
assertEquals("listing_view", preset.getName());
assertNotNull(preset.getValue());
}

@Test
void testDelete() throws Exception {
helper.createTestPreset();

PresetDeleteSchema result = this.client.presets("listing_view").delete();

assertNotNull(result);

assertEquals("listing_view", result.getName());

}
}