From 23793d312f4c4725d09b4dc7186fec4ad21b1c12 Mon Sep 17 00:00:00 2001 From: Hung Date: Mon, 27 Nov 2023 17:07:51 +0800 Subject: [PATCH] add autocomplete --- .../visearch/android/ProductSearch.java | 5 ++ .../visearch/android/ProductSearchTest.java | 51 +++++++++++++++++++ 2 files changed, 56 insertions(+) diff --git a/visearch-android/src/main/java/com/visenze/visearch/android/ProductSearch.java b/visearch-android/src/main/java/com/visenze/visearch/android/ProductSearch.java index e5e61c3..c019d7a 100644 --- a/visearch-android/src/main/java/com/visenze/visearch/android/ProductSearch.java +++ b/visearch-android/src/main/java/com/visenze/visearch/android/ProductSearch.java @@ -45,6 +45,11 @@ public void multisearch(ProductSearchByImageParams imageSearchParams, ResultList productSearchService.searchByImage(imageSearchParams, listener, true); } + public void multisearchAutocomplete(ProductSearchByImageParams imageSearchParams, AutoCompleteResultListener listener) { + addAnalyticsParams(imageSearchParams); + productSearchService.multisearchAutocomplete(imageSearchParams, listener); + } + public void recommendations(ProductSearchByIdParams visualSimilarParams, ResultListener listener) { searchById(visualSimilarParams, listener); } diff --git a/visearch-android/src/test/java/com/visenze/visearch/android/ProductSearchTest.java b/visearch-android/src/test/java/com/visenze/visearch/android/ProductSearchTest.java index f9bf486..3749dd3 100644 --- a/visearch-android/src/test/java/com/visenze/visearch/android/ProductSearchTest.java +++ b/visearch-android/src/test/java/com/visenze/visearch/android/ProductSearchTest.java @@ -6,6 +6,8 @@ import com.google.common.base.Joiner; import com.google.gson.Gson; import com.google.gson.GsonBuilder; +import com.visenze.visearch.android.model.AutoCompleteResponse; +import com.visenze.visearch.android.model.AutoCompleteResultItem; import com.visenze.visearch.android.model.BestImage; import com.visenze.visearch.android.model.Box; import com.visenze.visearch.android.model.ErrorData; @@ -1014,6 +1016,55 @@ public void onSearchResult(ProductResponse response, ErrorData error) { } + @Test + public void testAutocompleteResponse() { + String json = + "{\n" + + " \"result\": [\n" + + " {\n" + + " \"text\": \"red1\",\n" + + " \"score\": 898.0\n" + + " },\n" + + " {\n" + + " \"text\": \"red valentino\",\n" + + " \"score\": 188.0\n" + + " },\n" + + " {\n" + + " \"text\": \"oscar de la renta\",\n" + + " \"score\": 46.0\n" + + " }\n" + + " ],\n" + + " \"method\": \"multisearch/autocomplete\",\n" + + " \"status\": \"OK\",\n" + + " \"page\": 1,\n" + + " \"reqid\": \"14952330933117065212\"\n" + + "}"; + + AutoCompleteResponse response = gson.fromJson(json, AutoCompleteResponse.class); + searchService.handleAutoCompleteResponse(response, new ProductSearch.AutoCompleteResultListener() { + @Override + public void onResult(AutoCompleteResponse response, ErrorData error) { + assertNull(error); + + assertEquals(1, response.getPage()); + assertEquals("14952330933117065212", response.getReqId()); + + AutoCompleteResultItem item = response.getResult().get(0); + assertEquals("red1", item.getText()); + assertTrue(898.0 == item.getScore()); + + AutoCompleteResultItem item2 = response.getResult().get(1); + assertEquals("red valentino", item2.getText()); + assertTrue(188.0 == item2.getScore()); + + AutoCompleteResultItem item3 = response.getResult().get(2); + assertEquals("oscar de la renta", item3.getText()); + assertTrue(46.0 == item3.getScore()); + } + }); + + } + private String getBoxString(Box box) { return box.getX1() + "," + box.getY1() + "," + box.getX2() + "," + box.getY2(); }