Skip to content

Commit

Permalink
Add collection update API.
Browse files Browse the repository at this point in the history
  • Loading branch information
kishorenc committed May 2, 2022
1 parent a70db00 commit 7f26fa1
Show file tree
Hide file tree
Showing 5 changed files with 169 additions and 6 deletions.
4 changes: 2 additions & 2 deletions src/main/java/org/typesense/api/ApiCall.java
Original file line number Diff line number Diff line change
Expand Up @@ -171,15 +171,15 @@ <T, R> T put(String endpoint, R body, Class<T> resourceClass) throws Exception {
return makeRequest(endpoint, r, resourceClass);
}

<T, R> T patch(String endpoint, R body) throws Exception {
<T, R> T patch(String endpoint, R body, Class<T> resourceClass) throws Exception {

RequestHandler r = (String REST_URI) -> this.client.target(REST_URI)
.request(MediaType.APPLICATION_JSON)
.header(API_KEY_HEADER, apiKey)
.build("PATCH", Entity.entity(body, MediaType.APPLICATION_JSON))
.invoke();

return makeRequest(endpoint, r, null);
return makeRequest(endpoint, r, resourceClass);
}


Expand Down
5 changes: 5 additions & 0 deletions src/main/java/org/typesense/api/Collection.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.typesense.api.exceptions.TypesenseError;
import org.typesense.model.CollectionResponse;
import org.typesense.model.CollectionUpdateSchema;

import java.util.HashMap;

Expand Down Expand Up @@ -41,6 +42,10 @@ public CollectionResponse retrieve() throws Exception {
return this.apiCall.get(endpoint, CollectionResponse.class);
}

public CollectionUpdateSchema update(CollectionUpdateSchema c) throws Exception {
return this.apiCall.patch(endpoint, c, CollectionUpdateSchema.class);
}

public CollectionResponse delete() throws Exception {
return this.apiCall.delete(endpoint,CollectionResponse.class);
}
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/org/typesense/api/Document.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ public class Document {
this.endpoint = Collections.RESOURCE_PATH + "/" + this.collectionName + Documents.RESOURCE_PATH + "/" + this.documentId;
}

HashMap<String,Object> retrieve() throws Exception {
public HashMap<String,Object> retrieve() throws Exception {
return this.apiCall.get(endpoint);
}

HashMap<String, Object> delete() throws Exception {
public HashMap<String, Object> delete() throws Exception {
return this.apiCall.delete(this.endpoint);
}

HashMap<String , Object> update(HashMap<String, Object> document) throws Exception {
return this.apiCall.patch(this.endpoint, document);
public HashMap<String , Object> update(HashMap<String, Object> document) throws Exception {
return this.apiCall.patch(this.endpoint, document, HashMap.class);
}

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

import java.util.ArrayList;
import java.util.List;
import org.typesense.model.Field;

import io.swagger.v3.oas.annotations.media.Schema;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlType;
import javax.xml.bind.annotation.XmlEnum;
import javax.xml.bind.annotation.XmlEnumValue;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonValue;
import com.fasterxml.jackson.annotation.JsonCreator;

public class CollectionUpdateSchema {

@Schema(example = "[{\"name\":\"company_name\",\"type\":\"string\",\"facet\":false},{\"name\":\"num_employees\",\"type\":\"int32\",\"facet\":false},{\"name\":\"country\",\"type\":\"string\",\"facet\":true}]", required = true, description = "A list of fields for querying, filtering and faceting")
/**
* A list of fields for querying, filtering and faceting
**/
private List<Field> fields = new ArrayList<Field>();
/**
* A list of fields for querying, filtering and faceting
* @return fields
**/
@JsonProperty("fields")
public List<Field> getFields() {
return fields;
}

public void setFields(List<Field> fields) {
this.fields = fields;
}

public CollectionUpdateSchema fields(List<Field> fields) {
this.fields = fields;
return this;
}

public CollectionUpdateSchema addFieldsItem(Field fieldsItem) {
this.fields.add(fieldsItem);
return this;
}


@Override
public String toString() {
StringBuilder sb = new StringBuilder();
sb.append("class CollectionUpdateSchema {\n");

sb.append(" fields: ").append(toIndentedString(fields)).append("\n");
sb.append("}");
return sb.toString();
}

/**
* Convert the given object to string with each line indented by 4 spaces
* (except the first line).
*/
private static String toIndentedString(java.lang.Object o) {
if (o == null) {
return "null";
}
return o.toString().replace("\n", "\n ");
}
}
88 changes: 88 additions & 0 deletions src/main/java/org/typesense/model/Field.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,18 @@ public class Field {

@Schema(example = "true", description = "")
private Boolean index = true;

@Schema(example = "el", description = "")
private String locale = null;

@Schema(example = "true", description = "")
private Boolean sort = false;

@Schema(example = "true", description = "")
private Boolean infix = false;

@Schema(example = "true", description = "")
private Boolean drop = null;
/**
* Get name
* @return name
Expand Down Expand Up @@ -119,6 +131,78 @@ public Field index(Boolean index) {
return this;
}

/**
* Get locale
* @return locale
**/
@JsonProperty("locale")
public String getLocale() {
return locale;
}

public void setLocale(String locale) {
this.locale = locale;
}

public Field locale(String locale) {
this.locale = locale;
return this;
}

/**
* Get sort
* @return sort
**/
@JsonProperty("sort")
public Boolean isSort() {
return sort;
}

public void setSort(Boolean sort) {
this.sort = sort;
}

public Field sort(Boolean sort) {
this.sort = sort;
return this;
}

/**
* Get infix
* @return infix
**/
@JsonProperty("infix")
public Boolean isInfix() {
return infix;
}

public void setInfix(Boolean infix) {
this.infix = infix;
}

public Field infix(Boolean infix) {
this.infix = infix;
return this;
}

/**
* Get drop
* @return drop
**/
@JsonProperty("drop")
public Boolean isDrop() {
return drop;
}

public void setDrop(Boolean drop) {
this.drop = drop;
}

public Field drop(Boolean drop) {
this.drop = drop;
return this;
}


@Override
public String toString() {
Expand All @@ -130,6 +214,10 @@ public String toString() {
sb.append(" optional: ").append(toIndentedString(optional)).append("\n");
sb.append(" facet: ").append(toIndentedString(facet)).append("\n");
sb.append(" index: ").append(toIndentedString(index)).append("\n");
sb.append(" locale: ").append(toIndentedString(locale)).append("\n");
sb.append(" sort: ").append(toIndentedString(sort)).append("\n");
sb.append(" infix: ").append(toIndentedString(infix)).append("\n");
sb.append(" drop: ").append(toIndentedString(drop)).append("\n");
sb.append("}");
return sb.toString();
}
Expand Down

0 comments on commit 7f26fa1

Please sign in to comment.