Skip to content

Commit

Permalink
Merge pull request #20 from searchspring/autocomplete
Browse files Browse the repository at this point in the history
old autocomplete
  • Loading branch information
codeallthethingz authored Mar 31, 2022
2 parents 8844a75 + 5bfeeb3 commit be9188c
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 52 deletions.
31 changes: 11 additions & 20 deletions src/main/java/com/searchspring/nextopia/Converter.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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;
Expand Down Expand Up @@ -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<String, Object> container = new HashMap<>();
Map<String, Object> terms = new HashMap<>();
Map<String, Object> 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<String> 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<Map<String, Object>> productList = new ArrayList<>();
for (Product product : response.products) {
Map<String, Object> 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));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.searchspring.nextopia.model;

public class Alternative {
public String text;
}
9 changes: 0 additions & 9 deletions src/main/java/com/searchspring/nextopia/model/Product.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

public class SearchspringAutocompleteResponse {

public Product[] products;
public String[] terms;
public Suggested suggested;
public Alternative[] alternatives;

}
5 changes: 5 additions & 0 deletions src/main/java/com/searchspring/nextopia/model/Suggested.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package com.searchspring.nextopia.model;

public class Suggested {
public String text;
}
47 changes: 26 additions & 21 deletions src/test/java/com/searchspring/nextopia/ConverterTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "<query_time>0</query_time>";
private static final String PREFIX_SEARCH_EMPTY_BITS2 = "<custom_synonyms/>";
Expand All @@ -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\": \"<em>Red</em> Mop Head\"," +
"\"sku\": \"6051647\"," +
"\"url\": \"//www.wasserstrom.com/restaurant-supplies-equipment/Product_6051647\"" +
"}" +
"]," +
"\"terms\": [" +
"\"<em>red</em> bucket\"," +
"\"<em>red</em> 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
Expand Down Expand Up @@ -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}]"
Expand Down

0 comments on commit be9188c

Please sign in to comment.