diff --git a/src/main/java/com/searchspring/nextopia/Converter.java b/src/main/java/com/searchspring/nextopia/Converter.java index d916412..68b4057 100644 --- a/src/main/java/com/searchspring/nextopia/Converter.java +++ b/src/main/java/com/searchspring/nextopia/Converter.java @@ -14,7 +14,7 @@ import java.util.Set; import com.google.gson.Gson; -import com.searchspring.nextopia.model.Product; +import com.searchspring.nextopia.model.Alternative; import com.searchspring.nextopia.model.SearchspringAutocompleteResponse; import com.searchspring.nextopia.model.SearchspringSearchResponse; @@ -26,7 +26,7 @@ public class Converter { private final String SS_DOMAIN = ".a.searchspring.io"; private final String SS_SEARCH_PATH = "/api/search/search.json"; - private final String SS_AUTOCOMPLETE_PATH = "/api/suggest/legacy"; + private final String SS_AUTOCOMPLETE_PATH = "/api/suggest/query"; private final String SS_PRODUCT_COUNT = "productCount"; private final String siteId; @@ -55,34 +55,25 @@ public String convertSearchspringAutocompleteResponse(String callback, String se response = new SearchspringAutocompleteResponse(); } StringBuilder sb = new StringBuilder(callback + "("); - appendTermsAndProducts(sb, response); + appendSuggestedAndAlternatives(sb, response); sb.append(")"); return sb.toString(); } - public void appendTermsAndProducts(StringBuilder sb, SearchspringAutocompleteResponse response) { + public void appendSuggestedAndAlternatives(StringBuilder sb, SearchspringAutocompleteResponse response) { Map container = new HashMap<>(); Map terms = new HashMap<>(); - Map products = new HashMap<>(); container.put("terms", terms); - container.put("products", products); terms.put("n", "Popular Searches"); - if (response.terms != null) { - terms.put("r", response.terms); + ArrayList termList = new ArrayList<>(); + terms.put("r", termList); + if (response.suggested != null) { + termList.add(response.suggested.text); } - products.put("n", "Product Matches"); - if (response.products != null) { - List> productList = new ArrayList<>(); - for (Product product : response.products) { - Map productMap = new HashMap<>(); - productMap.put("Sku", product.sku); - productMap.put("Url", product.url); - productMap.put("Name", product.name); - productMap.put("Price", product.price); - productMap.put("Image", product.thumbnailImageUrl); - productList.add(productMap); + if (response.alternatives != null) { + for (Alternative alternative : response.alternatives) { + termList.add(alternative.text); } - products.put("r", productList); } sb.append(GSON.toJson(container)); } diff --git a/src/main/java/com/searchspring/nextopia/model/Alternative.java b/src/main/java/com/searchspring/nextopia/model/Alternative.java new file mode 100644 index 0000000..ba6d9bf --- /dev/null +++ b/src/main/java/com/searchspring/nextopia/model/Alternative.java @@ -0,0 +1,5 @@ +package com.searchspring.nextopia.model; + +public class Alternative { + public String text; +} diff --git a/src/main/java/com/searchspring/nextopia/model/Product.java b/src/main/java/com/searchspring/nextopia/model/Product.java deleted file mode 100644 index 71ecd10..0000000 --- a/src/main/java/com/searchspring/nextopia/model/Product.java +++ /dev/null @@ -1,9 +0,0 @@ -package com.searchspring.nextopia.model; - -public class Product { - public String sku; - public String url; - public String name; - public Double price; - public String thumbnailImageUrl; -} diff --git a/src/main/java/com/searchspring/nextopia/model/SearchspringAutocompleteResponse.java b/src/main/java/com/searchspring/nextopia/model/SearchspringAutocompleteResponse.java index f96d1d5..868dcc6 100644 --- a/src/main/java/com/searchspring/nextopia/model/SearchspringAutocompleteResponse.java +++ b/src/main/java/com/searchspring/nextopia/model/SearchspringAutocompleteResponse.java @@ -2,7 +2,7 @@ public class SearchspringAutocompleteResponse { - public Product[] products; - public String[] terms; + public Suggested suggested; + public Alternative[] alternatives; } \ No newline at end of file diff --git a/src/main/java/com/searchspring/nextopia/model/Suggested.java b/src/main/java/com/searchspring/nextopia/model/Suggested.java new file mode 100644 index 0000000..f482afa --- /dev/null +++ b/src/main/java/com/searchspring/nextopia/model/Suggested.java @@ -0,0 +1,5 @@ +package com.searchspring.nextopia.model; + +public class Suggested { + public String text; +} diff --git a/src/test/java/com/searchspring/nextopia/ConverterTest.java b/src/test/java/com/searchspring/nextopia/ConverterTest.java index 95535a6..e9456a6 100644 --- a/src/test/java/com/searchspring/nextopia/ConverterTest.java +++ b/src/test/java/com/searchspring/nextopia/ConverterTest.java @@ -30,7 +30,7 @@ public class ConverterTest { private static final String TEST_AUTOCOMPLETE_URL_PREFIX = "https://vector.nextopiasoftware.com/return_autocomplete_jsonp_v3.php?callback=callback&cid=66141eeeacafe959b288238d65b176cb&_=000000000"; private static final String EXPECTED_SEARCH_URL_PREFIX = "https://abcd12.a.searchspring.io/api/search/search.json?siteId=" + SITE_ID + "&resultsFormat=json"; - private static final String EXPECTED_AUTOCOMPLETE_URL_PREFIX = "https://abcd12.a.searchspring.io/api/suggest/legacy?siteId=" + private static final String EXPECTED_AUTOCOMPLETE_URL_PREFIX = "https://abcd12.a.searchspring.io/api/suggest/query?siteId=" + SITE_ID + "&productCount=4"; private static final String PREFIX_SEARCH_EMPTY_BITS = "0"; private static final String PREFIX_SEARCH_EMPTY_BITS2 = ""; @@ -50,26 +50,31 @@ public void convertAutocompleteUrlTest() throws URISyntaxException { @Test public void convertAutocompleteResponseTest() { - String seacrhspringJson = "{" + - "\"query\": \"red\"," + - "\"products\": [" + - "{" + - "\"thumbnailImageUrl\": \"https://s7d5.scene7.com/is/image/wasserstrom/6051647?defaultImage=noimage_wasserstrom&wid=220&hei=220\"," + - "\"price\": 4.8," + - "\"name\": \"Red Mop Head\"," + - "\"sku\": \"6051647\"," + - "\"url\": \"//www.wasserstrom.com/restaurant-supplies-equipment/Product_6051647\"" + - "}" + - "]," + - "\"terms\": [" + - "\"red bucket\"," + - "\"red wine glasses\"" + - "]" + - "}"; + String seacrhspringJson = "{" + + "\"query\": \"red\"," + + "\"suggested\": {" + + "\"text\": \"red\"," + + "\"type\": \"exact\"," + + "\"completed\": []," + + "\"source\": \"popular-query\"" + + "}," + + "\"alternatives\": [" + + "{" + + "\"popularity\": 12," + + "\"text\": \"red bucket\"" + + "}," + + "{" + + "\"popularity\": 13," + + "\"text\": \"red wine glass\"" + + "}," + + "{" + + "\"popularity\": 17," + + "\"text\": \"red wine glasses\"" + + "}" + + "]" + + "}"; String nextopiaJsonp = converter.convertSearchspringAutocompleteResponse("callback", seacrhspringJson); - assertEquals("callback({\"terms\":{\"r\":[\"\\u003cem\\u003ered\\u003c/em\\u003e bucket\",\"\\u003cem\\u003ered\\u003c/em\\u003e wine glasses\"],\"n\":\"Popular Searches\"}" + - ",\"products\":{\"r\":[{\"Price\":4.8,\"Sku\":\"6051647\",\"Image\":\"https://s7d5.scene7.com/is/image/wasserstrom/6051647?defaultImage\\u003dnoimage_wasserstrom\\u0026wid\\u003d220\\u0026hei\\u003d220\"," - + "\"Url\":\"//www.wasserstrom.com/restaurant-supplies-equipment/Product_6051647\",\"Name\":\"\\u003cem\\u003eRed\\u003c/em\\u003e Mop Head\"}],\"n\":\"Product Matches\"}})", nextopiaJsonp); + assertEquals("callback({\"terms\":{\"r\":[\"red\",\"red bucket\",\"red wine glass\",\"red wine glasses\"],\"n\":\"Popular Searches\"}})", nextopiaJsonp); } @Test @@ -101,7 +106,7 @@ public void convertSearchspringResponsePaginationTest() { @Test public void convertSearchspringResponseResultsTest() { String ssJson = "{\"pagination\": {\"totalResults\": 1981}," - + "\"didYouMean\": {\"query\": \"span\",\"highlighted\": \"\\u003cem\\u003espan\\u003c/em\\u003e\"}," + + "\"didYouMean\": {\"query\": \"span\",\"highlighted\": \"span\"}," + "\"results\": [ { \"uid\":\"1234\",\"brand\": \"Adidas\" } ]," + "\"facets\": [{\"field\":\"pattern_id7741124012283333869\", \"label\": \"Pattern\",\"type\": null,\"collapse\": 0,\"facet_active\": 0," + "\"values\": [{\"active\": false,\"type\": \"value\",\"value\": \"Baguette\",\"label\": \"Baguette\",\"count\": 21}]"