Skip to content

Commit

Permalink
Merge pull request #103 from visenze/feature/multisearch
Browse files Browse the repository at this point in the history
[API-9945] Multisearch support
  • Loading branch information
thehung111 authored Nov 24, 2023
2 parents 233fd3d + 0049753 commit 98c68c6
Show file tree
Hide file tree
Showing 12 changed files with 463 additions and 117 deletions.
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
<groupId>com.visenze</groupId>
<artifactId>visearch-java-sdk</artifactId>
<name>ViSearch Java SDK</name>
<version>1.14.0</version>
<version>1.14.1</version>
<packaging>jar</packaging>
<url>https://github.com/visenze/visearch-sdk-java</url>
<description>ViSearch Java SDK</description>
Expand Down
50 changes: 50 additions & 0 deletions src/main/java/com/visenze/productsearch/AutoCompleteResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package com.visenze.productsearch;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.visenze.common.util.ViJsonMapper;
import com.visenze.visearch.AutoCompleteResultItem;
import com.visenze.visearch.ResponseMessages;
import com.visenze.visearch.internal.InternalViSearchException;
import com.visenze.visearch.internal.http.ViSearchHttpResponse;
import java.io.IOException;
import java.util.List;

/**
* Created by Hung on 24/11/23.
*/
@JsonIgnoreProperties(ignoreUnknown = true)
public class AutoCompleteResponse extends BaseProductSearchResponse {

@JsonProperty("result")
private List<AutoCompleteResultItem> result;

/**
* Delegated construction with a ViHttpResponse will automatically parse the
* response into data members.
*
* Q: Why do we need to use a static function to create object instead of
* calling constructor straight?
* A: Because Java don't support self assignment. i.e. We cannot convert a
* response json text body into T object from T's own constructor.
*
* @param response The ViHttpResponse received by calling ViHttpClient
* api functions.
* @return Product Search autocomplete response
*/
public static AutoCompleteResponse fromResponse(ViSearchHttpResponse response) {
try {
return mapper.readValue(response.getBody(), AutoCompleteResponse.class);
} catch(IOException e){
throw new InternalViSearchException(ResponseMessages.PARSE_RESPONSE_ERROR, e.getMessage());
}
}

public List<AutoCompleteResultItem> getResult() {
return result;
}

public void setResult(List<AutoCompleteResultItem> result) {
this.result = result;
}
}
114 changes: 114 additions & 0 deletions src/main/java/com/visenze/productsearch/BaseProductSearchResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
package com.visenze.productsearch;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.visenze.common.util.ViJsonMapper;
import com.visenze.productsearch.response.ErrorMsg;

/**
* Created by Hung on 24/11/23.
*/
public abstract class BaseProductSearchResponse extends ViJsonMapper {

/**
* Each request is associated with an id to help with tracking api calls.
*
* The actual field name is called 'reqid' but we want to store it as
* 'requestId' inside this class. The @JsonProperty("...") annotation tells
* JSON that this variable 'requestId' will be serialized/deserialized as
* 'reqid' from JSON's point of view.
* i.e. some_json_map_thing["reqid"] = this_class.requestId;
*/
@JsonProperty("reqid")
private String requestId;

/**
* The request status, either “OK”, or “fail”.
*/
@JsonProperty("status")
private String status;

/**
* The result page number. Each response is tied to 1 'page' of a response.
* Since there can be potentially 1000 results and we limit to 10 results at
* a time, meaning 100 pages of 10 results each. This page number indicates
* which page it is displaying.
*/
@JsonProperty("page")
private int page;

/**
* The number of results per page. Use to determine how many results we will
* display one each 'page' at a time.
*/
@JsonProperty("limit")
private int limit;

/**
* Total number of search results.
*/
@JsonProperty("total")
private int total;

/**
* Error message and code if the request was not successful
* i.e. when status is “fail”.
*/
@JsonProperty("error")
private ErrorMsg error;

/**
* Each request is associated with an id to help with tracking api calls.
*
* @return String representation of the request identifier
*/
public String getRequestId() {
return requestId;
}

/**
* The request status, either “OK”, or “fail”.
*
* @return String status of the request sent
*/
public String getStatus() {
return status;
}

/**
* Get Page number.
*
* @return The result page number
*/
public int getPage() {
return page;
}

/**
* Get Results per page.
*
* @return The number of results per page
*/
public int getLimit() {
return limit;
}

/**
* Get Number of results.
*
* @return The total number of results
*/
public int getTotal() {
return total;
}

/**
* Get the error code.
*
* @return Map of error key-value pairs.
*/
public ErrorMsg getError() {
return error;
}


}
17 changes: 17 additions & 0 deletions src/main/java/com/visenze/productsearch/ProductSearch.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public class ProductSearch {
static final String DEFAULT_ENDPOINT = "https://search.visenze.com";
static final String DEFAULT_IMAGE_SEARCH_PATH = "/v1/product/search_by_image";
static final String DEFAULT_MULTI_SEARCH_PATH = "/v1/product/multisearch";
static final String DEFAULT_MULTI_SEARCH_AUTOCOMPLETE_PATH = "/v1/product/multisearch/autocomplete";

static final String DEFAULT_VISUAL_SIMILAR_PATH = "/v1/product/search_by_id";
static final String DEFAULT_RECOMMENDATION_PATH = "/v1/product/recommendations";
Expand Down Expand Up @@ -169,6 +170,22 @@ public ProductSearchResponse multiSearch(SearchByImageParam params) {
return postImageSearch(params, DEFAULT_MULTI_SEARCH_PATH);
}

public AutoCompleteResponse multiSearchAutocomplete(SearchByImageParam params) {
Multimap<String, String> paramMap = addAuth2Map(params);

final File imageFile = params.getImage();

if (imageFile != null) {
try {
return AutoCompleteResponse.fromResponse(httpClient.postImage(DEFAULT_MULTI_SEARCH_AUTOCOMPLETE_PATH, paramMap, new FileInputStream(imageFile), imageFile.getName()));
} catch (FileNotFoundException e) {
throw new InternalViSearchException(ResponseMessages.INVALID_IMAGE_OR_URL, e);
}
}

return AutoCompleteResponse.fromResponse(httpClient.post(DEFAULT_MULTI_SEARCH_AUTOCOMPLETE_PATH, paramMap));
}

/**
* Calls the POST method for the image search API
*
Expand Down
101 changes: 1 addition & 100 deletions src/main/java/com/visenze/productsearch/ProductSearchResponse.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,60 +45,14 @@
* @since 12 Jan 2021
*/
@JsonIgnoreProperties(ignoreUnknown = true)
public class ProductSearchResponse extends ViJsonMapper {
/**
* Each request is associated with an id to help with tracking api calls.
*
* The actual field name is called 'reqid' but we want to store it as
* 'requestId' inside this class. The @JsonProperty("...") annotation tells
* JSON that this variable 'requestId' will be serialized/deserialized as
* 'reqid' from JSON's point of view.
* i.e. some_json_map_thing["reqid"] = this_class.requestId;
*/
@JsonProperty("reqid")
private String requestId;

/**
* The request status, either “OK”, or “fail”.
*/
@JsonProperty("status")
private String status;
public class ProductSearchResponse extends BaseProductSearchResponse {

/**
* Image ID. Can be used to search again without re-uploading.
*/
@JsonProperty("im_id")
private String imageId;

/**
* The result page number. Each response is tied to 1 'page' of a response.
* Since there can be potentially 1000 results and we limit to 10 results at
* a time, meaning 100 pages of 10 results each. This page number indicates
* which page it is displaying.
*/
@JsonProperty("page")
private int page;

/**
* The number of results per page. Use to determine how many results we will
* display one each 'page' at a time.
*/
@JsonProperty("limit")
private int limit;

/**
* Total number of search results.
*/
@JsonProperty("total")
private int total;

/**
* Error message and code if the request was not successful
* i.e. when status is “fail”.
*/
@JsonProperty("error")
private ErrorMsg error;

/**
* This list of product types picked up from the image.
*
Expand Down Expand Up @@ -189,24 +143,6 @@ public static ProductSearchResponse fromResponse(ViSearchHttpResponse response)
}
}

/**
* Each request is associated with an id to help with tracking api calls.
*
* @return String representation of the request identifier
*/
public String getRequestId() {
return requestId;
}

/**
* The request status, either “OK”, or “fail”.
*
* @return String status of the request sent
*/
public String getStatus() {
return status;
}

/**
* Image ID. Can be used to search again without re-uploading.
*
Expand All @@ -216,41 +152,6 @@ public String getImageId() {
return imageId;
}

/**
* Get Page number.
*
* @return The result page number
*/
public int getPage() {
return page;
}

/**
* Get Results per page.
*
* @return The number of results per page
*/
public int getLimit() {
return limit;
}

/**
* Get Number of results.
*
* @return The total number of results
*/
public int getTotal() {
return total;
}

/**
* Get the error code.
*
* @return Map of error key-value pairs.
*/
public ErrorMsg getError() {
return error;
}

/**
* Get the product types.
Expand Down
41 changes: 41 additions & 0 deletions src/main/java/com/visenze/visearch/AutoCompleteResult.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.visenze.visearch;

import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import java.util.List;

/**
* Created by Hung on 24/11/23.
*/
@JsonIgnoreProperties(ignoreUnknown = true)
public class AutoCompleteResult extends PagedResult<AutoCompleteResultItem> {

private String reqId;

private String rawJson;

public AutoCompleteResult(List<AutoCompleteResultItem> result) {
this.result = result;
}

public AutoCompleteResult(String errorMessage, Throwable e, String rawResponse) {
super.setErrorMessage(errorMessage);
super.setCause(e);
super.setRawResponseMessage(rawResponse);
}

public String getReqId() {
return reqId;
}

public void setReqId(String reqId) {
this.reqId = reqId;
}

public String getRawJson() {
return rawJson;
}

public void setRawJson(String rawJson) {
this.rawJson = rawJson;
}
}
Loading

0 comments on commit 98c68c6

Please sign in to comment.