Rev 3656 | Rev 5729 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
package in.shop2020.serving.controllers;import in.shop2020.datalogger.EventType;import in.shop2020.serving.cache.EhcacheWrapper.CacheKeys;import in.shop2020.serving.cache.SnippetCacheWrapper;import in.shop2020.serving.services.SolrSearchService;import in.shop2020.serving.utils.Utils;import in.shop2020.utils.DataLogger;import java.io.UnsupportedEncodingException;import java.net.URLEncoder;import java.util.ArrayList;import java.util.Arrays;import java.util.HashMap;import java.util.LinkedHashMap;import java.util.List;import java.util.Map;import java.util.Map.Entry;import org.apache.commons.lang.StringUtils;import org.apache.log4j.Logger;import org.apache.struts2.convention.annotation.Result;/*** @author rajveer**/@Result(name="redirect", location="${location}", type="redirect")public class SearchController extends BaseController {private static final long serialVersionUID = -8392433517042806559L;private static Logger log = Logger.getLogger(Class.class);private List<String> results;/****/private Map<String, String> snippets;/****/private Map<String, List<String[]>> facets = null;/****/private List<String[]> crumbs;private String query;private String sortOrder = null;private Double minPrice = null;private Double maxPrice = null;private long windowSize = 10;private long page = 1;private long totalResults;private long beginIndex = 0;private String url;private String priceUrl = "?";private long categoryId = Utils.ROOT_CATEGORY;private String sortUrl;private String location;/**** @return* @throws UnsupportedEncodingException*/// GET /querypublic String index() throws UnsupportedEncodingException {log.info("this.request=" + this.request);//url = this.request.getRequestURL();query = this.request.getParameter("q");url = "q="+ URLEncoder.encode(query, "UTF-8");if(this.request.getParameter("category") != null){this.categoryId = Long.parseLong(this.request.getParameter("category"));url= url + "&category=" + this.request.getParameter("category");}if(query.trim().isEmpty()){if(categoryId == Utils.MOBILE_PHONES_CATEGORY){location = "/all-mobile-phones/" + categoryId;return "redirect";}else if(categoryId == Utils.MOBILE_ACCESSORIES_CATEGORY){location = "/all-mobile-accessories/" + categoryId;return "redirect";} else if(categoryId == Utils.TABLETS_CATEGORY){location = "/all-tablets/" + categoryId;return "redirect";} else if(categoryId == Utils.LAPTOPS_CATEGORY){location = "/all-laptops/" + categoryId;return "redirect";}else{return "index";}}sortUrl = url;if(this.request.getParameter("sort") != null){url= url + "&sort=" + this.request.getParameter("sort");sortOrder = this.request.getParameter("sort");}priceUrl += url;if(this.request.getParameter("page") != null){this.page = Long.parseLong(this.request.getParameter("page"));this.beginIndex = this.windowSize * (this.page-1);}if(this.request.getParameter("minPrice") != null){this.minPrice = (new Double(this.request.getParameter("minPrice")));url= url + "&minPrice=" + this.request.getParameter("minPrice");}if(this.request.getParameter("maxPrice") != null){this.maxPrice = (new Double(this.request.getParameter("maxPrice")));url= url + "&maxPrice=" + this.request.getParameter("maxPrice");}String[] fqrys = this.request.getParameterValues("fq");this.crumbs = new ArrayList<String[]>();String[] facetDefIDs = Utils.facetDefIDs;String[] facetLabels = Utils.facetLabels;String urlCrumb = "";if(query != null) {urlCrumb = url;}else {}if(fqrys != null) {log.info("fqrys=" + Arrays.toString(fqrys));List<String> arrList = Arrays.asList(facetDefIDs);for(int i=0; i<fqrys.length; i++){urlCrumb += "&fq=" + URLEncoder.encode(fqrys[i], "UTF-8");priceUrl += "&fq=" + URLEncoder.encode(fqrys[i], "UTF-8");sortUrl += "&fq=" + URLEncoder.encode(fqrys[i], "UTF-8");StringBuilder filterUrl = new StringBuilder(url);String facetName = StringUtils.split(fqrys[i], ":")[0];String facetValue = StringUtils.split(fqrys[i], ":")[1];String facetLabel = facetLabels[arrList.indexOf(facetName)];for(int j=0; j<fqrys.length; j++) {if(i==j){continue;}filterUrl.append("&fq=" + URLEncoder.encode(fqrys[j], "UTF-8"));}String[] acrumb = new String[] { facetLabel, facetValue, filterUrl.toString() };log.info("acrumb=" + Arrays.toString(acrumb));this.crumbs.add(acrumb);}}url = urlCrumb;SolrSearchService search = new SolrSearchService(query, fqrys, facetDefIDs, (page-1)*windowSize, windowSize, minPrice, maxPrice, categoryId, sortOrder, sourceId);this.results = search.getResultMap();// FacetsString qryString = this.request.getQueryString();log.info("qryString=" + qryString);this.facets = new LinkedHashMap<String, List<String[]>>();for (int i=0; i<facetDefIDs.length; i++) {String facetDefID = facetDefIDs[i];String facetLabel = facetLabels[i];HashMap<String, Integer> facetDetailMap = search.getFacetDetails(facetDefID);if(facetDetailMap==null)continue;List<String[]> values = new ArrayList<String[]>();for(Entry<String, Integer> facetEntry : facetDetailMap.entrySet()){String facet = facetEntry.getKey();String drilldownURL = url;drilldownURL += "&fq=" + facetDefID + ":" +URLEncoder.encode(facet, "UTF-8");String[] afacet = new String[] { facet,facetEntry.getValue().toString(), drilldownURL };values.add(afacet);}this.facets.put(facetLabel, values);}Map<String, Double> priceMap = search.getPriceStatsMap();if(priceMap != null){this.minPrice = priceMap.get("min");this.maxPrice = priceMap.get("max");}else{this.minPrice = 0.0;this.maxPrice = 0.0;}this.totalResults = search.getTotalResults();DataLogger.logData(EventType.PRODUCT_SEARCH, getSessionId(), userinfo.getUserId(), userinfo.getEmail(),query, Long.toString(categoryId), Long.toString(totalResults));return "index";}public Map<String, String> getSnippets() throws Exception {if(results != null){snippets = new HashMap<String, String>();for(String docId: results){String snippet = (String)SnippetCacheWrapper.getSnippet(CacheKeys.SEARCH_SNIPPET_CACHE_KEY, docId, sourceId);if (snippet != null) {snippets.put(docId, snippet);}}}return snippets;}/**** @return*/public String getQuery() {return this.query;}/**** @return*/public List<String> getResults() {return this.results;}/**** @return*/public Map<String, List<String[]>> getFacets() {return this.facets;}public long getTotalResults(){return totalResults;}public String getUrl(){return this.url;}public String getPriceUrl(){return this.priceUrl;}public String getSortUrl(){return this.sortUrl;}public long getBeginIndex(){if(totalResults>0)return beginIndex+1;return beginIndex;}public long getTotalPages() {return 1 + (totalResults-1)/windowSize;}public long getCurrentPage() {return this.page;}public Double getMinPrice() {return this.minPrice;}public Double getMaxPrice() {return this.maxPrice;}public List<String[]> getCrumbs() {return this.crumbs;}public String getSortOrder(){return this.sortOrder;}public String getLocation(){return this.location;}}